Skip to content

Commit cb2c80e

Browse files
committed
temp
1 parent 518d178 commit cb2c80e

32 files changed

Lines changed: 746 additions & 430 deletions

lib/libkiwi/ap/kiwiAPServer.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <libkiwi.h>
2+
3+
namespace kiwi {
4+
namespace ap {
5+
6+
/**
7+
* @brief Constructor
8+
*
9+
* @param pParser Message parser
10+
* @param port Local port
11+
*/
12+
Server::Server(IMessageParser* pParser, u16 port)
13+
: mpSocket(nullptr), mpConnection(nullptr), mpParser(pParser) {
14+
15+
K_ASSERT_PTR(mpParser);
16+
17+
mpSocket = new kiwi::SyncSocket(SO_PF_INET, SO_SOCK_DGRAM);
18+
K_ASSERT_PTR(mpSocket);
19+
mpSocket->SetBlocking(false);
20+
21+
// Variable packets only enforce an upper size bound
22+
kiwi::IPacketFactory* pPacketFactory =
23+
new kiwi::VariablePacketFactory<MAX_PACKET_SIZE>();
24+
K_ASSERT_PTR(pPacketFactory);
25+
26+
mpConnection = new kiwi::NetServer(mpSocket, pPacketFactory);
27+
K_ASSERT_PTR(mpConnection);
28+
29+
// Bind to any available local address
30+
kiwi::SockAddr4 addr(port);
31+
bool success = mpConnection->Bind(addr);
32+
ASSERT_EX(success, "Can't bind server socket on port %d", addr.port);
33+
34+
mpConnection->SetRecvCallback(PacketCallback, this);
35+
36+
// Show IP address in the console
37+
u16 portSave = addr.port;
38+
mpSocket->GetHostAddr(addr);
39+
addr.port = portSave;
40+
K_LOG_EX("Server listening on: %s\n", kiwi::ToString(addr).CStr());
41+
}
42+
43+
/**
44+
* @brief Destructor
45+
*/
46+
Server::~Server() {
47+
delete mpConnection;
48+
mpConnection = nullptr;
49+
50+
delete mpSocket;
51+
mpSocket = nullptr;
52+
53+
delete mpParser;
54+
mpParser = nullptr;
55+
}
56+
57+
/**
58+
* @brief Packet receive callback
59+
*
60+
* @param pPacket Incoming packet
61+
* @param pArg Callback user argument
62+
*/
63+
void Server::PacketCallback(kiwi::PacketBase* pPacket, void* pArg) {
64+
K_ASSERT_PTR(pPacket);
65+
K_ASSERT_PTR(pArg);
66+
67+
// Callback argument is this object
68+
Server* p = static_cast<Server*>(pArg);
69+
K_ASSERT_PTR(p->mpParser);
70+
71+
IMessage* pMessage =
72+
p->mpParser->Parse(pPacket->GetContent(), pPacket->GetContentSize());
73+
74+
if (pMessage == nullptr) {
75+
return;
76+
}
77+
78+
// Notify listeners
79+
K_FOREACH (it, p->mListenerList) {
80+
K_ASSERT_PTR(*it);
81+
(*it)->OnMessage(pMessage, p->mpConnection);
82+
}
83+
84+
// Release message memory
85+
delete pMessage;
86+
}
87+
88+
} // namespace ap
89+
} // namespace kiwi

lib/libkiwi/ap/kiwiAPServer.h

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#ifndef LIBKIWI_AP_AP_SERVER_H
2+
#define LIBKIWI_AP_AP_SERVER_H
3+
#include <libkiwi/k_types.h>
4+
#include <libkiwi/net/kiwiPortRegistry.h>
5+
#include <libkiwi/prim/kiwiList.h>
6+
#include <libkiwi/support/kiwiLibSO.h>
7+
8+
namespace kiwi {
9+
//! @addtogroup libkiwi_fun
10+
//! @{
11+
12+
// Forward declarations
13+
class NetServer;
14+
class PacketBase;
15+
class SockAddrAny;
16+
class SyncSocket;
17+
18+
namespace ap {
19+
//! @addtogroup libkiwi_fun
20+
//! @{
21+
22+
// Forward declarations
23+
class IMessageListener;
24+
class IMessageParser;
25+
26+
/**
27+
* @brief Archipelago server
28+
*/
29+
class Server {
30+
public:
31+
/**
32+
* @brief Constructor
33+
*
34+
* @param pParser Message parser (owned by this server)
35+
* @param port Local port
36+
*/
37+
Server(IMessageParser* pParser, u16 port = port::AP_COMM);
38+
39+
/**
40+
* @brief Destructor
41+
*/
42+
virtual ~Server();
43+
44+
/**
45+
* @brief Registers a new message listener
46+
*
47+
* @param pListener Message listener
48+
*/
49+
void RegistListener(IMessageListener* pListener) {
50+
K_ASSERT_PTR(pListener);
51+
mListenerList.PushBack(pListener);
52+
}
53+
54+
/**
55+
* @brief Removes a message listener
56+
*
57+
* @param pListener Message listener
58+
*/
59+
void RemoveListener(IMessageListener* pListener) {
60+
mListenerList.Remove(pListener);
61+
}
62+
63+
/**
64+
* @brief Sets the connection peer address
65+
*
66+
* @param rAddr Peer address
67+
*/
68+
void SetPeerAddr(const SockAddrAny& rAddr) {
69+
K_ASSERT(rAddr.IsValid());
70+
mPeerAddr = rAddr;
71+
}
72+
73+
private:
74+
//! Maximum datagram packet size from the PC client
75+
static const u32 MAX_PACKET_SIZE = 512;
76+
77+
private:
78+
/**
79+
* @brief Packet receive callback
80+
*
81+
* @param pPacket Incoming packet
82+
* @param pArg Callback user argument
83+
*/
84+
static void PacketCallback(PacketBase* pPacket, void* pArg);
85+
86+
private:
87+
//! Server socket
88+
SyncSocket* mpSocket;
89+
//! Server connection
90+
NetServer* mpConnection;
91+
//! Client socket address
92+
SockAddrAny mPeerAddr;
93+
94+
//! Message parser (owning view)
95+
IMessageParser* mpParser;
96+
//! Registered message listeners
97+
TList<IMessageListener*> mListenerList;
98+
};
99+
100+
//! @}
101+
} // namespace ap
102+
//! @}
103+
} // namespace kiwi
104+
105+
#endif

lib/libkiwi/ap/kiwiIAPMessage.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef LIBKIWI_AP_I_AP_MESSAGE_H
2+
#define LIBKIWI_AP_I_AP_MESSAGE_H
3+
#include <libkiwi/k_types.h>
4+
5+
namespace kiwi {
6+
//! @addtogroup libkiwi_fun
7+
//! @{
8+
namespace ap {
9+
//! @addtogroup libkiwi_fun
10+
//! @{
11+
12+
/**
13+
* @brief Archipelago message interface
14+
*/
15+
class IMessage {
16+
public:
17+
/**
18+
* @brief Destructor
19+
*/
20+
virtual ~IMessage() {}
21+
22+
/**
23+
* @brief Gets the type of this message
24+
*/
25+
virtual u32 GetType() const = 0;
26+
};
27+
28+
//! @}
29+
} // namespace ap
30+
//! @}
31+
} // namespace kiwi
32+
33+
#endif
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef LIBKIWI_AP_I_AP_MESSAGE_LISTENER_H
2+
#define LIBKIWI_AP_I_AP_MESSAGE_LISTENER_H
3+
#include <libkiwi/k_types.h>
4+
5+
namespace kiwi {
6+
//! @addtogroup libkiwi_fun
7+
//! @{
8+
9+
// Forward declarations
10+
class NetConnection;
11+
12+
namespace ap {
13+
//! @addtogroup libkiwi_fun
14+
//! @{
15+
16+
// Forward declarations
17+
class IMessage;
18+
19+
/**
20+
* @brief Archipelago message listener interface
21+
*/
22+
class IMessageListener {
23+
public:
24+
/**
25+
* @brief Destructor
26+
*/
27+
virtual ~IMessageListener() {}
28+
29+
/**
30+
* @brief Network message callback
31+
*
32+
* @param pMessage Network message
33+
* @param pConnection Network connection
34+
*/
35+
virtual void OnMessage(IMessage* pMessage,
36+
kiwi::NetConnection* pConnection) = 0;
37+
};
38+
39+
//! @}
40+
} // namespace ap
41+
//! @}
42+
} // namespace kiwi
43+
44+
#endif
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef LIBKIWI_AP_I_AP_MESSAGE_PARSER_H
2+
#define LIBKIWI_AP_I_AP_MESSAGE_PARSER_H
3+
#include <libkiwi/k_types.h>
4+
5+
namespace kiwi {
6+
//! @addtogroup libkiwi_fun
7+
//! @{
8+
namespace ap {
9+
//! @addtogroup libkiwi_fun
10+
//! @{
11+
12+
// Forward declarations
13+
class IMessage;
14+
15+
/**
16+
* @brief Archipelago message parser interface
17+
*/
18+
class IMessageParser {
19+
public:
20+
/**
21+
* @brief Attempts to parse a network message
22+
*
23+
* @param pData Raw message data
24+
* @param size Message data size
25+
*/
26+
virtual IMessage* Parse(const void* pData, u32 size) = 0;
27+
};
28+
29+
//! @}
30+
} // namespace ap
31+
//! @}
32+
} // namespace kiwi
33+
34+
#endif

lib/libkiwi/libkiwi.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#ifndef LIBKIWI_LIBRARY_H
22
#define LIBKIWI_LIBRARY_H
33

4+
#include <libkiwi/ap/kiwiAPServer.h>
5+
#include <libkiwi/ap/kiwiIAPMessage.h>
6+
#include <libkiwi/ap/kiwiIAPMessageListener.h>
7+
#include <libkiwi/ap/kiwiIAPMessageParser.h>
48
#include <libkiwi/core/kiwiColor.h>
59
#include <libkiwi/core/kiwiConsoleOut.h>
610
#include <libkiwi/core/kiwiController.h>

lib/libkiwi/net/kiwiNetClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace kiwi {
1010
/**
1111
* @brief Network client
1212
*/
13-
class NetClient : public detail::NetConnection {
13+
class NetClient : public NetConnection {
1414
public:
1515
/**
1616
* @brief Constructor

lib/libkiwi/net/kiwiNetConnection.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <libkiwi.h>
22

33
namespace kiwi {
4-
namespace detail {
54

65
/**
76
* @brief Constructor
@@ -19,14 +18,14 @@ NetConnection::NetConnection(SocketBase* pSocket,
1918
K_ASSERT_PTR(mpSocket);
2019
K_ASSERT_PTR(mpPacketFactory);
2120

22-
NetConnectionMgr::GetInstance().AddConnection(this);
21+
detail::NetConnectionMgr::GetInstance().AddConnection(this);
2322
}
2423

2524
/**
2625
* @brief Destructor
2726
*/
2827
NetConnection::~NetConnection() {
29-
NetConnectionMgr::GetInstance().RemoveConnection(this);
28+
detail::NetConnectionMgr::GetInstance().RemoveConnection(this);
3029

3130
// Not owned by the connection
3231
mpSocket = nullptr;
@@ -109,5 +108,4 @@ void NetConnection::Calc() {
109108
}
110109
}
111110

112-
} // namespace detail
113111
} // namespace kiwi

lib/libkiwi/net/kiwiNetConnection.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,17 @@ class PacketBase;
1313
class SockAddrAny;
1414
class SocketBase;
1515

16-
namespace detail {
17-
//! @addtogroup libkiwi_net
18-
//! @{
19-
2016
// Forward declarations
17+
namespace detail {
2118
class NetConnectionMgr;
19+
} // namespace detail
2220

2321
/**
2422
* @brief Network socket connection wrapper
2523
*/
2624
class NetConnection {
2725
// Expose Calc only to the manager
28-
friend class NetConnectionMgr;
26+
friend class detail::NetConnectionMgr;
2927

3028
public:
3129
/**
@@ -129,8 +127,6 @@ class NetConnection {
129127
void Calc();
130128
};
131129

132-
//! @}
133-
} // namespace detail
134130
//! @}
135131
} // namespace kiwi
136132

0 commit comments

Comments
 (0)