Removed polling for GetPublicAddress with callback.

This commit is contained in:
hiker 2015-10-29 18:58:18 +11:00
parent ef310a5666
commit dd289dc1cd
4 changed files with 52 additions and 31 deletions

View File

@ -44,7 +44,8 @@
// make the linker happy
const uint32_t GetPublicAddress::m_stun_magic_cookie = 0x2112A442;
GetPublicAddress::GetPublicAddress() : Protocol(PROTOCOL_SILENT)
GetPublicAddress::GetPublicAddress(CallbackObject *callback)
: Protocol(PROTOCOL_SILENT, callback)
{
m_state = NOTHING_DONE;
} // GetPublicAddress
@ -83,6 +84,8 @@ void GetPublicAddress::createStunRequest()
assert(res != NULL);
struct sockaddr_in* current_interface = (struct sockaddr_in*)(res->ai_addr);
m_stun_server_ip = ntohl(current_interface->sin_addr.s_addr);
// Create a new socket for the stun server.
m_transaction_host = new Network(1, 1, 0, 0);
// Assemble the message for the stun server
@ -204,6 +207,7 @@ void GetPublicAddress::asynchronousUpdate()
if (m_state == STUN_REQUEST_SENT)
{
std::string message = parseStunResponse();
delete m_transaction_host;
if (message != "")
{
Log::warn("GetPublicAddress", "%s", message.c_str());

View File

@ -28,7 +28,7 @@ class Network;
class GetPublicAddress : public Protocol
{
public:
GetPublicAddress();
GetPublicAddress(CallbackObject *callback = NULL);
virtual ~GetPublicAddress() {}
virtual bool notifyEvent(Event* event) { return true; }
@ -45,13 +45,13 @@ class GetPublicAddress : public Protocol
static const uint32_t m_stun_magic_cookie;
static const int m_stun_server_port = 3478;
enum STATE
enum State
{
NOTHING_DONE,
STUN_REQUEST_SENT,
EXITING
};
STATE m_state;
} m_state;
uint8_t m_stun_tansaction_id[12];
uint32_t m_stun_server_ip;
Network* m_transaction_host;

View File

@ -116,21 +116,24 @@ void ServerLobbyRoomProtocol::update()
{
case NONE:
// Start the protocol to find the public ip address.
m_current_protocol = new GetPublicAddress();
m_current_protocol = new GetPublicAddress(this);
m_current_protocol->requestStart();
m_state = GETTING_PUBLIC_ADDRESS;
// The callback from GetPublicAddress will wake this protocl up
ProtocolManager::getInstance()->pauseProtocol(this);
break;
case GETTING_PUBLIC_ADDRESS:
if (m_current_protocol->getState() == PROTOCOL_STATE_TERMINATED)
{
Log::debug("ServerLobbyRoomProtocol", "Public address known.");
// Free GetPublicAddress protocol
delete m_current_protocol;
// Register this server with the STK server. The callback in the
// xml request will update the state to WORKING.
// Register this server with the STK server. This will block
// this thread, but there is no need for the protocol manager
// to react to any requests before the server is registered.
registerServer();
Log::info("ServerLobbyRoomProtocol", "Server registered.");
m_state = WORKING;
}
break;
case WORKING:
@ -153,6 +156,15 @@ void ServerLobbyRoomProtocol::update()
}
} // update
//-----------------------------------------------------------------------------
/** Callback when the GetPublicAddress terminates. It will unpause this
* protocol, which triggers the next state of the finite state machine.
*/
void ServerLobbyRoomProtocol::callback(Protocol *protocol)
{
ProtocolManager::getInstance()->unpauseProtocol(this);
} // callback
//-----------------------------------------------------------------------------
/** Register this server (i.e. its public address) with the STK server
* so that clients can find it. It blocks till a responsce from the

View File

@ -2,17 +2,29 @@
#define SERVER_LOBBY_ROOM_PROTOCOL_HPP
#include "network/protocols/lobby_room_protocol.hpp"
#include "utils/cpp2011.hpp"
class ServerLobbyRoomProtocol : public LobbyRoomProtocol
, public CallbackObject
{
private:
uint8_t m_next_id; //!< Next id to assign to a peer.
/* The state for a small finite state machine. */
enum
{
NONE,
GETTING_PUBLIC_ADDRESS,
WORKING,
DONE,
EXITING
} m_state;
/** Next id to assign to a peer. */
uint8_t m_next_id;
Protocol *m_current_protocol;
bool m_selection_enabled;
bool m_in_race;
// connection management
void kartDisconnected(Event* event);
void connectionRequested(Event* event);
@ -27,29 +39,22 @@ private:
void playerLapsVote(Event* event);
void registerServer();
enum STATE
{
NONE,
GETTING_PUBLIC_ADDRESS,
WORKING,
DONE,
EXITING
};
STATE m_state;
public:
ServerLobbyRoomProtocol();
virtual ~ServerLobbyRoomProtocol();
ServerLobbyRoomProtocol();
virtual ~ServerLobbyRoomProtocol();
virtual bool notifyEventAsynchronous(Event* event);
virtual void setup();
virtual void update();
virtual void asynchronousUpdate() {};
virtual bool notifyEventAsynchronous(Event* event);
virtual void setup();
virtual void update();
virtual void asynchronousUpdate() {};
void startGame();
void startSelection();
void checkIncomingConnectionRequests();
void checkRaceFinished();
void startGame();
void startSelection();
void checkIncomingConnectionRequests();
void checkRaceFinished();
virtual void callback(Protocol *protocol) OVERRIDE;
}; // class ServerLobbyRoomProtocol
};
#endif // SERVER_LOBBY_ROOM_PROTOCOL_HPP