base commit
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@12865 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
parent
7eed6c59e8
commit
d933eae338
68
dev/SocketsBase/SocketsBase.cbp
Normal file
68
dev/SocketsBase/SocketsBase.cbp
Normal file
@ -0,0 +1,68 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="SocketsBase" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/SocketsBase" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
</Compiler>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/SocketsBase" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Release/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
</Linker>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-lenet" />
|
||||
<Add option="-lpthread" />
|
||||
</Linker>
|
||||
<Unit filename="client_network_manager.cpp" />
|
||||
<Unit filename="client_network_manager.hpp" />
|
||||
<Unit filename="http_functions.cpp" />
|
||||
<Unit filename="http_functions.hpp" />
|
||||
<Unit filename="main.cpp" />
|
||||
<Unit filename="network_manager.cpp" />
|
||||
<Unit filename="network_manager.hpp" />
|
||||
<Unit filename="packet.cpp" />
|
||||
<Unit filename="packet.hpp" />
|
||||
<Unit filename="protocol.cpp" />
|
||||
<Unit filename="protocol.hpp" />
|
||||
<Unit filename="protocol_listener.cpp" />
|
||||
<Unit filename="protocol_listener.hpp" />
|
||||
<Unit filename="protocol_manager.cpp" />
|
||||
<Unit filename="protocol_manager.hpp" />
|
||||
<Unit filename="protocols/get_public_address.cpp" />
|
||||
<Unit filename="protocols/get_public_address.hpp" />
|
||||
<Unit filename="server_network_manager.cpp" />
|
||||
<Unit filename="server_network_manager.hpp" />
|
||||
<Unit filename="singleton.hpp" />
|
||||
<Unit filename="stk_host.cpp" />
|
||||
<Unit filename="stk_host.hpp" />
|
||||
<Unit filename="stk_peer.cpp" />
|
||||
<Unit filename="stk_peer.hpp" />
|
||||
<Extensions>
|
||||
<code_completion />
|
||||
<debugger />
|
||||
</Extensions>
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
124
dev/SocketsBase/SocketsBase.depend
Normal file
124
dev/SocketsBase/SocketsBase.depend
Normal file
@ -0,0 +1,124 @@
|
||||
# depslib dependency file v1.0
|
||||
1371430952 source:/home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/main.cpp
|
||||
<iostream>
|
||||
<enet/enet.h>
|
||||
"client_network_manager.hpp"
|
||||
"server_network_manager.hpp"
|
||||
"protocol_manager.hpp"
|
||||
"protocols/get_public_address.hpp"
|
||||
<stdio.h>
|
||||
<string.h>
|
||||
|
||||
1371308465 source:/home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/client.cpp
|
||||
"client.hpp"
|
||||
<string.h>
|
||||
|
||||
1371308471 /home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/client.hpp
|
||||
"stk_peer.h"
|
||||
<enet/enet.h>
|
||||
|
||||
1371308808 /home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/stk_peer.h
|
||||
|
||||
1371342078 source:/home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/client_network_manager.cpp
|
||||
"client_network_manager.hpp"
|
||||
<stdio.h>
|
||||
|
||||
1371312921 /home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/client_network_manager.hpp
|
||||
"network_manager.hpp"
|
||||
|
||||
1371431127 /home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/network_manager.hpp
|
||||
"stk_peer.hpp"
|
||||
"stk_host.hpp"
|
||||
<vector>
|
||||
"protocol_listener.hpp"
|
||||
|
||||
1370655017 /home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/server.hpp
|
||||
"stk_peer.h"
|
||||
<enet/enet.h>
|
||||
|
||||
1371431262 source:/home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/network_manager.cpp
|
||||
"network_manager.hpp"
|
||||
<stdio.h>
|
||||
|
||||
1370655140 source:/home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/server.cpp
|
||||
"server.hpp"
|
||||
<string.h>
|
||||
|
||||
1371340734 source:/home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/server_network_manager.cpp
|
||||
"server_network_manager.hpp"
|
||||
<enet/enet.h>
|
||||
<stdio.h>
|
||||
<string.h>
|
||||
<pthread.h>
|
||||
|
||||
1371340658 /home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/server_network_manager.hpp
|
||||
"network_manager.hpp"
|
||||
|
||||
1371345329 source:/home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/stk_peer.cpp
|
||||
"stk_peer.hpp"
|
||||
<stdio.h>
|
||||
<string.h>
|
||||
|
||||
1371342227 /home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/stk_peer.hpp
|
||||
"stk_host.hpp"
|
||||
<enet/enet.h>
|
||||
|
||||
1371431149 /home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/stk_host.hpp
|
||||
<enet/enet.h>
|
||||
|
||||
1371430709 source:/home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/stk_host.cpp
|
||||
"stk_host.hpp"
|
||||
"network_manager.hpp"
|
||||
<stdio.h>
|
||||
<string.h>
|
||||
<pthread.h>
|
||||
|
||||
1371429449 /home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/protocol_listener.hpp
|
||||
<vector>
|
||||
<stdint.h>
|
||||
|
||||
1371344039 source:/home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/http_functions.cpp
|
||||
"http_functions.hpp"
|
||||
<iostream>
|
||||
<string>
|
||||
<curl/curl.h>
|
||||
|
||||
1371343994 /home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/http_functions.hpp
|
||||
<string>
|
||||
|
||||
1371428907 /home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/protocol_manager.hpp
|
||||
"protocol_listener.hpp"
|
||||
|
||||
1371346908 source:/home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/packet.cpp
|
||||
"packet.hpp"
|
||||
|
||||
1371427096 /home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/packet.hpp
|
||||
<string>
|
||||
<vector>
|
||||
|
||||
1371429400 source:/home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/protocol.cpp
|
||||
"protocol.hpp"
|
||||
|
||||
1371429637 /home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/protocol.hpp
|
||||
"protocol_listener.hpp"
|
||||
<stdint.h>
|
||||
|
||||
1371342576 source:/home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/protocol_listener.cpp
|
||||
"protocol_listener.hpp"
|
||||
|
||||
1371429738 source:/home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/protocol_manager.cpp
|
||||
"protocol_manager.hpp"
|
||||
"protocol.hpp"
|
||||
<assert.h>
|
||||
|
||||
1371431357 source:/home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/protocols/get_public_address.cpp
|
||||
"get_public_address.hpp"
|
||||
"../network_manager.hpp"
|
||||
<time.h>
|
||||
<stdlib.h>
|
||||
<assert.h>
|
||||
<stdio.h>
|
||||
|
||||
1371427140 /home/hilnius/Development/svn/supertuxkart/main/branches/hilnius/dev/SocketsBase/protocols/get_public_address.hpp
|
||||
"../protocol.hpp"
|
||||
|
84
dev/SocketsBase/SocketsBase.layout
Normal file
84
dev/SocketsBase/SocketsBase.layout
Normal file
@ -0,0 +1,84 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_layout_file>
|
||||
<ActiveTarget name="Debug" />
|
||||
<File name="stk_host.cpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="352" topLine="3" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="server_network_manager.cpp" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="755" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="protocol.hpp" open="1" top="0" tabpos="12" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="278" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="protocol_manager.hpp" open="1" top="0" tabpos="10" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="319" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="network_manager.hpp" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="154" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="client_network_manager.cpp" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="830" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="client_network_manager.hpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="509" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="stk_peer.hpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="436" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="protocol_manager.cpp" open="1" top="0" tabpos="11" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="115" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="stk_peer.cpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="483" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="network_manager.cpp" open="1" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="501" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="server_network_manager.hpp" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="427" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="main.cpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="587" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="singleton.hpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="942" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="protocol_listener.hpp" open="1" top="0" tabpos="8" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="349" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="stk_host.hpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="857" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
</CodeBlocks_layout_file>
|
BIN
dev/SocketsBase/bin/Debug/SocketsBase
Executable file
BIN
dev/SocketsBase/bin/Debug/SocketsBase
Executable file
Binary file not shown.
42
dev/SocketsBase/client_network_manager.cpp
Normal file
42
dev/SocketsBase/client_network_manager.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
#include "client_network_manager.hpp"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
ClientNetworkManager::ClientNetworkManager()
|
||||
{
|
||||
}
|
||||
|
||||
ClientNetworkManager::~ClientNetworkManager()
|
||||
{
|
||||
}
|
||||
|
||||
void ClientNetworkManager::run()
|
||||
{
|
||||
if (enet_initialize() != 0)
|
||||
{
|
||||
printf("Could not initialize enet.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void ClientNetworkManager::connect(uint32_t ip, uint16_t port)
|
||||
{
|
||||
m_localhost = new STKHost();
|
||||
m_localhost->setupClient(1, 2, 0, 0);
|
||||
m_localhost->startListening();
|
||||
|
||||
STKPeer* peer = new STKPeer();
|
||||
peer->connectToServer(m_localhost, ip, port, 2, 0);
|
||||
|
||||
m_peers.push_back(peer);
|
||||
}
|
||||
|
||||
void ClientNetworkManager::packetReceived(char* data)
|
||||
{
|
||||
printf("ClientNetworkManager::packetReceived()\n");
|
||||
puts(data);
|
||||
}
|
||||
void ClientNetworkManager::sendPacket(char* data)
|
||||
{
|
||||
m_peers[0]->sendPacket(data);
|
||||
}
|
23
dev/SocketsBase/client_network_manager.hpp
Normal file
23
dev/SocketsBase/client_network_manager.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef CLIENT_NETWORK_MANAGER_HPP
|
||||
#define CLIENT_NETWORK_MANAGER_HPP
|
||||
|
||||
#include "network_manager.hpp"
|
||||
|
||||
|
||||
class ClientNetworkManager : public NetworkManager
|
||||
{
|
||||
public:
|
||||
ClientNetworkManager();
|
||||
virtual ~ClientNetworkManager();
|
||||
|
||||
virtual void run();
|
||||
|
||||
void connect(uint32_t ip, uint16_t port);
|
||||
|
||||
virtual void packetReceived(char* data);
|
||||
virtual void sendPacket(char* data);
|
||||
protected:
|
||||
private:
|
||||
};
|
||||
|
||||
#endif // CLIENT_NETWORK_MANAGER_HPP
|
47
dev/SocketsBase/http_functions.cpp
Normal file
47
dev/SocketsBase/http_functions.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include "http_functions.hpp"
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <curl/curl.h>
|
||||
|
||||
namespace HTTP
|
||||
{
|
||||
CURL *curl;
|
||||
CURLcode res;
|
||||
|
||||
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
||||
{
|
||||
((std::string*)userp)->append((char*)contents, size * nmemb);
|
||||
return size * nmemb;
|
||||
}
|
||||
|
||||
void init()
|
||||
{
|
||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
curl = curl_easy_init();
|
||||
if(!curl)
|
||||
printf("Error while loading cURL library.\n");
|
||||
}
|
||||
|
||||
std::string getPage(std::string url)
|
||||
{
|
||||
std::string readBuffer = "";
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
|
||||
res = curl_easy_perform(curl);
|
||||
if(res != CURLE_OK)
|
||||
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
|
||||
|
||||
readBuffer.erase(readBuffer.begin()+64, readBuffer.end());
|
||||
return readBuffer;
|
||||
}
|
||||
|
||||
void shutdown()
|
||||
{
|
||||
curl_easy_cleanup(curl);
|
||||
curl_global_cleanup();
|
||||
}
|
||||
|
||||
}
|
17
dev/SocketsBase/http_functions.hpp
Normal file
17
dev/SocketsBase/http_functions.hpp
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef HTTP_FUNCTIONS_HPP
|
||||
#define HTTP_FUNCTIONS_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace HTTP
|
||||
{
|
||||
|
||||
void init();
|
||||
void shutdown();
|
||||
|
||||
std::string getPage(std::string url);
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif // HTTP_FUNCTIONS_HPP
|
60
dev/SocketsBase/main.cpp
Normal file
60
dev/SocketsBase/main.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
#include <iostream>
|
||||
#include <enet/enet.h>
|
||||
|
||||
#include "client_network_manager.hpp"
|
||||
#include "server_network_manager.hpp"
|
||||
#include "protocol_manager.hpp"
|
||||
#include "protocols/get_public_address.hpp"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
using namespace std;
|
||||
ProtocolManager* protocolListener;
|
||||
|
||||
void* foo(void* data)
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
protocolListener->update();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string answer;
|
||||
cout << "host or client:";
|
||||
cin >> answer;
|
||||
if (answer == "client")
|
||||
{
|
||||
ClientNetworkManager clt;
|
||||
clt.run();
|
||||
clt.connect(0x0100007f, 7000); // addr in little endian, real address is 7f 00 00 01 (127.0.0.1)
|
||||
|
||||
protocolListener = new ProtocolManager();
|
||||
|
||||
pthread_t* thrd = (pthread_t*)(malloc(sizeof(pthread_t)));
|
||||
pthread_create(thrd, NULL, foo, NULL);
|
||||
|
||||
// start a retreive stun addr protocol
|
||||
Protocol* prt = new GetPublicAddress();
|
||||
protocolListener->runProtocol(prt);
|
||||
|
||||
char buffer[256];
|
||||
while (1)
|
||||
{
|
||||
gets(buffer);
|
||||
if (strlen(buffer) == 0) { continue; }
|
||||
clt.sendPacket(buffer);
|
||||
}
|
||||
}
|
||||
else if (answer == "host")
|
||||
{
|
||||
ServerNetworkManager srv;
|
||||
srv.run();
|
||||
srv.start();
|
||||
//srv.protocolListener = new ProtocolManager();
|
||||
while(1){}
|
||||
}
|
||||
return 0;
|
||||
}
|
43
dev/SocketsBase/network_manager.cpp
Normal file
43
dev/SocketsBase/network_manager.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
#include "network_manager.hpp"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
NetworkManager* NetworkManager::instance = NULL;
|
||||
|
||||
NetworkManager::NetworkManager()
|
||||
{
|
||||
if (NetworkManager::instance)
|
||||
{
|
||||
printf("Warning : a Newtork Manager is being deleted.\n");
|
||||
delete NetworkManager::instance;
|
||||
}
|
||||
NetworkManager::instance = this;
|
||||
printf("New Network Manager created.\n");
|
||||
}
|
||||
|
||||
NetworkManager::~NetworkManager()
|
||||
{
|
||||
}
|
||||
|
||||
void NetworkManager::run()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void NetworkManager::sendRawPacket(uint8_t* data, int length, unsigned int dstIp, unsigned short dstPort)
|
||||
{
|
||||
instance->getHost()->sendRawPacket(data, length, dstIp, dstPort);
|
||||
}
|
||||
uint8_t* NetworkManager::receiveRawPacket()
|
||||
{
|
||||
return instance->getHost()->receiveRawPacket();
|
||||
}
|
||||
void NetworkManager::receptionCallback(char* data)
|
||||
{
|
||||
instance->packetReceived(data);
|
||||
}
|
||||
|
||||
STKHost* NetworkManager::getHost()
|
||||
{
|
||||
return m_localhost;
|
||||
}
|
31
dev/SocketsBase/network_manager.hpp
Normal file
31
dev/SocketsBase/network_manager.hpp
Normal file
@ -0,0 +1,31 @@
|
||||
#ifndef NETWORKMANAGER_HPP
|
||||
#define NETWORKMANAGER_HPP
|
||||
|
||||
#include "stk_peer.hpp"
|
||||
#include "stk_host.hpp"
|
||||
#include <vector>
|
||||
|
||||
#include "protocol_listener.hpp"
|
||||
|
||||
class NetworkManager
|
||||
{
|
||||
public:
|
||||
NetworkManager();
|
||||
virtual ~NetworkManager();
|
||||
|
||||
virtual void run() = 0;
|
||||
|
||||
static void sendRawPacket(uint8_t* data, int length, unsigned int dstIp, unsigned short dstPort);
|
||||
static uint8_t* receiveRawPacket();
|
||||
static void receptionCallback(char* data);
|
||||
virtual void packetReceived(char* data) = 0;
|
||||
|
||||
STKHost* getHost();
|
||||
protected:
|
||||
std::vector<STKPeer*> m_peers;
|
||||
STKHost* m_localhost;
|
||||
|
||||
static NetworkManager* instance;
|
||||
};
|
||||
|
||||
#endif // NETWORKMANAGER_HPP
|
9
dev/SocketsBase/packet.cpp
Normal file
9
dev/SocketsBase/packet.cpp
Normal file
@ -0,0 +1,9 @@
|
||||
#include "packet.hpp"
|
||||
|
||||
Packet::Packet()
|
||||
{
|
||||
}
|
||||
|
||||
Packet::~Packet()
|
||||
{
|
||||
}
|
23
dev/SocketsBase/packet.hpp
Normal file
23
dev/SocketsBase/packet.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef PACKET_HPP
|
||||
#define PACKET_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Packet
|
||||
{
|
||||
public:
|
||||
Packet();
|
||||
virtual ~Packet();
|
||||
|
||||
virtual std::string getData();
|
||||
|
||||
protected:
|
||||
char* m_packetHeader;
|
||||
unsigned int m_packetHeaderSize;
|
||||
unsigned int m_packetSize;
|
||||
Packet* m_parent;
|
||||
std::vector<Packet*> m_children;
|
||||
};
|
||||
|
||||
#endif // PACKET_HPP
|
14
dev/SocketsBase/protocol.cpp
Normal file
14
dev/SocketsBase/protocol.cpp
Normal file
@ -0,0 +1,14 @@
|
||||
#include "protocol.hpp"
|
||||
|
||||
Protocol::Protocol()
|
||||
{
|
||||
}
|
||||
|
||||
Protocol::~Protocol()
|
||||
{
|
||||
}
|
||||
|
||||
PROTOCOL_TYPE Protocol::getProtocolType()
|
||||
{
|
||||
return m_type;
|
||||
}
|
31
dev/SocketsBase/protocol.hpp
Normal file
31
dev/SocketsBase/protocol.hpp
Normal file
@ -0,0 +1,31 @@
|
||||
#ifndef PROTOCOL_HPP
|
||||
#define PROTOCOL_HPP
|
||||
|
||||
#include "protocol_listener.hpp"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
enum PROTOCOL_TYPE
|
||||
{
|
||||
GET_PUBLIC_ADDRESS = 0
|
||||
};
|
||||
|
||||
class Protocol
|
||||
{
|
||||
public:
|
||||
Protocol();
|
||||
virtual ~Protocol();
|
||||
|
||||
virtual void messageReceived(uint8_t* data) = 0;
|
||||
|
||||
virtual void setup() = 0;
|
||||
virtual void start() = 0;
|
||||
virtual void update() = 0;
|
||||
|
||||
PROTOCOL_TYPE getProtocolType();
|
||||
protected:
|
||||
ProtocolListener* m_listener;
|
||||
PROTOCOL_TYPE m_type;
|
||||
};
|
||||
|
||||
#endif // PROTOCOL_HPP
|
9
dev/SocketsBase/protocol_listener.cpp
Normal file
9
dev/SocketsBase/protocol_listener.cpp
Normal file
@ -0,0 +1,9 @@
|
||||
#include "protocol_listener.hpp"
|
||||
|
||||
ProtocolListener::ProtocolListener()
|
||||
{
|
||||
}
|
||||
|
||||
ProtocolListener::~ProtocolListener()
|
||||
{
|
||||
}
|
26
dev/SocketsBase/protocol_listener.hpp
Normal file
26
dev/SocketsBase/protocol_listener.hpp
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef PROTOCOL_LISTENER_HPP
|
||||
#define PROTOCOL_LISTENER_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <stdint.h>
|
||||
|
||||
class Protocol;
|
||||
|
||||
class ProtocolListener
|
||||
{
|
||||
public:
|
||||
ProtocolListener();
|
||||
virtual ~ProtocolListener();
|
||||
|
||||
virtual void messageReceived(uint8_t* data) = 0;
|
||||
|
||||
virtual void runProtocol(Protocol* protocol) = 0;
|
||||
virtual void stopProtocol(Protocol* protocol) = 0;
|
||||
|
||||
virtual void update() = 0;
|
||||
|
||||
protected:
|
||||
std::vector<Protocol*> m_protocols;
|
||||
};
|
||||
|
||||
#endif // PROTOCOL_LISTENER_HPP
|
51
dev/SocketsBase/protocol_manager.cpp
Normal file
51
dev/SocketsBase/protocol_manager.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
#include "protocol_manager.hpp"
|
||||
|
||||
#include "protocol.hpp"
|
||||
#include <assert.h>
|
||||
|
||||
ProtocolManager::ProtocolManager() : ProtocolListener()
|
||||
{
|
||||
}
|
||||
|
||||
ProtocolManager::~ProtocolManager()
|
||||
{
|
||||
}
|
||||
|
||||
void ProtocolManager::messageReceived(uint8_t* data)
|
||||
{
|
||||
assert(data);
|
||||
m_messagesToProcess.push_back(data);
|
||||
}
|
||||
|
||||
void ProtocolManager::runProtocol(Protocol* protocol)
|
||||
{
|
||||
m_protocols.push_back(protocol);
|
||||
protocol->setup();
|
||||
protocol->start();
|
||||
}
|
||||
void ProtocolManager::stopProtocol(Protocol* protocol)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ProtocolManager::update()
|
||||
{
|
||||
// before updating, notice protocols that they have received information
|
||||
int size = m_messagesToProcess.size();
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
uint8_t* data = m_messagesToProcess.back();
|
||||
PROTOCOL_TYPE searchedProtocol = (PROTOCOL_TYPE)(data[0]);
|
||||
for (unsigned int i = 0; i < m_protocols.size() ; i++)
|
||||
{
|
||||
if (m_protocols[i]->getProtocolType() == searchedProtocol)
|
||||
m_protocols[i]->messageReceived(data+1);
|
||||
}
|
||||
m_messagesToProcess.pop_back();
|
||||
}
|
||||
// now update all protocols
|
||||
for (unsigned int i = 0; i < m_protocols.size(); i++)
|
||||
{
|
||||
m_protocols[i]->update();
|
||||
}
|
||||
}
|
22
dev/SocketsBase/protocol_manager.hpp
Normal file
22
dev/SocketsBase/protocol_manager.hpp
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef PROTOCOL_MANAGER_HPP
|
||||
#define PROTOCOL_MANAGER_HPP
|
||||
|
||||
#include "protocol_listener.hpp"
|
||||
|
||||
class ProtocolManager : public ProtocolListener
|
||||
{
|
||||
public:
|
||||
ProtocolManager();
|
||||
virtual ~ProtocolManager();
|
||||
|
||||
virtual void messageReceived(uint8_t* data);
|
||||
|
||||
virtual void runProtocol(Protocol* protocol);
|
||||
virtual void stopProtocol(Protocol* protocol);
|
||||
|
||||
virtual void update();
|
||||
protected:
|
||||
std::vector<uint8_t*> m_messagesToProcess;
|
||||
};
|
||||
|
||||
#endif // PROTOCOL_MANAGER_HPP
|
121
dev/SocketsBase/protocols/get_public_address.cpp
Normal file
121
dev/SocketsBase/protocols/get_public_address.cpp
Normal file
@ -0,0 +1,121 @@
|
||||
#include "get_public_address.hpp"
|
||||
|
||||
#include "../network_manager.hpp"
|
||||
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int stunRand()
|
||||
{
|
||||
static bool init = false;
|
||||
if (!init)
|
||||
{
|
||||
srand(time(NULL));
|
||||
init = true;
|
||||
}
|
||||
return rand();
|
||||
}
|
||||
|
||||
GetPublicAddress::GetPublicAddress() : Protocol()
|
||||
{
|
||||
m_type = GET_PUBLIC_ADDRESS;
|
||||
}
|
||||
|
||||
GetPublicAddress::~GetPublicAddress()
|
||||
{
|
||||
}
|
||||
|
||||
void GetPublicAddress::messageReceived(uint8_t* data)
|
||||
{
|
||||
assert(data);
|
||||
if (m_state == TEST_SENT && sizeof(data) >= 20)
|
||||
{
|
||||
|
||||
m_state = ADDRESS_KNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
void GetPublicAddress::setup()
|
||||
{
|
||||
m_state = NOTHING_DONE;
|
||||
}
|
||||
|
||||
void GetPublicAddress::start()
|
||||
{
|
||||
}
|
||||
|
||||
void GetPublicAddress::update()
|
||||
{
|
||||
if (m_state == NOTHING_DONE)
|
||||
{
|
||||
// format : 00MMMMMCMMMCMMMM (cf rfc 5389)
|
||||
uint16_t message_type = 0b0000000000000001; // binding request
|
||||
m_stunTransactionID[0] = stunRand();
|
||||
m_stunTransactionID[1] = stunRand();
|
||||
m_stunTransactionID[2] = stunRand();
|
||||
uint16_t message_length = 0x0000;
|
||||
|
||||
uint8_t bytes[21]; // the message to be sent
|
||||
// bytes 0-1 : the type of the message,
|
||||
bytes[0] = (uint8_t)(message_type>>8);
|
||||
bytes[1] = (uint8_t)(message_type);
|
||||
|
||||
// bytes 2-3 : message length added to header (attributes)
|
||||
bytes[2] = (uint8_t)(message_length>>8);
|
||||
bytes[3] = (uint8_t)(message_length);
|
||||
|
||||
// bytes 4-7 : magic cookie to recognize the stun protocol
|
||||
bytes[4] = (uint8_t)(m_stunMagicCookie>>24);
|
||||
bytes[5] = (uint8_t)(m_stunMagicCookie>>16);
|
||||
bytes[6] = (uint8_t)(m_stunMagicCookie>>8);
|
||||
bytes[7] = (uint8_t)(m_stunMagicCookie);
|
||||
|
||||
// bytes 8-19 : the transaction id
|
||||
bytes[8] = (uint8_t)(m_stunTransactionID[0]>>24);
|
||||
bytes[9] = (uint8_t)(m_stunTransactionID[0]>>16);
|
||||
bytes[10] = (uint8_t)(m_stunTransactionID[0]>>8);
|
||||
bytes[11] = (uint8_t)(m_stunTransactionID[0]);
|
||||
bytes[12] = (uint8_t)(m_stunTransactionID[1]>>24);
|
||||
bytes[13] = (uint8_t)(m_stunTransactionID[1]>>16);
|
||||
bytes[14] = (uint8_t)(m_stunTransactionID[1]>>8);
|
||||
bytes[15] = (uint8_t)(m_stunTransactionID[1]);
|
||||
bytes[16] = (uint8_t)(m_stunTransactionID[2]>>24);
|
||||
bytes[17] = (uint8_t)(m_stunTransactionID[2]>>16);
|
||||
bytes[18] = (uint8_t)(m_stunTransactionID[2]>>8);
|
||||
bytes[19] = (uint8_t)(m_stunTransactionID[2]);
|
||||
bytes[20] = '\0';
|
||||
|
||||
unsigned int dst = 132*256*256*256+177*256*256+123*256+6;
|
||||
NetworkManager::sendRawPacket(bytes, 20, dst, 3478);
|
||||
m_state = TEST_SENT;
|
||||
|
||||
uint8_t* data = NetworkManager::receiveRawPacket();
|
||||
assert(data);
|
||||
// check that the stun response is a response, contains the magic cookie and the transaction ID
|
||||
if ( data[0] == 0b01 &&
|
||||
data[1] == 0b01 &&
|
||||
data[4] == (uint8_t)(m_stunMagicCookie>>24) &&
|
||||
data[5] == (uint8_t)(m_stunMagicCookie>>16) &&
|
||||
data[6] == (uint8_t)(m_stunMagicCookie>>8) &&
|
||||
data[7] == (uint8_t)(m_stunMagicCookie) &&
|
||||
data[8] == (uint8_t)(m_stunTransactionID[0]>>24) &&
|
||||
data[9] == (uint8_t)(m_stunTransactionID[0]>>16) &&
|
||||
data[10] == (uint8_t)(m_stunTransactionID[0]>>8 ) &&
|
||||
data[11] == (uint8_t)(m_stunTransactionID[0] ) &&
|
||||
data[12] == (uint8_t)(m_stunTransactionID[1]>>24) &&
|
||||
data[13] == (uint8_t)(m_stunTransactionID[1]>>16) &&
|
||||
data[14] == (uint8_t)(m_stunTransactionID[1]>>8 ) &&
|
||||
data[15] == (uint8_t)(m_stunTransactionID[1] ) &&
|
||||
data[16] == (uint8_t)(m_stunTransactionID[2]>>24) &&
|
||||
data[17] == (uint8_t)(m_stunTransactionID[2]>>16) &&
|
||||
data[18] == (uint8_t)(m_stunTransactionID[2]>>8 ) &&
|
||||
data[19] == (uint8_t)(m_stunTransactionID[2] ))
|
||||
{
|
||||
printf("the stun server reponded a valid answer\n");
|
||||
int messageSize = data[2]*256+data[3];
|
||||
printf("the answer is %i bytes long\n", messageSize);
|
||||
}
|
||||
}
|
||||
}
|
30
dev/SocketsBase/protocols/get_public_address.hpp
Normal file
30
dev/SocketsBase/protocols/get_public_address.hpp
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef GET_PUBLIC_ADDRESS_HPP
|
||||
#define GET_PUBLIC_ADDRESS_HPP
|
||||
|
||||
#include "../protocol.hpp"
|
||||
|
||||
class GetPublicAddress : public Protocol
|
||||
{
|
||||
public:
|
||||
GetPublicAddress();
|
||||
virtual ~GetPublicAddress();
|
||||
|
||||
virtual void messageReceived(uint8_t* data);
|
||||
|
||||
virtual void setup();
|
||||
virtual void start();
|
||||
virtual void update();
|
||||
|
||||
protected:
|
||||
enum STATE
|
||||
{
|
||||
NOTHING_DONE,
|
||||
TEST_SENT,
|
||||
ADDRESS_KNOWN
|
||||
};
|
||||
STATE m_state;
|
||||
uint32_t m_stunTransactionID[3];
|
||||
const uint32_t m_stunMagicCookie = 0x2112A442;
|
||||
};
|
||||
|
||||
#endif // GET_PUBLIC_ADDRESS_HPP
|
43
dev/SocketsBase/server_network_manager.cpp
Normal file
43
dev/SocketsBase/server_network_manager.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
#include "server_network_manager.hpp"
|
||||
|
||||
#include <enet/enet.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
|
||||
ServerNetworkManager::ServerNetworkManager()
|
||||
{
|
||||
m_localhost = NULL;
|
||||
}
|
||||
|
||||
ServerNetworkManager::~ServerNetworkManager()
|
||||
{
|
||||
}
|
||||
|
||||
void ServerNetworkManager::run()
|
||||
{
|
||||
if (enet_initialize() != 0)
|
||||
{
|
||||
printf("Could not initialize enet.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void ServerNetworkManager::start()
|
||||
{
|
||||
m_localhost = new STKHost();
|
||||
m_localhost->setupServer(STKHost::HOST_ANY, 7000, 32, 2, 0, 0);
|
||||
m_localhost->startListening();
|
||||
printf("Server now setup.\n");
|
||||
}
|
||||
|
||||
void ServerNetworkManager::packetReceived(char* data)
|
||||
{
|
||||
printf("ServerNetworkManager::packetReceived()\n");
|
||||
puts(data);
|
||||
sendPacket(data);
|
||||
}
|
||||
void ServerNetworkManager::sendPacket(char* data)
|
||||
{
|
||||
m_localhost->broadcastPacket(data);
|
||||
}
|
23
dev/SocketsBase/server_network_manager.hpp
Normal file
23
dev/SocketsBase/server_network_manager.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef SERVER_NETWORK_MANAGER_HPP
|
||||
#define SERVER_NETWORK_MANAGER_HPP
|
||||
|
||||
#include "network_manager.hpp"
|
||||
|
||||
|
||||
class ServerNetworkManager : public NetworkManager
|
||||
{
|
||||
public:
|
||||
ServerNetworkManager();
|
||||
virtual ~ServerNetworkManager();
|
||||
|
||||
virtual void run();
|
||||
|
||||
void start();
|
||||
|
||||
virtual void packetReceived(char* data);
|
||||
virtual void sendPacket(char* data);
|
||||
protected:
|
||||
|
||||
};
|
||||
|
||||
#endif // SERVER_NETWORK_MANAGER_HPP
|
43
dev/SocketsBase/singleton.hpp
Normal file
43
dev/SocketsBase/singleton.hpp
Normal file
@ -0,0 +1,43 @@
|
||||
#ifndef SINGLETON_HPP
|
||||
#define SINGLETON_HPP
|
||||
|
||||
#include <iostream>
|
||||
|
||||
template <typename T>
|
||||
class Singleton
|
||||
{
|
||||
protected:
|
||||
Singleton () { }
|
||||
~Singleton () { std::cout << "destroying singleton." << std::endl; }
|
||||
|
||||
public:
|
||||
static T *getInstance ()
|
||||
{
|
||||
if (NULL == m_singleton)
|
||||
{
|
||||
std::cout << "creating singleton." << std::endl;
|
||||
m_singleton = new T;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "singleton already created!" << std::endl;
|
||||
}
|
||||
return (static_cast<T*> (m_singleton));
|
||||
}
|
||||
|
||||
static void kill ()
|
||||
{
|
||||
if (NULL != m_singleton)
|
||||
{
|
||||
delete m_singleton;
|
||||
m_singleton = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
static T *m_singleton;
|
||||
};
|
||||
|
||||
template <typename T> T *Singleton<T>::_singleton = NULL;
|
||||
|
||||
#endif // SINGLETON_HPP
|
108
dev/SocketsBase/stk_host.cpp
Normal file
108
dev/SocketsBase/stk_host.cpp
Normal file
@ -0,0 +1,108 @@
|
||||
#include "stk_host.hpp"
|
||||
|
||||
#include "network_manager.hpp"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
|
||||
void* STKHost::receive_data(void* self)
|
||||
{
|
||||
ENetEvent event;
|
||||
ENetHost* host = (((STKHost*)(self))->m_host);
|
||||
while (1)
|
||||
{
|
||||
while (enet_host_service(host, &event, 0) != 0) {
|
||||
printf("message received\n");
|
||||
switch (event.type) {
|
||||
case ENET_EVENT_TYPE_RECEIVE:
|
||||
NetworkManager::receptionCallback((char*) event.packet->data);
|
||||
break;
|
||||
case ENET_EVENT_TYPE_DISCONNECT:
|
||||
printf("Somebody is now disconnected.\n");
|
||||
case ENET_EVENT_TYPE_CONNECT:
|
||||
printf("A client has just connected.\n");
|
||||
break;
|
||||
case ENET_EVENT_TYPE_NONE:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
STKHost::STKHost()
|
||||
{
|
||||
m_host = NULL;
|
||||
}
|
||||
|
||||
STKHost::~STKHost()
|
||||
{
|
||||
}
|
||||
|
||||
void STKHost::setupServer(uint32_t address, uint16_t port, int peerCount, int channelLimit, uint32_t maxIncomingBandwidth, uint32_t maxOutgoingBandwidth)
|
||||
{
|
||||
ENetAddress addr;
|
||||
addr.host = address;
|
||||
addr.port = port;
|
||||
|
||||
m_host = enet_host_create(&addr, peerCount, channelLimit, maxIncomingBandwidth, maxOutgoingBandwidth);
|
||||
if (m_host == NULL)
|
||||
{
|
||||
fprintf (stderr, "An error occurred while trying to create an ENet server host.\n");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
//pthread_t* thrd = (pthread_t*)(malloc(sizeof(pthread_t)));
|
||||
//pthread_create(thrd, NULL, &STKHost::receive_data, this);
|
||||
}
|
||||
|
||||
void STKHost::setupClient(int peerCount, int channelLimit, uint32_t maxIncomingBandwidth, uint32_t maxOutgoingBandwidth)
|
||||
{
|
||||
m_host = enet_host_create(NULL, peerCount, channelLimit, maxIncomingBandwidth, maxOutgoingBandwidth);
|
||||
if (m_host == NULL)
|
||||
{
|
||||
fprintf (stderr, "An error occurred while trying to create an ENet client host.\n");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
//pthread_t* thrd = (pthread_t*)(malloc(sizeof(pthread_t)));
|
||||
//pthread_create(thrd, NULL, &STKHost::receive_data, this);
|
||||
}
|
||||
|
||||
void STKHost::startListening()
|
||||
{
|
||||
pthread_t* thrd = (pthread_t*)(malloc(sizeof(pthread_t)));
|
||||
pthread_create(thrd, NULL, &STKHost::receive_data, this);
|
||||
}
|
||||
|
||||
void STKHost::sendRawPacket(uint8_t* data, int length, unsigned int dstIp, unsigned short dstPort)
|
||||
{
|
||||
struct sockaddr_in to;
|
||||
int toLen = sizeof(to);
|
||||
memset(&to,0,toLen);
|
||||
|
||||
to.sin_family = AF_INET;
|
||||
to.sin_port = htons(dstPort);
|
||||
to.sin_addr.s_addr = htonl(dstIp);
|
||||
|
||||
sendto(m_host->socket, data, length, 0,(sockaddr*)&to, toLen);
|
||||
}
|
||||
|
||||
uint8_t* STKHost::receiveRawPacket()
|
||||
{
|
||||
uint8_t* buffer; // max size needed normally (only used for stun)
|
||||
buffer = (uint8_t*)(malloc(sizeof(uint8_t)*2048));
|
||||
int len = recv(m_host->socket, buffer, 2048, 0);
|
||||
|
||||
if ( len == -1 ) // socket error
|
||||
{
|
||||
printf("Socket Error while receiving information.\n");
|
||||
return NULL;
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void STKHost::broadcastPacket(char* data)
|
||||
{
|
||||
ENetPacket* packet = enet_packet_create(data, strlen(data)+1,ENET_PACKET_FLAG_RELIABLE);
|
||||
enet_host_broadcast(m_host, 0, packet);
|
||||
}
|
37
dev/SocketsBase/stk_host.hpp
Normal file
37
dev/SocketsBase/stk_host.hpp
Normal file
@ -0,0 +1,37 @@
|
||||
#ifndef STK_HOST_HPP
|
||||
#define STK_HOST_HPP
|
||||
|
||||
#include <enet/enet.h>
|
||||
|
||||
|
||||
class STKHost
|
||||
{
|
||||
friend class STKPeer;
|
||||
public:
|
||||
enum HOST_TYPE
|
||||
{
|
||||
HOST_ANY = 0,
|
||||
HOST_BROADCAST = 0xFFFFFFFF,
|
||||
PORT_ANY = 0
|
||||
};
|
||||
|
||||
STKHost();
|
||||
virtual ~STKHost();
|
||||
|
||||
static void* receive_data(void* self);
|
||||
|
||||
void setupServer(uint32_t address, uint16_t port, int peerCount, int channelLimit, uint32_t maxIncomingBandwidth, uint32_t maxOutgoingBandwidth);
|
||||
void setupClient(int peerCount, int channelLimit, uint32_t maxIncomingBandwidth, uint32_t maxOutgoingBandwidth);
|
||||
|
||||
void startListening();
|
||||
|
||||
void sendRawPacket(uint8_t* data, int length, unsigned int dstIp, unsigned short dstPort);
|
||||
uint8_t* receiveRawPacket();
|
||||
void broadcastPacket(char* data);
|
||||
|
||||
protected:
|
||||
ENetHost* m_host;
|
||||
|
||||
};
|
||||
|
||||
#endif // STK_HOST_HPP
|
33
dev/SocketsBase/stk_peer.cpp
Normal file
33
dev/SocketsBase/stk_peer.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include "stk_peer.hpp"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
STKPeer::STKPeer()
|
||||
{
|
||||
m_peer = NULL;
|
||||
}
|
||||
|
||||
STKPeer::~STKPeer()
|
||||
{
|
||||
}
|
||||
|
||||
void STKPeer::connectToServer(STKHost* host, uint32_t ip, uint16_t port, uint32_t channelCount, uint32_t data)
|
||||
{
|
||||
ENetAddress address;
|
||||
address.host = ip;
|
||||
address.port = port;
|
||||
|
||||
m_peer = enet_host_connect(host->m_host, &address, 2, 0);
|
||||
if (m_peer == NULL)
|
||||
{
|
||||
printf("Could not connect to server.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
22
dev/SocketsBase/stk_peer.hpp
Normal file
22
dev/SocketsBase/stk_peer.hpp
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef STK_PEER_HPP
|
||||
#define STK_PEER_HPP
|
||||
|
||||
#include "stk_host.hpp"
|
||||
#include <enet/enet.h>
|
||||
|
||||
class STKPeer
|
||||
{
|
||||
public:
|
||||
STKPeer();
|
||||
virtual ~STKPeer();
|
||||
|
||||
static void* receive_data(void* self);
|
||||
|
||||
virtual void sendPacket(char* data);
|
||||
|
||||
void connectToServer(STKHost* host, uint32_t ip, uint16_t port, uint32_t channelCount, uint32_t data);
|
||||
protected:
|
||||
ENetPeer* m_peer;
|
||||
};
|
||||
|
||||
#endif // STK_PEER_HPP
|
Loading…
Reference in New Issue
Block a user