the way events pass trough the protocol system is now clean.
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@12918 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
@@ -71,7 +71,7 @@ bool ClientNetworkManager::connectToHost(std::string serverNickname)
|
||||
bool success = false;
|
||||
if (m_localhost->isConnectedTo(addr.ip, addr.port))
|
||||
{
|
||||
success = true;
|
||||
success = true;
|
||||
printf("_NetworkInterface> CONNECTION SUCCES : YOU ARE NOW CONNECTED TO A SERVER.\n");
|
||||
}
|
||||
else
|
||||
|
||||
@@ -41,7 +41,7 @@ Event::Event(ENetEvent* event)
|
||||
}
|
||||
if (peer == NULL)
|
||||
{
|
||||
printf("The peer still does not exist in %i peers\n", peers.size());
|
||||
printf("The peer still does not exist in %u peers\n", peers.size());
|
||||
STKPeer* newPeer = new STKPeer();
|
||||
newPeer->m_peer = event->peer;
|
||||
peer = newPeer;
|
||||
|
||||
@@ -44,10 +44,8 @@ bool NetworkManager::connect(uint32_t ip, uint16_t port)
|
||||
{
|
||||
if (peerExists(ip, port))
|
||||
return isConnectedTo(ip, port);
|
||||
|
||||
bool success = STKPeer::connectToHost(m_localhost, ip, port, 2, 0);
|
||||
|
||||
return success;
|
||||
return STKPeer::connectToHost(m_localhost, ip, port, 2, 0);
|
||||
}
|
||||
|
||||
void NetworkManager::setManualSocketsMode(bool manual)
|
||||
@@ -64,20 +62,30 @@ void NetworkManager::notifyEvent(Event* event)
|
||||
switch (event->type)
|
||||
{
|
||||
case EVENT_TYPE_MESSAGE:
|
||||
printf("Message, Sender : %ld\n", event->peer->getAddress());
|
||||
printf("Message, Sender : %u, message = \"%s\"\n", event->peer->getAddress(), event->data.c_str());
|
||||
break;
|
||||
case EVENT_TYPE_DISCONNECTED:
|
||||
printf("Somebody is now disconnected.\n");
|
||||
printf("Somebody is now disconnected. There are now %lu peers.\n", m_peers.size());
|
||||
printf("Disconnected host: %i.%i.%i.%i:%i\n", event->peer->getAddress()>>24&0xff, event->peer->getAddress()>>16&0xff, event->peer->getAddress()>>8&0xff, event->peer->getAddress()&0xff,event->peer->getPort());
|
||||
// remove the peer:
|
||||
for (unsigned int i = 0; i < m_peers.size(); i++)
|
||||
{
|
||||
if (m_peers[i] == event->peer)
|
||||
{
|
||||
delete m_peers[i];
|
||||
m_peers.erase(m_peers.begin()+i, m_peers.begin()+i+1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
printf("ERROR : the peer that has been disconnected was not registered by the Network Manager.\n");
|
||||
break;
|
||||
case EVENT_TYPE_CONNECTED:
|
||||
printf("A client has just connected.\n");
|
||||
printf("A client has just connected. There are now %lu peers.\n", m_peers.size() + 1);
|
||||
// create the new peer:
|
||||
m_peers.push_back(event->peer);
|
||||
break;
|
||||
}
|
||||
ProtocolManager::getInstance()->notifyEvent(event);
|
||||
delete event; // event won't be use again
|
||||
}
|
||||
|
||||
void NetworkManager::setLogin(std::string username, std::string password)
|
||||
|
||||
@@ -8,18 +8,18 @@
|
||||
|
||||
enum PROTOCOL_TYPE
|
||||
{
|
||||
PROTOCOL_NOT_CONCERNED = 0,
|
||||
PROTOCOL_CONNECTION = 1
|
||||
|
||||
PROTOCOL_NONE = 0,
|
||||
PROTOCOL_CONNECTION = 1,
|
||||
PROTOCOL_SILENT = 0xffff // used for protocols that do not subscribe to any network event.
|
||||
};
|
||||
|
||||
class Protocol
|
||||
{
|
||||
public:
|
||||
Protocol(CallbackObject* callbackObject, PROTOCOL_TYPE type = PROTOCOL_NOT_CONCERNED);
|
||||
Protocol(CallbackObject* callbackObject, PROTOCOL_TYPE type);
|
||||
virtual ~Protocol();
|
||||
|
||||
virtual void messageReceived(uint8_t* data) = 0;
|
||||
virtual void notifyEvent(Event* event) = 0;
|
||||
|
||||
void setListener(ProtocolManager* listener);
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
ProtocolManager::ProtocolManager()
|
||||
{
|
||||
pthread_mutex_init(&m_messagesMutex, NULL);
|
||||
pthread_mutex_init(&m_eventsMutex, NULL);
|
||||
pthread_mutex_init(&m_protocolsMutex, NULL);
|
||||
}
|
||||
|
||||
@@ -20,12 +20,9 @@ ProtocolManager::~ProtocolManager()
|
||||
|
||||
void ProtocolManager::notifyEvent(Event* event)
|
||||
{
|
||||
if (event->type == EVENT_TYPE_MESSAGE)
|
||||
{
|
||||
pthread_mutex_lock(&m_messagesMutex);
|
||||
m_messagesToProcess.push_back(event->data);
|
||||
pthread_mutex_unlock(&m_messagesMutex);
|
||||
}
|
||||
pthread_mutex_lock(&m_eventsMutex);
|
||||
m_eventsToProcess.push_back(event);
|
||||
pthread_mutex_unlock(&m_eventsMutex);
|
||||
}
|
||||
|
||||
void ProtocolManager::sendMessage(std::string message)
|
||||
@@ -83,38 +80,42 @@ void ProtocolManager::unpauseProtocol(Protocol* protocol)
|
||||
}
|
||||
void ProtocolManager::protocolTerminated(Protocol* protocol)
|
||||
{
|
||||
pthread_mutex_lock(&m_protocolsMutex);
|
||||
int offset = 0;
|
||||
for (unsigned int i = 0; i < m_protocols.size(); i++)
|
||||
{
|
||||
if (m_protocols[i-offset].protocol == protocol)
|
||||
{
|
||||
pthread_mutex_lock(&m_protocolsMutex);
|
||||
delete m_protocols[i].protocol;
|
||||
m_protocols.erase(m_protocols.begin()+(i-offset), m_protocols.begin()+(i-offset)+1);
|
||||
pthread_mutex_unlock(&m_protocolsMutex);
|
||||
offset++;
|
||||
}
|
||||
}
|
||||
printf("__ProtocolManager> A protocol has been terminated. There are %ld protocols running.\n", m_protocols.size());
|
||||
pthread_mutex_unlock(&m_protocolsMutex);
|
||||
}
|
||||
|
||||
void ProtocolManager::update()
|
||||
{
|
||||
// before updating, notice protocols that they have received information
|
||||
pthread_mutex_lock(&m_messagesMutex); // secure threads
|
||||
int size = m_messagesToProcess.size();
|
||||
pthread_mutex_lock(&m_eventsMutex); // secure threads
|
||||
int size = m_eventsToProcess.size();
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
std::string data = m_messagesToProcess.back();
|
||||
PROTOCOL_TYPE searchedProtocol = (PROTOCOL_TYPE)(data[0]);
|
||||
Event* event = m_eventsToProcess.back();
|
||||
|
||||
PROTOCOL_TYPE searchedProtocol = PROTOCOL_NONE;
|
||||
if (event->data.size() > 0)
|
||||
searchedProtocol = (PROTOCOL_TYPE)(event->data[0]);
|
||||
for (unsigned int i = 0; i < m_protocols.size() ; i++)
|
||||
{
|
||||
if (m_protocols[i].protocol->getProtocolType() == searchedProtocol) // pass data to them even when paused
|
||||
m_protocols[i].protocol->messageReceived((uint8_t*)(&data[1]));
|
||||
if (m_protocols[i].protocol->getProtocolType() == searchedProtocol || event->type != EVENT_TYPE_MESSAGE) // pass data to protocols even when paused
|
||||
m_protocols[i].protocol->notifyEvent(event);
|
||||
}
|
||||
m_messagesToProcess.pop_back();
|
||||
delete event;
|
||||
m_eventsToProcess.pop_back();
|
||||
}
|
||||
pthread_mutex_unlock(&m_messagesMutex); // release the mutex
|
||||
pthread_mutex_unlock(&m_eventsMutex); // release the mutex
|
||||
// now update all protocols
|
||||
for (unsigned int i = 0; i < m_protocols.size(); i++)
|
||||
{
|
||||
@@ -151,10 +152,8 @@ void ProtocolManager::assignProtocolId(ProtocolInfo& protocolInfo)
|
||||
exists = false;
|
||||
for (unsigned int i = 0; i < m_protocols.size(); i++)
|
||||
{
|
||||
pthread_mutex_lock(&m_protocolsMutex);
|
||||
if (m_protocols[i].id == newId)
|
||||
exists = true;
|
||||
pthread_mutex_unlock(&m_protocolsMutex);
|
||||
}
|
||||
} while (exists);
|
||||
protocolInfo.id = newId;
|
||||
|
||||
@@ -49,10 +49,10 @@ class ProtocolManager : public Singleton<ProtocolManager>
|
||||
|
||||
// protected members
|
||||
std::vector<ProtocolInfo> m_protocols;
|
||||
std::vector<std::string> m_messagesToProcess;
|
||||
std::vector<Event*> m_eventsToProcess;
|
||||
|
||||
// mutexes:
|
||||
pthread_mutex_t m_messagesMutex;
|
||||
pthread_mutex_t m_eventsMutex;
|
||||
pthread_mutex_t m_protocolsMutex;
|
||||
};
|
||||
|
||||
|
||||
@@ -6,31 +6,34 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
ConnectToServer::ConnectToServer(CallbackObject* callbackObject) : Protocol(callbackObject)
|
||||
ConnectToServer::ConnectToServer(CallbackObject* callbackObject) : Protocol(callbackObject, PROTOCOL_CONNECTION)
|
||||
{
|
||||
m_serverIp = 0;
|
||||
m_serverPort = 0;
|
||||
m_state = NONE;
|
||||
}
|
||||
|
||||
ConnectToServer::~ConnectToServer()
|
||||
{
|
||||
}
|
||||
|
||||
void ConnectToServer::messageReceived(uint8_t* data)
|
||||
void ConnectToServer::notifyEvent(Event* event)
|
||||
{
|
||||
printf("data received\n");
|
||||
printf("%s", data);
|
||||
m_state = NONE; // we received a message, we are connected
|
||||
if (event->type == EVENT_TYPE_CONNECTED && event->peer->getAddress() == m_serverIp && event->peer->getPort() == m_serverPort)
|
||||
{
|
||||
printf("The Connect To Server protocol has received an event notifying that he's connected to the peer. The peer sent \"%s\"\n", event->data.c_str());
|
||||
m_state = DONE; // we received a message, we are connected
|
||||
}
|
||||
}
|
||||
|
||||
void ConnectToServer::setup()
|
||||
{
|
||||
m_state = NONE;
|
||||
if (m_serverIp == 0 || m_serverPort == 0 )
|
||||
{
|
||||
printf("You have to set the server's public ip:port of the server.\n");
|
||||
m_listener->protocolTerminated(this);
|
||||
}
|
||||
m_state = NONE;
|
||||
}
|
||||
|
||||
void ConnectToServer::update()
|
||||
@@ -43,11 +46,9 @@ void ConnectToServer::update()
|
||||
currentTime += 3600;
|
||||
if (currentTime > target)
|
||||
{
|
||||
printf("DOING AN UPDATE BECAUSE IT IS TIME TO DO IT.\n");
|
||||
NetworkManager::getInstance()->connect(m_serverIp, m_serverPort);
|
||||
if (NetworkManager::getInstance()->isConnectedTo(m_serverIp, m_serverPort))
|
||||
{
|
||||
printf("NetworkManager sayon THAT YOU ARE CONNECTED, BIATCHHH\n");
|
||||
m_state = DONE;
|
||||
return;
|
||||
}
|
||||
@@ -57,7 +58,6 @@ void ConnectToServer::update()
|
||||
}
|
||||
else if (m_state == DONE)
|
||||
{
|
||||
printf("STATE IS KNOWN AS DONE\n");
|
||||
m_listener->protocolTerminated(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ class ConnectToServer : public Protocol, public CallbackObject
|
||||
ConnectToServer(CallbackObject* callbackObject);
|
||||
virtual ~ConnectToServer();
|
||||
|
||||
virtual void messageReceived(uint8_t* data);
|
||||
virtual void notifyEvent(Event* event);
|
||||
virtual void setup();
|
||||
virtual void update();
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
GetPeerAddress::GetPeerAddress(CallbackObject* callbackObject) : Protocol(callbackObject)
|
||||
GetPeerAddress::GetPeerAddress(CallbackObject* callbackObject) : Protocol(callbackObject, PROTOCOL_SILENT)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ GetPeerAddress::~GetPeerAddress()
|
||||
{
|
||||
}
|
||||
|
||||
void GetPeerAddress::messageReceived(uint8_t* data)
|
||||
void GetPeerAddress::notifyEvent(Event* event)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ class GetPeerAddress : public Protocol
|
||||
GetPeerAddress(CallbackObject* callbackObject);
|
||||
virtual ~GetPeerAddress();
|
||||
|
||||
virtual void messageReceived(uint8_t* data);
|
||||
virtual void notifyEvent(Event* event);
|
||||
virtual void setup();
|
||||
virtual void update();
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ int stunRand()
|
||||
return rand();
|
||||
}
|
||||
|
||||
GetPublicAddress::GetPublicAddress(CallbackObject* callbackObject) : Protocol(callbackObject)
|
||||
GetPublicAddress::GetPublicAddress(CallbackObject* callbackObject) : Protocol(callbackObject, PROTOCOL_SILENT)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ GetPublicAddress::~GetPublicAddress()
|
||||
{
|
||||
}
|
||||
|
||||
void GetPublicAddress::messageReceived(uint8_t* data)
|
||||
void GetPublicAddress::notifyEvent(Event* event)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ class GetPublicAddress : public Protocol
|
||||
GetPublicAddress(CallbackObject* callbackObject);
|
||||
virtual ~GetPublicAddress();
|
||||
|
||||
virtual void messageReceived(uint8_t* data);
|
||||
virtual void notifyEvent(Event* event);
|
||||
|
||||
virtual void setup();
|
||||
virtual void update();
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
HidePublicAddress::HidePublicAddress(CallbackObject* callbackObject) : Protocol(callbackObject)
|
||||
HidePublicAddress::HidePublicAddress(CallbackObject* callbackObject) : Protocol(callbackObject, PROTOCOL_SILENT)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ HidePublicAddress::~HidePublicAddress()
|
||||
{
|
||||
}
|
||||
|
||||
void HidePublicAddress::messageReceived(uint8_t* data)
|
||||
void HidePublicAddress::notifyEvent(Event* event)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ class HidePublicAddress : public Protocol
|
||||
HidePublicAddress(CallbackObject* callbackObject);
|
||||
virtual ~HidePublicAddress();
|
||||
|
||||
virtual void messageReceived(uint8_t* data);
|
||||
virtual void notifyEvent(Event* event);
|
||||
virtual void setup();
|
||||
virtual void update();
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
ShowPublicAddress::ShowPublicAddress(CallbackObject* callbackObject) : Protocol(callbackObject)
|
||||
ShowPublicAddress::ShowPublicAddress(CallbackObject* callbackObject) : Protocol(callbackObject, PROTOCOL_SILENT)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ ShowPublicAddress::~ShowPublicAddress()
|
||||
{
|
||||
}
|
||||
|
||||
void ShowPublicAddress::messageReceived(uint8_t* data)
|
||||
void ShowPublicAddress::notifyEvent(Event* event)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ class ShowPublicAddress : public Protocol
|
||||
ShowPublicAddress(CallbackObject* callbackObject);
|
||||
virtual ~ShowPublicAddress();
|
||||
|
||||
virtual void messageReceived(uint8_t* data);
|
||||
virtual void notifyEvent(Event* event);
|
||||
virtual void setup();
|
||||
virtual void update();
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@ void* STKHost::receive_data(void* self)
|
||||
while (enet_host_service(host, &event, 0) != 0) {
|
||||
Event* evt = new Event(&event);
|
||||
NetworkManager::getInstance()->notifyEvent(evt);
|
||||
//NetworkManager::getInstance()->packetReceived((char*)(event.data));
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
|
||||
@@ -10,6 +10,8 @@ STKPeer::STKPeer()
|
||||
|
||||
STKPeer::~STKPeer()
|
||||
{
|
||||
if (m_peer)
|
||||
delete m_peer;
|
||||
}
|
||||
|
||||
bool STKPeer::connectToHost(STKHost* host, uint32_t ip, uint16_t port, uint32_t channelCount, uint32_t data)
|
||||
|
||||
Reference in New Issue
Block a user