Working mutliple-user connection trough enet, using STUN protocol. Needs to be tested on WAN to be sure it works.
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@12888 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
#include "callback_object.hpp"
|
||||
|
||||
CallbackObject::CallbackObject()
|
||||
{
|
||||
}
|
||||
|
||||
CallbackObject::~CallbackObject()
|
||||
{
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
#ifndef CALLBACK_OBJECT_HPP
|
||||
#define CALLBACK_OBJECT_HPP
|
||||
|
||||
|
||||
class CallbackObject
|
||||
{
|
||||
public:
|
||||
CallbackObject();
|
||||
virtual ~CallbackObject();
|
||||
|
||||
protected:
|
||||
};
|
||||
|
||||
#endif // CALLBACK_OBJECT_HPP
|
||||
@@ -1,5 +1,11 @@
|
||||
#include "client_network_manager.hpp"
|
||||
|
||||
#include "protocols/get_public_address.hpp"
|
||||
#include "protocols/hide_public_address.hpp"
|
||||
#include "protocols/show_public_address.hpp"
|
||||
#include "protocols/get_peer_address.hpp"
|
||||
#include "protocols/connect_to_server.hpp"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
ClientNetworkManager::ClientNetworkManager()
|
||||
@@ -20,14 +26,78 @@ void ClientNetworkManager::run()
|
||||
m_localhost = new STKHost();
|
||||
m_localhost->setupClient(1, 2, 0, 0);
|
||||
m_localhost->startListening();
|
||||
|
||||
NetworkManager::run();
|
||||
}
|
||||
|
||||
void ClientNetworkManager::connect(uint32_t ip, uint16_t port)
|
||||
bool ClientNetworkManager::connect(uint32_t ip, uint16_t port)
|
||||
{
|
||||
STKPeer* peer = new STKPeer();
|
||||
peer->connectToServer(m_localhost, ip, port, 2, 0);
|
||||
bool success = peer->connectToServer(m_localhost, ip, port, 2, 0);
|
||||
if (success)
|
||||
m_peers.push_back(peer);
|
||||
return success;
|
||||
}
|
||||
|
||||
bool ClientNetworkManager::connectToHost(std::string serverNickname)
|
||||
{
|
||||
printf("_NetworkInterface>Starting the connection to host protocol\n");
|
||||
// step 1 : retreive public address
|
||||
int id = ProtocolManager::getInstance()->startProtocol(new GetPublicAddress(&m_publicAddress));
|
||||
while (ProtocolManager::getInstance()->getProtocolState(id) != PROTOCOL_STATE_TERMINATED )
|
||||
{
|
||||
}
|
||||
printf("_NetworkInterface> The public address is known.\n");
|
||||
|
||||
m_peers.push_back(peer);
|
||||
// step 2 : show the public address for others (here, the server)
|
||||
ShowPublicAddress* spa = new ShowPublicAddress(NULL);
|
||||
spa->setPassword(m_playerLogin.password);
|
||||
spa->setUsername(m_playerLogin.username);
|
||||
spa->setPublicAddress(m_publicAddress.ip, m_publicAddress.port);
|
||||
id = ProtocolManager::getInstance()->startProtocol(spa);
|
||||
while (ProtocolManager::getInstance()->getProtocolState(id) != PROTOCOL_STATE_TERMINATED )
|
||||
{
|
||||
}
|
||||
printf("_NetworkInterface> The public address is being shown online.\n");
|
||||
|
||||
// step 3 : get the server's addres.
|
||||
TransportAddress addr;
|
||||
GetPeerAddress* gpa = new GetPeerAddress(&addr);
|
||||
gpa->setPeerName(serverNickname);
|
||||
id = ProtocolManager::getInstance()->startProtocol(gpa);
|
||||
while (ProtocolManager::getInstance()->getProtocolState(id) != PROTOCOL_STATE_TERMINATED )
|
||||
{
|
||||
}
|
||||
printf("_NetworkInterface> The public address of the server is known.\n");
|
||||
|
||||
// step 4 : connect to the server
|
||||
ConnectToServer* cts = new ConnectToServer(NULL);
|
||||
cts->setServerAddress(addr.ip, addr.port);
|
||||
id = ProtocolManager::getInstance()->startProtocol(cts);
|
||||
while (ProtocolManager::getInstance()->getProtocolState(id) != PROTOCOL_STATE_TERMINATED )
|
||||
{
|
||||
}
|
||||
bool success = false;
|
||||
if (m_peers[0]->isConnected())
|
||||
{
|
||||
success = true;
|
||||
printf("_NetworkInterface> CONNECTION SUCCES : YOU ARE NOW CONNECTED TO A SERVER.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
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);
|
||||
hpa->setUsername(m_playerLogin.username);
|
||||
id = ProtocolManager::getInstance()->startProtocol(hpa);
|
||||
while (ProtocolManager::getInstance()->getProtocolState(id) != PROTOCOL_STATE_TERMINATED )
|
||||
{
|
||||
}
|
||||
printf("_NetworkInterface> The public address is now hidden online.\n");
|
||||
return success;
|
||||
}
|
||||
|
||||
void ClientNetworkManager::packetReceived(char* data)
|
||||
@@ -41,3 +111,8 @@ void ClientNetworkManager::sendPacket(char* data)
|
||||
printf("Ambiguous send of data\n");
|
||||
m_peers[0]->sendPacket(data);
|
||||
}
|
||||
|
||||
STKPeer* ClientNetworkManager::getPeer()
|
||||
{
|
||||
return m_peers[0];
|
||||
}
|
||||
|
||||
@@ -15,11 +15,14 @@ class ClientNetworkManager : public NetworkManager
|
||||
|
||||
virtual void run();
|
||||
|
||||
void connect(uint32_t ip, uint16_t port);
|
||||
bool connect(uint32_t ip, uint16_t port);
|
||||
bool connectToHost(std::string serverNickname);
|
||||
|
||||
virtual void packetReceived(char* data);
|
||||
virtual void sendPacket(char* data);
|
||||
|
||||
STKPeer* getPeer();
|
||||
|
||||
protected:
|
||||
ClientNetworkManager();
|
||||
virtual ~ClientNetworkManager();
|
||||
|
||||
@@ -26,50 +26,41 @@ int main()
|
||||
cin >> answer;
|
||||
if (answer == "client")
|
||||
{
|
||||
ClientNetworkManager::getInstance();
|
||||
NetworkManager::getInstance()->packetReceived("test");
|
||||
/// NICKNAME :
|
||||
ClientNetworkManager::getInstance()->run();
|
||||
|
||||
std::string nickname;
|
||||
cout << "Nickname=";
|
||||
std::cin >> nickname;
|
||||
/// PASSWORD :
|
||||
std::string password;
|
||||
cout << "Password=";
|
||||
std::cin >> password;
|
||||
/// HOST NICKNAME :
|
||||
std::string hostNickname;
|
||||
cout << "Host Nickname=";
|
||||
std::cin >> hostNickname;
|
||||
|
||||
NetworkInterface::getInstance()->initNetwork(false);
|
||||
//NetworkManager::getInstance()->run();
|
||||
|
||||
NetworkInterface::getInstance()->setLogin(nickname, password);
|
||||
NetworkInterface::getInstance()->connectToHost(hostNickname);
|
||||
|
||||
NetworkManager::getInstance()->setLogin(nickname, password);
|
||||
bool connected = false;
|
||||
//clt.connect(0x0100007f, 7000); // addr in little endian, real address is 7f 00 00 01 (127.0.0.1)
|
||||
std::string buffer;
|
||||
while (1)
|
||||
{
|
||||
cin >> buffer;
|
||||
if (buffer == "cmd=protocolsCount")
|
||||
if (buffer == "cmd=connect")
|
||||
{
|
||||
//cout << protocolListener->runningProtocolsCount() << " protocols are running." << endl;
|
||||
cout << "Host Nickname=";
|
||||
std::cin >> hostNickname;
|
||||
connected = ClientNetworkManager::getInstance()->connectToHost(hostNickname);
|
||||
continue;
|
||||
}
|
||||
if (buffer == "cmd=hideAddress")
|
||||
{
|
||||
}
|
||||
if (buffer == "cmd=login")
|
||||
{
|
||||
std::cout << "Username=";
|
||||
std::cin >> nickname;
|
||||
std::cout << "Password=";
|
||||
std::cin >> password;
|
||||
NetworkManager::getInstance()->setLogin(nickname, password);
|
||||
}
|
||||
if (buffer.size() == 0) { continue; }
|
||||
char buffer2[256];
|
||||
strcpy(buffer2, buffer.c_str());
|
||||
//NetworkInterface::getInstance()->sendPacket(buffer2);
|
||||
if (connected)
|
||||
ClientNetworkManager::getInstance()->sendPacket(buffer2);
|
||||
}
|
||||
|
||||
|
||||
@@ -77,8 +68,9 @@ int main()
|
||||
}
|
||||
else if (answer == "host")
|
||||
{
|
||||
NetworkInterface::getInstance()->initNetwork(true);
|
||||
|
||||
//NetworkInterface::getInstance()->initNetwork(true);
|
||||
ServerNetworkManager::getInstance()->run();
|
||||
ServerNetworkManager::getInstance()->start();
|
||||
//GetPublicAddress
|
||||
|
||||
while(1){}
|
||||
|
||||
10
dev/SocketsBase/network_interface.cpp
Normal file
10
dev/SocketsBase/network_interface.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "network_interface.hpp"
|
||||
|
||||
|
||||
NetworkInterface::NetworkInterface()
|
||||
{
|
||||
}
|
||||
|
||||
NetworkInterface::~NetworkInterface()
|
||||
{
|
||||
}
|
||||
27
dev/SocketsBase/network_interface.hpp
Normal file
27
dev/SocketsBase/network_interface.hpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef NETWORK_INTERFACE_H
|
||||
#define NETWORK_INTERFACE_H
|
||||
|
||||
#include "singleton.hpp"
|
||||
#include "types.hpp"
|
||||
#include "network_manager.hpp"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <pthread.h>
|
||||
#include <string>
|
||||
|
||||
|
||||
class NetworkInterface : public Singleton<NetworkInterface>
|
||||
{
|
||||
friend class Singleton<NetworkInterface>;
|
||||
public:
|
||||
|
||||
void initNetwork(bool server);
|
||||
|
||||
protected:
|
||||
// protected functions
|
||||
NetworkInterface();
|
||||
virtual ~NetworkInterface();
|
||||
|
||||
};
|
||||
|
||||
#endif // NETWORK_INTERFACE_H
|
||||
@@ -1,9 +1,32 @@
|
||||
#include "network_manager.hpp"
|
||||
|
||||
#include "protocols/hide_public_address.hpp"
|
||||
#include "protocols/show_public_address.hpp"
|
||||
#include "protocols/get_public_address.hpp"
|
||||
|
||||
#include "protocol_manager.hpp"
|
||||
#include "client_network_manager.hpp"
|
||||
#include "server_network_manager.hpp"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void* protocolManagerUpdate(void* data)
|
||||
{
|
||||
ProtocolManager* manager = static_cast<ProtocolManager*>(data);
|
||||
while(1)
|
||||
{
|
||||
manager->update();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
NetworkManager::NetworkManager()
|
||||
{
|
||||
m_publicAddress.ip = 0;
|
||||
m_publicAddress.port = 0;
|
||||
m_networkManager = NULL;
|
||||
m_protocolManagerUpdateThread = NULL;
|
||||
}
|
||||
|
||||
NetworkManager::~NetworkManager()
|
||||
@@ -12,6 +35,9 @@ NetworkManager::~NetworkManager()
|
||||
|
||||
void NetworkManager::run()
|
||||
{
|
||||
ProtocolManager::getInstance<ProtocolManager>();
|
||||
m_protocolManagerUpdateThread = (pthread_t*)(malloc(sizeof(pthread_t)));
|
||||
pthread_create(m_protocolManagerUpdateThread, NULL, protocolManagerUpdate, ProtocolManager::getInstance());
|
||||
}
|
||||
|
||||
void NetworkManager::setManualSocketsMode(bool manual)
|
||||
@@ -22,6 +48,18 @@ void NetworkManager::setManualSocketsMode(bool manual)
|
||||
m_localhost->startListening();
|
||||
}
|
||||
|
||||
void NetworkManager::setLogin(std::string username, std::string password)
|
||||
{
|
||||
m_playerLogin.username = username;
|
||||
m_playerLogin.password = password;
|
||||
}
|
||||
|
||||
void NetworkManager::setPublicAddress(uint32_t ip, uint16_t port)
|
||||
{
|
||||
m_publicAddress.ip = ip;
|
||||
m_publicAddress.port = port;
|
||||
}
|
||||
|
||||
STKHost* NetworkManager::getHost()
|
||||
{
|
||||
return m_localhost;
|
||||
|
||||
@@ -7,23 +7,37 @@
|
||||
|
||||
#include "protocol_manager.hpp"
|
||||
#include "singleton.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
class NetworkManager : public Singleton<NetworkManager>
|
||||
{
|
||||
friend class Singleton<NetworkManager>;
|
||||
public:
|
||||
virtual void run() = 0;
|
||||
virtual void run();
|
||||
|
||||
// network management functions
|
||||
virtual void setManualSocketsMode(bool manual);
|
||||
virtual void packetReceived(char* data) = 0;
|
||||
|
||||
// raw data management
|
||||
void setLogin(std::string username, std::string password);
|
||||
void setPublicAddress(uint32_t ip, uint16_t port);
|
||||
|
||||
// getters
|
||||
STKHost* getHost();
|
||||
protected:
|
||||
NetworkManager();
|
||||
virtual ~NetworkManager();
|
||||
|
||||
// protected members
|
||||
std::vector<STKPeer*> m_peers;
|
||||
STKHost* m_localhost;
|
||||
|
||||
TransportAddress m_publicAddress;
|
||||
PlayerLogin m_playerLogin;
|
||||
|
||||
NetworkManager* m_networkManager;
|
||||
pthread_t* m_protocolManagerUpdateThread;
|
||||
};
|
||||
|
||||
#endif // NETWORKMANAGER_HPP
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#include "protocol.hpp"
|
||||
|
||||
Protocol::Protocol(CallbackObject* callbackObject)
|
||||
Protocol::Protocol(CallbackObject* callbackObject, PROTOCOL_TYPE type)
|
||||
{
|
||||
m_callbackObject = callbackObject;
|
||||
m_type = type;
|
||||
}
|
||||
|
||||
Protocol::~Protocol()
|
||||
|
||||
@@ -2,19 +2,21 @@
|
||||
#define PROTOCOL_HPP
|
||||
|
||||
#include "protocol_manager.hpp"
|
||||
#include "callback_object.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
enum PROTOCOL_TYPE
|
||||
{
|
||||
GET_PUBLIC_ADDRESS = 0
|
||||
PROTOCOL_NOT_CONCERNED = 0,
|
||||
PROTOCOL_CONNECTION = 1
|
||||
|
||||
};
|
||||
|
||||
class Protocol
|
||||
{
|
||||
public:
|
||||
Protocol(CallbackObject* callbackObject);
|
||||
Protocol(CallbackObject* callbackObject, PROTOCOL_TYPE type = PROTOCOL_NOT_CONCERNED);
|
||||
virtual ~Protocol();
|
||||
|
||||
virtual void messageReceived(uint8_t* data) = 0;
|
||||
|
||||
@@ -22,6 +22,12 @@ void ProtocolManager::messageReceived(uint8_t* data)
|
||||
m_messagesToProcess.push_back(data);
|
||||
}
|
||||
|
||||
void ProtocolManager::sendMessage(std::string message)
|
||||
{
|
||||
std::string newMessage = " " + message; // add one byte
|
||||
newMessage[0] = (char)(0);
|
||||
}
|
||||
|
||||
int ProtocolManager::startProtocol(Protocol* protocol)
|
||||
{
|
||||
ProtocolInfo protocolInfo;
|
||||
|
||||
@@ -24,12 +24,9 @@ class ProtocolManager : public Singleton<ProtocolManager>
|
||||
uint32_t id;
|
||||
} ProtocolInfo;
|
||||
public:
|
||||
static ProtocolManager* getInstance()
|
||||
{
|
||||
return Singleton<ProtocolManager>::getInstance<ProtocolManager>();
|
||||
}
|
||||
|
||||
virtual void messageReceived(uint8_t* data);
|
||||
virtual void sendMessage(std::string message);
|
||||
|
||||
virtual int startProtocol(Protocol* protocol);
|
||||
virtual void stopProtocol(Protocol* protocol);
|
||||
|
||||
@@ -1,18 +1,15 @@
|
||||
#include "connect_to_server.hpp"
|
||||
|
||||
#include "../http_functions.hpp"
|
||||
#include "../time.hpp"
|
||||
#include "../client_network_manager.hpp"
|
||||
#include "show_public_address.hpp"
|
||||
#include "../time.hpp"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
ConnectToServer::ConnectToServer(CallbackObject* callbackObject) : Protocol(callbackObject)
|
||||
{
|
||||
m_ownPublicIp = 0;
|
||||
m_ownPublicPort = 0;
|
||||
connected = false;
|
||||
m_serverIp = 0;
|
||||
m_serverPort = 0;
|
||||
}
|
||||
|
||||
ConnectToServer::~ConnectToServer()
|
||||
@@ -21,109 +18,50 @@ ConnectToServer::~ConnectToServer()
|
||||
|
||||
void ConnectToServer::messageReceived(uint8_t* data)
|
||||
{
|
||||
|
||||
printf("data received\n");
|
||||
printf("%s", data);
|
||||
m_state = NONE; // we received a message, we are connected
|
||||
}
|
||||
|
||||
void ConnectToServer::setup()
|
||||
{
|
||||
if (m_ownPublicIp == 0 || m_ownPublicPort == 0 || m_username == "" || m_password == "" || m_hostName == "")
|
||||
if (m_serverIp == 0 || m_serverPort == 0 )
|
||||
{
|
||||
printf("You have to set the public ip:port, username:password and the host nickname before starting this protocol.\n");
|
||||
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()
|
||||
{
|
||||
if (m_state == NOTHING)
|
||||
{
|
||||
char url[512];
|
||||
sprintf(url, "http://stkconnect.freeserver.me/log.php?set&nick=%s&ip=%u&port=%u&pwd=%s", m_username.c_str(), m_ownPublicIp, m_ownPublicPort, m_password.c_str());
|
||||
std::string result = HTTP::getPage(url);
|
||||
if (result[0] == 's' && result[1] == 'u' && result[2] == 'c' && result[3] == 'c' && result[4] == 'e' && result[5] == 's' && result[6] == 's')
|
||||
{
|
||||
printf("Address set.\n");
|
||||
m_state = ADDRESS_KNOWN_ONLINE;
|
||||
}
|
||||
if (result[0] == 'f' && result[1] == 'a' && result[2] == 'i' && result[3] == 'l')
|
||||
{
|
||||
printf("Login fail. Please re-set username:password and unpause the protocol.\n");
|
||||
m_state = NOTHING;
|
||||
pause();
|
||||
}
|
||||
ShowPublicAddress* showAddr = new ShowPublicAddress(NULL);
|
||||
showAddr->setUsername(m_username);
|
||||
}
|
||||
else if (m_state == ADDRESS_KNOWN_ONLINE)
|
||||
if (m_state == NONE)
|
||||
{
|
||||
static double target = 0;
|
||||
double currentTime = Time::getSeconds();
|
||||
if (currentTime < target-1800) // sometimes the getSeconds method forgets 3600 seconds.
|
||||
while (currentTime < target-1800) // sometimes the getSeconds method forgets 3600 seconds.
|
||||
currentTime += 3600;
|
||||
if (currentTime > target)
|
||||
{
|
||||
char url[512];
|
||||
sprintf(url, "http://stkconnect.freeserver.me/log.php?get&nick=%s", m_hostName.c_str());
|
||||
std::string result = HTTP::getPage(url);
|
||||
if (result == "")
|
||||
{
|
||||
printf("The host you try to reach does not exist. Change the host name please.\n");
|
||||
m_state = NOTHING;
|
||||
pause();
|
||||
ClientNetworkManager::getInstance()->connect(m_serverIp, m_serverPort);
|
||||
if (ClientNetworkManager::getInstance()->getPeer()->isConnected())
|
||||
{
|
||||
m_state = DONE;
|
||||
return;
|
||||
}
|
||||
std::string ipAddr = result;
|
||||
ipAddr.erase(ipAddr.find_first_of(':'));
|
||||
std::string portNb = result;
|
||||
portNb.erase(0, portNb.find_first_of(':')+1);
|
||||
uint32_t dstIp = (uint32_t)(atoi(ipAddr.c_str()));
|
||||
uint16_t dstPort = (uint32_t)(atoi(portNb.c_str()));
|
||||
if (dstIp == 0 || dstPort == 0)
|
||||
{
|
||||
printf("The host you try to reach is not online. There will be a new try in 10 seconds.\n");
|
||||
target = currentTime+10;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Public ip of target is %i.%i.%i.%i:%i\n", (dstIp>>24)&0xff, (dstIp>>16)&0xff, (dstIp>>8)&0xff, dstIp&0xff, dstPort);
|
||||
m_serverIp = ((dstIp&0x000000ff)<<24) // change the server IP to have a network-byte order
|
||||
+ ((dstIp&0x0000ff00)<<8)
|
||||
+ ((dstIp&0x00ff0000)>>8)
|
||||
+ ((dstIp&0xff000000)>>24);
|
||||
m_serverPort = dstPort;
|
||||
m_state = PEER_ADDRESS_RETREIVED;
|
||||
}
|
||||
target = currentTime+5;
|
||||
printf("Retrying to connect in 5 seconds.\n");
|
||||
}
|
||||
}
|
||||
else if (m_state == PEER_ADDRESS_RETREIVED)
|
||||
else if (m_state == DONE)
|
||||
{
|
||||
// we know the distant address:port, just need to connect.
|
||||
m_state = CONNECTED;
|
||||
connected = true;
|
||||
m_listener->protocolTerminated(this);
|
||||
}
|
||||
else if (m_state == CONNECTED)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ConnectToServer::setSelfAddress(uint32_t ip, uint16_t port)
|
||||
void ConnectToServer::setServerAddress(uint32_t ip, uint16_t port)
|
||||
{
|
||||
m_ownPublicIp = ip;
|
||||
m_ownPublicPort = port;
|
||||
m_serverIp = ip;
|
||||
m_serverPort = port;
|
||||
}
|
||||
|
||||
void ConnectToServer::setUsername(std::string username)
|
||||
{
|
||||
m_username = username;
|
||||
}
|
||||
|
||||
void ConnectToServer::setPassword(std::string password)
|
||||
{
|
||||
m_password = password;
|
||||
}
|
||||
|
||||
void ConnectToServer::setHostName(std::string hostName)
|
||||
{
|
||||
m_hostName = hostName;
|
||||
}
|
||||
|
||||
@@ -14,29 +14,18 @@ class ConnectToServer : public Protocol, public CallbackObject
|
||||
virtual void setup();
|
||||
virtual void update();
|
||||
|
||||
void setSelfAddress(uint32_t ip, uint16_t port);
|
||||
void setUsername(std::string username);
|
||||
void setPassword(std::string password);
|
||||
void setHostName(std::string hostName);
|
||||
void setServerAddress(uint32_t ip, uint16_t port);
|
||||
|
||||
bool connected;
|
||||
protected:
|
||||
uint32_t m_ownPublicIp;
|
||||
uint16_t m_ownPublicPort;
|
||||
uint32_t m_serverIp;
|
||||
uint16_t m_serverPort;
|
||||
std::string m_hostName;
|
||||
std::string m_username;
|
||||
std::string m_password;
|
||||
|
||||
enum STATE
|
||||
{
|
||||
NOTHING,
|
||||
ADDRESS_KNOWN_ONLINE,
|
||||
PEER_ADDRESS_RETREIVED,
|
||||
CONNECTED
|
||||
NONE,
|
||||
DONE
|
||||
};
|
||||
STATE m_state;
|
||||
double firstTime;
|
||||
};
|
||||
|
||||
#endif // CONNECT_TO_SERVER_HPP
|
||||
|
||||
80
dev/SocketsBase/protocols/get_peer_address.cpp
Normal file
80
dev/SocketsBase/protocols/get_peer_address.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include "get_peer_address.hpp"
|
||||
|
||||
#include "../time.hpp"
|
||||
#include "../http_functions.hpp"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
GetPeerAddress::GetPeerAddress(CallbackObject* callbackObject) : Protocol(callbackObject)
|
||||
{
|
||||
}
|
||||
|
||||
GetPeerAddress::~GetPeerAddress()
|
||||
{
|
||||
}
|
||||
|
||||
void GetPeerAddress::messageReceived(uint8_t* data)
|
||||
{
|
||||
}
|
||||
|
||||
void GetPeerAddress::setup()
|
||||
{
|
||||
m_state = NONE;
|
||||
}
|
||||
|
||||
void GetPeerAddress::update()
|
||||
{
|
||||
if (m_state == NONE)
|
||||
{
|
||||
static double target = 0;
|
||||
double currentTime = Time::getSeconds();
|
||||
while (currentTime < target-1800) // sometimes the getSeconds method forgets 3600 seconds.
|
||||
currentTime += 3600;
|
||||
if (currentTime > target)
|
||||
{
|
||||
char url[512];
|
||||
sprintf(url, "http://stkconnect.freeserver.me/log.php?get&nick=%s", m_peerName.c_str());
|
||||
std::string result = HTTP::getPage(url);
|
||||
if (result == "")
|
||||
{
|
||||
printf("__GetPeerAddress> The host you try to reach does not exist. Change the host name please.\n");
|
||||
pause();
|
||||
return;
|
||||
}
|
||||
std::string ipAddr = result;
|
||||
ipAddr.erase(ipAddr.find_first_of(':'));
|
||||
std::string portNb = result;
|
||||
portNb.erase(0, portNb.find_first_of(':')+1);
|
||||
uint32_t dstIp = (uint32_t)(atoi(ipAddr.c_str()));
|
||||
uint16_t dstPort = (uint32_t)(atoi(portNb.c_str()));
|
||||
if (dstIp == 0 || dstPort == 0)
|
||||
{
|
||||
printf("__GetPeerAddress> The host you try to reach is not online. There will be a new try in 10 seconds.\n");
|
||||
target = currentTime+10;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("__GetPeerAddress> Public ip of target is %i.%i.%i.%i:%i\n", (dstIp>>24)&0xff, (dstIp>>16)&0xff, (dstIp>>8)&0xff, dstIp&0xff, dstPort);
|
||||
uint32_t serverIp = ((dstIp&0x000000ff)<<24) // change the server IP to have a network-byte order
|
||||
+ ((dstIp&0x0000ff00)<<8)
|
||||
+ ((dstIp&0x00ff0000)>>8)
|
||||
+ ((dstIp&0xff000000)>>24);
|
||||
uint16_t serverPort = dstPort;
|
||||
TransportAddress* addr = static_cast<TransportAddress*>(m_callbackObject);
|
||||
addr->ip = serverIp;
|
||||
addr->port = serverPort;
|
||||
m_state = DONE;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (m_state == DONE)
|
||||
{
|
||||
m_listener->protocolTerminated(this);
|
||||
}
|
||||
}
|
||||
|
||||
void GetPeerAddress::setPeerName(std::string peerName)
|
||||
{
|
||||
m_peerName = peerName;
|
||||
}
|
||||
29
dev/SocketsBase/protocols/get_peer_address.hpp
Normal file
29
dev/SocketsBase/protocols/get_peer_address.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef GET_PEER_ADDRESS_HPP
|
||||
#define GET_PEER_ADDRESS_HPP
|
||||
|
||||
#include "../protocol.hpp"
|
||||
|
||||
class GetPeerAddress : public Protocol
|
||||
{
|
||||
public:
|
||||
GetPeerAddress(CallbackObject* callbackObject);
|
||||
virtual ~GetPeerAddress();
|
||||
|
||||
virtual void messageReceived(uint8_t* data);
|
||||
virtual void setup();
|
||||
virtual void update();
|
||||
|
||||
void setPeerName(std::string peerName);
|
||||
protected:
|
||||
std::string m_peerName;
|
||||
|
||||
enum STATE
|
||||
{
|
||||
NONE,
|
||||
DONE
|
||||
};
|
||||
STATE m_state;
|
||||
|
||||
};
|
||||
|
||||
#endif // GET_PEER_ADDRESS_HPP
|
||||
@@ -23,7 +23,6 @@ int stunRand()
|
||||
|
||||
GetPublicAddress::GetPublicAddress(CallbackObject* callbackObject) : Protocol(callbackObject)
|
||||
{
|
||||
m_type = GET_PUBLIC_ADDRESS;
|
||||
}
|
||||
|
||||
GetPublicAddress::~GetPublicAddress()
|
||||
|
||||
@@ -17,7 +17,7 @@ void* STKHost::receive_data(void* self)
|
||||
printf("message received\n");
|
||||
switch (event.type) {
|
||||
case ENET_EVENT_TYPE_RECEIVE:
|
||||
//NetworkManager::receptionCallback((char*) event.packet->data);
|
||||
NetworkManager::getInstance()->packetReceived((char*) event.packet->data);
|
||||
break;
|
||||
case ENET_EVENT_TYPE_DISCONNECT:
|
||||
printf("Somebody is now disconnected.\n");
|
||||
|
||||
@@ -12,7 +12,7 @@ STKPeer::~STKPeer()
|
||||
{
|
||||
}
|
||||
|
||||
void STKPeer::connectToServer(STKHost* host, uint32_t ip, uint16_t port, uint32_t channelCount, uint32_t data)
|
||||
bool STKPeer::connectToServer(STKHost* host, uint32_t ip, uint16_t port, uint32_t channelCount, uint32_t data)
|
||||
{
|
||||
ENetAddress address;
|
||||
address.host = ip;
|
||||
@@ -21,13 +21,11 @@ void STKPeer::connectToServer(STKHost* host, uint32_t ip, uint16_t port, uint32_
|
||||
m_peer = enet_host_connect(host->m_host, &address, 2, 0);
|
||||
if (m_peer == NULL)
|
||||
{
|
||||
printf("Could not connect to server.\n");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Connected.\n");
|
||||
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);
|
||||
return true;
|
||||
}
|
||||
|
||||
void STKPeer::sendPacket(char* data)
|
||||
@@ -37,3 +35,9 @@ void STKPeer::sendPacket(char* data)
|
||||
ENetPacket* packet = enet_packet_create(data, strlen(data)+1,ENET_PACKET_FLAG_RELIABLE);
|
||||
enet_peer_send(m_peer, 0, packet);
|
||||
}
|
||||
|
||||
bool STKPeer::isConnected()
|
||||
{
|
||||
printf("PEER STATE %i\n", m_peer->state);
|
||||
return (m_peer->state == ENET_PEER_STATE_CONNECTED);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,9 @@ class STKPeer
|
||||
|
||||
virtual void sendPacket(char* data);
|
||||
|
||||
void connectToServer(STKHost* host, uint32_t ip, uint16_t port, uint32_t channelCount, uint32_t data);
|
||||
bool connectToServer(STKHost* host, uint32_t ip, uint16_t port, uint32_t channelCount, uint32_t data);
|
||||
|
||||
bool isConnected();
|
||||
protected:
|
||||
ENetPeer* m_peer;
|
||||
};
|
||||
|
||||
14
dev/SocketsBase/time.cpp
Normal file
14
dev/SocketsBase/time.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "time.hpp"
|
||||
|
||||
namespace Time
|
||||
{
|
||||
double getSeconds()
|
||||
{
|
||||
time_t timer;
|
||||
time(&timer);
|
||||
struct tm y2k;
|
||||
y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0;
|
||||
y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;
|
||||
return difftime(timer,mktime(&y2k)); // get the seconds elapsed since january 2000
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,11 @@
|
||||
#ifndef TIME_HPP_INCLUDED
|
||||
#define TIME_HPP_INCLUDED
|
||||
#ifndef TIME_HPP
|
||||
#define TIME_HPP
|
||||
|
||||
#include <time.h>
|
||||
|
||||
namespace Time
|
||||
{
|
||||
double getSeconds()
|
||||
{
|
||||
time_t timer;
|
||||
time(&timer);
|
||||
struct tm y2k;
|
||||
y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0;
|
||||
y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;
|
||||
return difftime(timer,mktime(&y2k)); // get the seconds elapsed since january 2000
|
||||
}
|
||||
double getSeconds();
|
||||
}
|
||||
|
||||
#endif // TIME_HPP_INCLUDED
|
||||
|
||||
32
dev/SocketsBase/types.hpp
Normal file
32
dev/SocketsBase/types.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef TYPES_HPP
|
||||
#define TYPES_HPP
|
||||
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
|
||||
class CallbackObject
|
||||
{
|
||||
public:
|
||||
CallbackObject() {}
|
||||
|
||||
};
|
||||
|
||||
class TransportAddress : public CallbackObject
|
||||
{
|
||||
public:
|
||||
TransportAddress() {}
|
||||
|
||||
uint32_t ip;
|
||||
uint16_t port;
|
||||
};
|
||||
|
||||
class PlayerLogin : public CallbackObject
|
||||
{
|
||||
public:
|
||||
PlayerLogin() {}
|
||||
|
||||
std::string username;
|
||||
std::string password;
|
||||
};
|
||||
|
||||
#endif // TYPES_HPP
|
||||
Reference in New Issue
Block a user