adding command line features for a client to connect to a server

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@13155 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hilnius 2013-07-09 22:05:06 +00:00
parent 54aaa39309
commit 4446f530db
8 changed files with 25 additions and 16 deletions

View File

@ -34,19 +34,23 @@ void* waitInput(void* data)
{ {
std::string str = ""; std::string str = "";
bool stop = false; bool stop = false;
int n = 0;
while(!stop) while(!stop)
{ {
getline(std::cin, str); getline(std::cin, str);
if (str == "connect=")
{
int id = 0;
std::cin >> id;
ProtocolManager::getInstance()->requestStart(new ConnectToServer(id));
}
if (str == "quit") if (str == "quit")
{ {
stop = true; stop = true;
} }
else if (str == "connect")
{
ProtocolManager::getInstance()->requestStart(new ConnectToServer());
}
else if (sscanf(str.c_str(), "connect=%d", &n))
{
ProtocolManager::getInstance()->requestStart(new ConnectToServer(n));
}
} }
exit(0); exit(0);

View File

@ -47,8 +47,12 @@ Event::Event(ENetEvent* event)
{ {
} }
m_packet = NULL;
if (event->packet) if (event->packet)
m_packet = event->packet; m_packet = event->packet;
if (m_packet)
enet_packet_destroy(m_packet); // we got all we need, just remove the data.
std::vector<STKPeer*> peers = NetworkManager::getInstance()->getPeers(); std::vector<STKPeer*> peers = NetworkManager::getInstance()->getPeers();
peer = NULL; peer = NULL;
@ -70,8 +74,6 @@ Event::Event(ENetEvent* event)
Event::~Event() Event::~Event()
{ {
if (m_packet)
enet_packet_destroy(m_packet);
} }
void Event::removeFront(int size) void Event::removeFront(int size)

View File

@ -92,7 +92,7 @@ void NetworkManager::notifyEvent(Event* event)
switch (event->type) switch (event->type)
{ {
case EVENT_TYPE_MESSAGE: case EVENT_TYPE_MESSAGE:
Log::info("NetworkManager", "Message, Sender : %u, message = \"%s\"", event->peer->getAddress(), event->data.c_str()); Log::info("NetworkManager", "Message, Sender : %i.%i.%i.%i, message = \"%s\"", event->peer->getAddress()>>24&0xff, event->peer->getAddress()>>16&0xff, event->peer->getAddress()>>8&0xff, event->peer->getAddress()&0xff, event->data.c_str());
break; break;
case EVENT_TYPE_DISCONNECTED: case EVENT_TYPE_DISCONNECTED:
{ {

View File

@ -25,6 +25,7 @@
#include <assert.h> #include <assert.h>
#include <cstdlib> #include <cstdlib>
#include <errno.h> #include <errno.h>
#include <typeinfo>
void* protocolManagerUpdate(void* data) void* protocolManagerUpdate(void* data)
{ {
@ -175,7 +176,7 @@ void ProtocolManager::requestTerminate(Protocol* protocol)
void ProtocolManager::startProtocol(ProtocolInfo protocol) void ProtocolManager::startProtocol(ProtocolInfo protocol)
{ {
Log::info("ProtocolManager", "A new protocol with id=%u has been started. There are %ld protocols running.", protocol.id, m_protocols.size()+1); Log::info("ProtocolManager", "A %s protocol with id=%u has been started. There are %ld protocols running.", typeid(*protocol.protocol).name(), protocol.id, m_protocols.size()+1);
// add the protocol to the protocol vector so that it's updated // add the protocol to the protocol vector so that it's updated
pthread_mutex_lock(&m_protocols_mutex); pthread_mutex_lock(&m_protocols_mutex);
m_protocols.push_back(protocol); m_protocols.push_back(protocol);
@ -214,6 +215,7 @@ void ProtocolManager::protocolTerminated(ProtocolInfo protocol)
{ {
pthread_mutex_lock(&m_protocols_mutex); // be sure that noone accesses the protocols vector while we erase a protocol pthread_mutex_lock(&m_protocols_mutex); // be sure that noone accesses the protocols vector while we erase a protocol
int offset = 0; int offset = 0;
std::string protocol_type = typeid(*protocol.protocol).name();
for (unsigned int i = 0; i < m_protocols.size(); i++) for (unsigned int i = 0; i < m_protocols.size(); i++)
{ {
if (m_protocols[i-offset].protocol == protocol.protocol) if (m_protocols[i-offset].protocol == protocol.protocol)
@ -223,7 +225,7 @@ void ProtocolManager::protocolTerminated(ProtocolInfo protocol)
offset++; offset++;
} }
} }
Log::info("ProtocolManager", "A protocol has been terminated. There are %ld protocols running.", m_protocols.size()); Log::info("ProtocolManager", "A %s protocol has been terminated. There are %ld protocols running.", protocol_type.c_str(), m_protocols.size());
pthread_mutex_unlock(&m_protocols_mutex); pthread_mutex_unlock(&m_protocols_mutex);
} }

View File

@ -50,7 +50,7 @@ void ConnectToPeer::notifyEvent(Event* event)
{ {
if (event->type == EVENT_TYPE_CONNECTED) if (event->type == EVENT_TYPE_CONNECTED)
{ {
Log::info("ConnectToPeer", "The Connect To Server protocol has \ Log::info("ConnectToPeer", "The Connect To Server protocol has\
received an event notifying that he's connected to the peer."); received an event notifying that he's connected to the peer.");
m_state = CONNECTED; // we received a message, we are connected m_state = CONNECTED; // we received a message, we are connected
} }

View File

@ -312,12 +312,12 @@ void ServerLobbyRoomProtocol::update()
} }
else else
{ {
Log::error("ServerLobbyRoomProtocol", "INSERT SOME ERROR MESSAGE"); Log::error("ServerLobbyRoomProtocol", "Error while reading the list.");
} }
} }
else else
{ {
Log::error("ServerLobbyRoomProtocol", "Cannot retrieve the list"); Log::error("ServerLobbyRoomProtocol", "Cannot retrieve the list.");
} }
} }

View File

@ -38,6 +38,7 @@ void* waitInput2(void* data)
{ {
std::string str = ""; std::string str = "";
bool stop = false; bool stop = false;
int n = 0;
while(!stop) while(!stop)
{ {
getline(std::cin, str); getline(std::cin, str);

View File

@ -58,7 +58,7 @@ bool STKPeer::connectToHost(STKHost* localhost, TransportAddress host, uint32_t
void STKPeer::sendPacket(NetworkString const& data) void STKPeer::sendPacket(NetworkString const& data)
{ {
Log::info("STKPeer", "sending packet of size %d to %i.%i.%i.%i:%i", data.size(), (m_peer->address.host>>24)&0xff,(m_peer->address.host>>16)&0xff,(m_peer->address.host>>8)&0xff,(m_peer->address.host>>0)&0xff,m_peer->address.port); Log::info("STKPeer", "sending packet of size %d to %i.%i.%i.%i:%i", data.size(), (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);
ENetPacket* packet = enet_packet_create(data.c_str(), data.size()+1,ENET_PACKET_FLAG_RELIABLE); ENetPacket* packet = enet_packet_create(data.c_str(), data.size()+1,ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(m_peer, 0, packet); enet_peer_send(m_peer, 0, packet);
@ -66,7 +66,7 @@ void STKPeer::sendPacket(NetworkString const& data)
uint32_t STKPeer::getAddress() const uint32_t STKPeer::getAddress() const
{ {
return m_peer->address.host; return turnEndianness(m_peer->address.host);
} }
uint16_t STKPeer::getPort() const uint16_t STKPeer::getPort() const