cleaning http function

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@12882 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hilnius 2013-06-18 15:31:04 +00:00
parent f63ab88f88
commit 918af26024
10 changed files with 110 additions and 51 deletions

View File

@ -14,36 +14,6 @@ namespace HTTP
CURL *curl;
CURLcode res;
struct string {
char *ptr;
size_t len;
};
void init_string(struct string *s) {
s->len = 0;
s->ptr = (char*)(malloc(s->len+1));
if (s->ptr == NULL) {
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILURE);
}
s->ptr[0] = '\0';
}
size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
{
size_t new_len = s->len + size*nmemb;
s->ptr = (char*)(realloc(s->ptr, new_len+1));
if (s->ptr == NULL) {
fprintf(stderr, "realloc() failed\n");
exit(EXIT_FAILURE);
}
memcpy(s->ptr+s->len, ptr, size*nmemb);
s->ptr[new_len] = '\0';
s->len = new_len;
return size*nmemb;
}
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
@ -60,16 +30,15 @@ void init()
std::string getPage(std::string url)
{
struct string readBuffer;
init_string(&readBuffer);
std::string readBuffer;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
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));
return readBuffer.ptr;
return readBuffer;
}
void shutdown()

View File

@ -41,9 +41,14 @@ int main()
std::string password;
cout << "Password=";
std::cin >> password;
/// HOST NICKNAME :
std::string hostNickname;
cout << "Nickname=";
std::cin >> hostNickname;
ConnectToServer* connectionProtocol = new ConnectToServer(NULL);
connectionProtocol->setPassword(password);
connectionProtocol->setUsername(nickname);
connectionProtocol->setHostName(hostNickname);
ClientNetworkManager clt;

View File

@ -23,6 +23,8 @@ class Protocol
virtual void setup() = 0;
virtual void start() = 0;
virtual void pause() = 0;
virtual void unpause() = 0;
virtual void update() = 0;
PROTOCOL_TYPE getProtocolType();

View File

@ -22,29 +22,54 @@ void ProtocolManager::messageReceived(uint8_t* data)
void ProtocolManager::runProtocol(Protocol* protocol)
{
m_protocols.push_back(protocol);
ProtocolInfo protocolInfo;
protocolInfo.paused = false;
protocolInfo.protocol = protocol;
m_protocols.push_back(protocolInfo);
protocol->setListener(this);
protocol->setup();
protocol->start();
printf("*** PROTOCOL MANAGER *** - A new protocol has been started. There are %i protocols running.\n", m_protocols.size());
printf("*** PROTOCOL MANAGER *** - A new protocol has been started. There are %ld protocols running.\n", m_protocols.size());
}
void ProtocolManager::stopProtocol(Protocol* protocol)
{
}
void ProtocolManager::pauseProtocol(Protocol* protocol)
{
for (unsigned int i = 0; i < m_protocols.size(); i++)
{
if (m_protocols[i].protocol == protocol)
{
m_protocols[i].paused = true;
m_protocols[i].protocol->pause();
}
}
}
void ProtocolManager::unpauseProtocol(Protocol* protocol)
{
for (unsigned int i = 0; i < m_protocols.size(); i++)
{
if (m_protocols[i].protocol == protocol && m_protocols[i].paused == true)
{
m_protocols[i].paused = false;
m_protocols[i].protocol->unpause();
}
}
}
void ProtocolManager::protocolTerminated(Protocol* protocol)
{
int offset = 0;
for (unsigned int i = 0; i < m_protocols.size(); i++)
{
if (m_protocols[i-offset] == protocol)
if (m_protocols[i-offset].protocol == protocol)
{
delete m_protocols[i];
delete m_protocols[i].protocol;
m_protocols.erase(m_protocols.begin()+(i-offset), m_protocols.begin()+(i-offset)+1);
offset++;
}
}
printf("*** PROTOCOL MANAGER *** - A protocol has been terminated. There are %i protocols running.\n", m_protocols.size());
printf("*** PROTOCOL MANAGER *** - A protocol has been terminated. There are %ld protocols running.\n", m_protocols.size());
}
void ProtocolManager::update()
@ -57,15 +82,16 @@ void ProtocolManager::update()
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);
if (m_protocols[i].protocol->getProtocolType() == searchedProtocol) // pass data to them even when paused
m_protocols[i].protocol->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();
if (m_protocols[i].paused == false)
m_protocols[i].protocol->update();
}
}

View File

@ -8,6 +8,11 @@ class Protocol;
class ProtocolManager
{
typedef struct
{
bool paused;
Protocol* protocol;
} ProtocolInfo;
public:
ProtocolManager();
virtual ~ProtocolManager();
@ -16,6 +21,8 @@ class ProtocolManager
virtual void runProtocol(Protocol* protocol);
virtual void stopProtocol(Protocol* protocol);
virtual void pauseProtocol(Protocol* protocol);
virtual void unpauseProtocol(Protocol* protocol);
virtual void protocolTerminated(Protocol* protocol);
virtual void update();
@ -23,7 +30,7 @@ class ProtocolManager
virtual int runningProtocolsCount();
protected:
std::vector<Protocol*> m_protocols;
std::vector<ProtocolInfo> m_protocols;
std::vector<uint8_t*> m_messagesToProcess;
};

View File

@ -3,6 +3,7 @@
#include "../http_functions.hpp"
#include <stdio.h>
#include <iostream>
ConnectToServer::ConnectToServer(CallbackObject* callbackObject) : Protocol(callbackObject)
{
@ -25,13 +26,21 @@ void ConnectToServer::messageReceived(uint8_t* data)
void ConnectToServer::start()
{
if (m_ownPublicIp == 0 || m_ownPublicPort == 0 || m_username == "" || m_password == "")
if (m_ownPublicIp == 0 || m_ownPublicPort == 0 || m_username == "" || m_password == "" || m_hostName == "")
{
printf("You have to set the public ip:port and username:password before starting this protocol.\n");
printf("You have to set the public ip:port, username:password and the host nickname before starting this protocol.\n");
m_listener->protocolTerminated(this);
}
}
void ConnectToServer::pause()
{
}
void ConnectToServer::unpause()
{
}
void ConnectToServer::update()
{
if (m_state == NOTHING)
@ -39,20 +48,22 @@ void ConnectToServer::update()
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);
//printf("Result from web page is %s\n", result.c_str());
if (result[0] == 's' && result[1] == 'u' && result[2] == 'c')
std::cout << "size of answer : " << result.size() << std::endl;
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')
if (result[0] == 'f' && result[1] == 'a' && result[2] == 'i' && result[3] == 'l')
{
printf("Login fail.\n");
m_state = ADDRESS_KNOWN_ONLINE;
printf("Login fail. Please re-set username:password and restart the protocol.\n");
m_state = NOTHING;
m_listener->pauseProtocol(this);
}
}
else if (m_state == ADDRESS_KNOWN_ONLINE)
{
}
else if (m_state == PEER_ADDRESS_RETREIVED)
{
@ -78,3 +89,8 @@ void ConnectToServer::setPassword(std::string password)
{
m_password = password;
}
void ConnectToServer::setHostName(std::string hostName)
{
m_hostName = hostName;
}

View File

@ -13,15 +13,19 @@ class ConnectToServer : public Protocol, public CallbackObject
virtual void messageReceived(uint8_t* data);
virtual void setup();
virtual void start();
virtual void pause();
virtual void unpause();
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);
protected:
uint32_t m_ownPublicIp;
uint16_t m_ownPublicPort;
std::string m_hostName;
std::string m_username;
std::string m_password;
enum STATE
@ -32,6 +36,7 @@ class ConnectToServer : public Protocol, public CallbackObject
CONNECTED
};
STATE m_state;
double firstTime;
};
#endif // CONNECT_TO_SERVER_HPP

View File

@ -41,6 +41,14 @@ void GetPublicAddress::setup()
void GetPublicAddress::start()
{
}
void GetPublicAddress::pause()
{
}
void GetPublicAddress::unpause()
{
}
void GetPublicAddress::update()
{

View File

@ -13,6 +13,8 @@ class GetPublicAddress : public Protocol
virtual void setup();
virtual void start();
virtual void pause();
virtual void unpause();
virtual void update();
protected:
@ -24,7 +26,7 @@ class GetPublicAddress : public Protocol
};
STATE m_state;
uint32_t m_stunTransactionID[3];
const uint32_t m_stunMagicCookie = 0x2112A442;
static const uint32_t m_stunMagicCookie = 0x2112A442;
};
#endif // GET_PUBLIC_ADDRESS_HPP

19
dev/SocketsBase/time.hpp Normal file
View File

@ -0,0 +1,19 @@
#ifndef TIME_HPP_INCLUDED
#define TIME_HPP_INCLUDED
#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
}
}
#endif // TIME_HPP_INCLUDED