cleaning the way events come out from Enet, improving peer management (removing double peers)
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@12916 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
@@ -40,6 +40,8 @@
|
||||
</Linker>
|
||||
<Unit filename="client_network_manager.cpp" />
|
||||
<Unit filename="client_network_manager.hpp" />
|
||||
<Unit filename="event.cpp" />
|
||||
<Unit filename="event.hpp" />
|
||||
<Unit filename="http_functions.cpp" />
|
||||
<Unit filename="http_functions.hpp" />
|
||||
<Unit filename="main.cpp" />
|
||||
@@ -47,6 +49,8 @@
|
||||
<Unit filename="network_interface.hpp" />
|
||||
<Unit filename="network_manager.cpp" />
|
||||
<Unit filename="network_manager.hpp" />
|
||||
<Unit filename="packet_logger.cpp" />
|
||||
<Unit filename="packet_logger.hpp" />
|
||||
<Unit filename="protocol.cpp" />
|
||||
<Unit filename="protocol.hpp" />
|
||||
<Unit filename="protocol_manager.cpp" />
|
||||
|
||||
@@ -16,7 +16,7 @@ ClientNetworkManager::~ClientNetworkManager()
|
||||
{
|
||||
}
|
||||
|
||||
void ClientNetworkManager::run()
|
||||
void ClientNetworkManager::run()
|
||||
{
|
||||
if (enet_initialize() != 0)
|
||||
{
|
||||
@@ -78,7 +78,6 @@ bool ClientNetworkManager::connectToHost(std::string serverNickname)
|
||||
{
|
||||
printf("_NetworkInterface> We are NOT connected to the server.\n");
|
||||
}
|
||||
|
||||
// step 5 : hide our public address
|
||||
HidePublicAddress* hpa = new HidePublicAddress(NULL);
|
||||
hpa->setPassword(m_playerLogin.password);
|
||||
@@ -88,6 +87,7 @@ bool ClientNetworkManager::connectToHost(std::string serverNickname)
|
||||
{
|
||||
}
|
||||
printf("_NetworkInterface> The public address is now hidden online.\n");
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
55
dev/SocketsBase/event.cpp
Normal file
55
dev/SocketsBase/event.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "event.hpp"
|
||||
|
||||
#include "network_manager.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <stdio.h>
|
||||
|
||||
Event::Event(ENetEvent* event)
|
||||
{
|
||||
switch (event->type)
|
||||
{
|
||||
case ENET_EVENT_TYPE_CONNECT:
|
||||
type = EVENT_TYPE_CONNECTED;
|
||||
break;
|
||||
case ENET_EVENT_TYPE_DISCONNECT:
|
||||
type = EVENT_TYPE_DISCONNECTED;
|
||||
break;
|
||||
case ENET_EVENT_TYPE_RECEIVE:
|
||||
type = EVENT_TYPE_MESSAGE;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (type == EVENT_TYPE_MESSAGE)
|
||||
data = std::string((char*)(event->packet->data));
|
||||
else if (event->data)
|
||||
data = std::string((char*)(event->data));
|
||||
|
||||
if (event->packet)
|
||||
m_packet = event->packet;
|
||||
|
||||
std::vector<STKPeer*> peers = NetworkManager::getInstance()->getPeers();
|
||||
peer = NULL;
|
||||
for (unsigned int i = 0; i < peers.size(); i++)
|
||||
{
|
||||
if (*peers[i] == event->peer)
|
||||
{
|
||||
peer = peers[i];
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (peer == NULL)
|
||||
{
|
||||
printf("The peer still does not exist in %i peers\n", peers.size());
|
||||
STKPeer* newPeer = new STKPeer();
|
||||
newPeer->m_peer = event->peer;
|
||||
peer = newPeer;
|
||||
}
|
||||
}
|
||||
|
||||
Event::~Event()
|
||||
{
|
||||
if (m_packet)
|
||||
enet_packet_destroy(m_packet);
|
||||
}
|
||||
29
dev/SocketsBase/event.hpp
Normal file
29
dev/SocketsBase/event.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef EVENT_HPP
|
||||
#define EVENT_HPP
|
||||
|
||||
#include "stk_peer.hpp"
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
|
||||
enum EVENT_TYPE
|
||||
{
|
||||
EVENT_TYPE_CONNECTED,
|
||||
EVENT_TYPE_DISCONNECTED,
|
||||
EVENT_TYPE_MESSAGE
|
||||
};
|
||||
|
||||
class Event
|
||||
{
|
||||
public:
|
||||
Event(ENetEvent* event);
|
||||
~Event();
|
||||
|
||||
EVENT_TYPE type;
|
||||
std::string data;
|
||||
STKPeer* peer;
|
||||
|
||||
private:
|
||||
ENetPacket* m_packet;
|
||||
};
|
||||
|
||||
#endif // EVENT_HPP
|
||||
@@ -45,10 +45,8 @@ bool NetworkManager::connect(uint32_t ip, uint16_t port)
|
||||
if (peerExists(ip, port))
|
||||
return isConnectedTo(ip, port);
|
||||
|
||||
STKPeer* peer = new STKPeer();
|
||||
bool success = peer->connectToHost(m_localhost, ip, port, 2, 0);
|
||||
if (success)
|
||||
m_peers.push_back(peer);
|
||||
bool success = STKPeer::connectToHost(m_localhost, ip, port, 2, 0);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -74,6 +72,8 @@ void NetworkManager::notifyEvent(Event* event)
|
||||
break;
|
||||
case EVENT_TYPE_CONNECTED:
|
||||
printf("A client has just connected.\n");
|
||||
// create the new peer:
|
||||
m_peers.push_back(event->peer);
|
||||
break;
|
||||
}
|
||||
ProtocolManager::getInstance()->notifyEvent(event);
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
|
||||
ProtocolManager::ProtocolManager()
|
||||
{
|
||||
m_messagesMutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
m_protocolsMutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_mutex_init(&m_messagesMutex, NULL);
|
||||
pthread_mutex_init(&m_protocolsMutex, NULL);
|
||||
}
|
||||
|
||||
ProtocolManager::~ProtocolManager()
|
||||
@@ -36,18 +36,18 @@ void ProtocolManager::sendMessage(std::string message)
|
||||
|
||||
int ProtocolManager::startProtocol(Protocol* protocol)
|
||||
{
|
||||
|
||||
ProtocolInfo protocolInfo;
|
||||
protocolInfo.state = PROTOCOL_STATE_RUNNING;
|
||||
assignProtocolId(protocolInfo);
|
||||
protocolInfo.protocol = protocol;
|
||||
printf("__ProtocolManager> A new protocol with id=%u has been started. There are %ld protocols running.\n", protocolInfo.id, m_protocols.size()+1);
|
||||
|
||||
pthread_mutex_lock(&m_protocolsMutex);
|
||||
m_protocols.push_back(protocolInfo);
|
||||
pthread_mutex_unlock(&m_protocolsMutex);
|
||||
|
||||
protocol->setListener(this);
|
||||
protocol->setup();
|
||||
printf("__ProtocolManager> A new protocol with id=%ud been started. There are %ld protocols running.\n", protocolInfo.id, m_protocols.size());
|
||||
|
||||
|
||||
return protocolInfo.id;
|
||||
}
|
||||
@@ -91,8 +91,8 @@ void ProtocolManager::protocolTerminated(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);
|
||||
offset++;
|
||||
pthread_mutex_unlock(&m_protocolsMutex);
|
||||
offset++;
|
||||
}
|
||||
}
|
||||
printf("__ProtocolManager> A protocol has been terminated. There are %ld protocols running.\n", m_protocols.size());
|
||||
|
||||
@@ -43,9 +43,11 @@ 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;
|
||||
}
|
||||
@@ -55,6 +57,7 @@ void ConnectToServer::update()
|
||||
}
|
||||
else if (m_state == DONE)
|
||||
{
|
||||
printf("STATE IS KNOWN AS DONE\n");
|
||||
m_listener->protocolTerminated(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ 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;
|
||||
|
||||
@@ -18,13 +18,13 @@ bool STKPeer::connectToHost(STKHost* host, uint32_t ip, uint16_t port, uint32_t
|
||||
address.host = ip;
|
||||
address.port = port;
|
||||
|
||||
m_peer = enet_host_connect(host->m_host, &address, 2, 0);
|
||||
if (m_peer == NULL)
|
||||
ENetPeer* peer = enet_host_connect(host->m_host, &address, 2, 0);
|
||||
if (peer == NULL)
|
||||
{
|
||||
printf("Could not try to connect to server.\n");
|
||||
return false;
|
||||
}
|
||||
printf("Connecting to %i.%i.%i.%i:%i.\n", (m_peer->address.host>>0)&0xff,(m_peer->address.host>>8)&0xff,(m_peer->address.host>>16)&0xff,(m_peer->address.host>>24)&0xff,m_peer->address.port);
|
||||
printf("Connecting to %i.%i.%i.%i:%i.\n", (peer->address.host>>0)&0xff,(peer->address.host>>8)&0xff,(peer->address.host>>16)&0xff,(peer->address.host>>24)&0xff,peer->address.port);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
class STKPeer
|
||||
{
|
||||
friend class Event;
|
||||
public:
|
||||
STKPeer();
|
||||
virtual ~STKPeer();
|
||||
@@ -15,7 +16,7 @@ class STKPeer
|
||||
|
||||
virtual void sendPacket(char* data);
|
||||
|
||||
bool connectToHost(STKHost* host, uint32_t ip, uint16_t port, uint32_t channelCount, uint32_t data);
|
||||
static bool connectToHost(STKHost* host, uint32_t ip, uint16_t port, uint32_t channelCount, uint32_t data);
|
||||
|
||||
bool isConnected();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user