77 lines
3.4 KiB
C++
77 lines
3.4 KiB
C++
// CashWorld Client Program
|
||
#include <cstdio>
|
||
#include <cstdlib>
|
||
#include <cstring>
|
||
#include <string>
|
||
#include <sstream>
|
||
#include <ctime>
|
||
#include "GSock/gsock.h"
|
||
#include "GSock/gsock_helper.h"
|
||
|
||
using namespace std;
|
||
|
||
const string SERVER_IP = "127.0.0.1";
|
||
const int PORT_CHECK = 59401;
|
||
const int PORT_LOGIN = 59402;
|
||
|
||
char _xtime[256];
|
||
|
||
const char* _update_xtime()
|
||
{
|
||
time_t t = time(NULL);
|
||
strftime(_xtime, 256, "%Y-%m-%d %H:%M:%S", localtime(&t));
|
||
return _xtime;
|
||
}
|
||
|
||
FILE* _log_f = NULL;
|
||
#define xlog(fmt,...) _update_xtime();fprintf(_log_f,"<%s | %s> " fmt "\n",_xtime,__func__,##__VA_ARGS__);fflush(_log_f)
|
||
#define start_xlog() _log_f=fopen("log.txt","a");fprintf(_log_f,"==============================\n");fflush(_log_f)
|
||
|
||
int update_check()
|
||
{
|
||
sock s;
|
||
if (s.connect(SERVER_IP, PORT_CHECK) < 0)
|
||
{
|
||
xlog("Cannot connect to server");
|
||
return -1;
|
||
}
|
||
sock_helper sh(s);
|
||
string response;
|
||
if (sh.sendline("get_current_version") <= 0 ||
|
||
sh.recvline(response)<0 )
|
||
{
|
||
xlog("Failed to querying version");
|
||
return -2;
|
||
}
|
||
istringstream istr(response);
|
||
string code, version;
|
||
if (!(istr >> code >> version))
|
||
{
|
||
xlog("Failed to parse version code. Response: %s", response.c_str());
|
||
return -3;
|
||
}
|
||
if (code != "current_version")
|
||
{
|
||
xlog("Wrong response header. Header: %s", code.c_str());
|
||
return -4;
|
||
}
|
||
|
||
xlog("Server response latest version is: %s", version);
|
||
return 0;
|
||
}
|
||
|
||
int user_login()
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
int main()
|
||
{
|
||
start_xlog();
|
||
xlog("Program Started");
|
||
|
||
xlog("Program Finished Successfully.");
|
||
return 0;
|
||
}
|
||
|