Skip to content

Commit 328a5d9

Browse files
committed
Move common AP code to libkiwi
1 parent 518d178 commit 328a5d9

33 files changed

+1010
-494
lines changed

lib/libkiwi/ap/kiwiAPServer.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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), mPort(0), mpConnection(nullptr), mpParser(pParser) {
14+
15+
K_ASSERT_PTR(mpParser);
16+
17+
mpSocket = new 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+
IPacketFactory* pPacketFactory =
23+
new VariablePacketFactory<MAX_PACKET_SIZE>();
24+
K_ASSERT_PTR(pPacketFactory);
25+
26+
mpConnection = new NetServer(mpSocket, pPacketFactory);
27+
K_ASSERT_PTR(mpConnection);
28+
29+
// Bind to any available local address
30+
SockAddr4 addr(port);
31+
bool success = mpConnection->Bind(addr);
32+
ASSERT_EX(success, "Can't bind server socket on port %d", addr.port);
33+
mPort = addr.port;
34+
35+
mpConnection->SetRecvCallback(PacketCallback, this);
36+
37+
// Save server address
38+
mpSocket->GetHostAddr(mServerAddr);
39+
mServerAddr.port = mPort;
40+
}
41+
42+
/**
43+
* @brief Destructor
44+
*/
45+
Server::~Server() {
46+
delete mpConnection;
47+
mpConnection = nullptr;
48+
49+
delete mpSocket;
50+
mpSocket = nullptr;
51+
52+
delete mpParser;
53+
mpParser = nullptr;
54+
}
55+
56+
/**
57+
* @brief Packet receive callback
58+
*
59+
* @param pPacket Incoming packet
60+
* @param pArg Callback user argument
61+
*/
62+
void Server::PacketCallback(PacketBase* pPacket, void* pArg) {
63+
K_ASSERT_PTR(pPacket);
64+
K_ASSERT_PTR(pArg);
65+
66+
// Callback argument is this object
67+
Server* p = static_cast<Server*>(pArg);
68+
K_ASSERT_PTR(p->mpParser);
69+
70+
IMessage* pMessage =
71+
p->mpParser->Parse(pPacket->GetContent(), pPacket->GetContentSize());
72+
73+
if (pMessage == nullptr) {
74+
return;
75+
}
76+
77+
// Notify listeners
78+
K_FOREACH (it, p->mListenerList) {
79+
K_ASSERT_PTR(*it);
80+
(*it)->OnReceiveMessage(pMessage);
81+
}
82+
83+
// Listener responses
84+
K_FOREACH (it, p->mListenerList) {
85+
K_ASSERT_PTR(*it);
86+
(*it)->OnRespondMessage(pMessage, p->mpConnection);
87+
}
88+
89+
// Release message memory
90+
delete pMessage;
91+
}
92+
93+
} // namespace ap
94+
} // namespace kiwi

lib/libkiwi/ap/kiwiAPServer.h

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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/prim/kiwiOptional.h>
7+
#include <libkiwi/support/kiwiLibSO.h>
8+
9+
namespace kiwi {
10+
//! @addtogroup libkiwi_fun
11+
//! @{
12+
13+
// Forward declarations
14+
class NetServer;
15+
class PacketBase;
16+
class SockAddrAny;
17+
class SyncSocket;
18+
19+
namespace ap {
20+
//! @addtogroup libkiwi_fun
21+
//! @{
22+
23+
// Forward declarations
24+
class IMessageListener;
25+
class IMessageParser;
26+
27+
/**
28+
* @brief Archipelago server
29+
*/
30+
class Server {
31+
public:
32+
/**
33+
* @brief Constructor
34+
*
35+
* @param pParser Message parser (owned by this server)
36+
* @param port Local port
37+
*/
38+
Server(IMessageParser* pParser, u16 port = port::AP_COMM);
39+
40+
/**
41+
* @brief Destructor
42+
*/
43+
virtual ~Server();
44+
45+
/**
46+
* @brief Registers a new message listener
47+
*
48+
* @param pListener Message listener
49+
*/
50+
void RegistListener(IMessageListener* pListener) {
51+
K_ASSERT_PTR(pListener);
52+
mListenerList.PushBack(pListener);
53+
}
54+
55+
/**
56+
* @brief Removes a message listener
57+
*
58+
* @param pListener Message listener
59+
*/
60+
void RemoveListener(IMessageListener* pListener) {
61+
mListenerList.Remove(pListener);
62+
}
63+
64+
/**
65+
* @brief Gets the local address of the server
66+
*/
67+
const SockAddr4& GetServerAddr() const {
68+
return mServerAddr;
69+
}
70+
71+
/**
72+
* @brief Gets the connection peer address
73+
*/
74+
const SockAddrAny& GetPeerAddr() const {
75+
return mPeerAddr;
76+
}
77+
78+
/**
79+
* @brief Sets the connection peer address
80+
*
81+
* @param rAddr Peer address
82+
*/
83+
void SetPeerAddr(const SockAddrAny& rAddr) {
84+
K_ASSERT(rAddr.IsValid());
85+
mPeerAddr = rAddr;
86+
}
87+
88+
/**
89+
* @brief Clears the connection peer address
90+
*/
91+
void ResetPeerAddr() {
92+
mPeerAddr = SockAddrAny();
93+
}
94+
95+
private:
96+
//! Maximum datagram packet size from the PC client
97+
static const u32 MAX_PACKET_SIZE = 512;
98+
99+
private:
100+
/**
101+
* @brief Packet receive callback
102+
*
103+
* @param pPacket Incoming packet
104+
* @param pArg Callback user argument
105+
*/
106+
static void PacketCallback(PacketBase* pPacket, void* pArg);
107+
108+
private:
109+
//! Server socket
110+
SyncSocket* mpSocket;
111+
//! Server socket port
112+
u16 mPort;
113+
//! Server connection
114+
NetServer* mpConnection;
115+
116+
//! Server socket address
117+
SockAddr4 mServerAddr;
118+
//! Client socket address
119+
SockAddrAny mPeerAddr;
120+
121+
//! Message parser (owning view)
122+
IMessageParser* mpParser;
123+
//! Registered message listeners
124+
TList<IMessageListener*> mListenerList;
125+
};
126+
127+
//! @}
128+
} // namespace ap
129+
//! @}
130+
} // namespace kiwi
131+
132+
#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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 Callback for receiving a network message
31+
*
32+
* @param pMessage Network message
33+
*/
34+
virtual void OnReceiveMessage(IMessage* pMessage) = 0;
35+
36+
/**
37+
* @brief Callback for responding to a network message
38+
*
39+
* @param pMessage Network message
40+
* @param pConnection Network connection
41+
*/
42+
virtual void OnRespondMessage(IMessage* pMessage,
43+
NetConnection* pConnection) = 0;
44+
};
45+
46+
//! @}
47+
} // namespace ap
48+
//! @}
49+
} // namespace kiwi
50+
51+
#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

0 commit comments

Comments
 (0)