Skip to content

Commit

Permalink
Server fix/improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanLucPons committed May 14, 2020
1 parent 50e393a commit dd5a68d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 33 deletions.
1 change: 1 addition & 0 deletions Kangaroo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Kangaroo::Kangaroo(Secp256K1 *secp,int32_t initDPSize,bool useGpu,string &workFi
this->clientMode = serverIp.length()>0;
this->endOfSearch = false;
this->saveRequest = false;
this->connectedClient = 0;

CPU_GRP_SIZE = 1024;

Expand Down
5 changes: 4 additions & 1 deletion Kangaroo.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ class Kangaroo {
bool HandleRequest(TH_PARAM *p);
void ProcessServer();

void AddConnectedClient();
void RemoveConnectedClient();

private:

bool IsDP(uint64_t x);
Expand Down Expand Up @@ -236,10 +239,10 @@ class Kangaroo {
bool clientMode;
bool isConnected;
SOCKET serverConn;
std::vector<TH_PARAM *> clients;
std::vector<DP_CACHE> recvDP;
std::vector<DP_CACHE> localCache;
std::string serverStatus;
int connectedClient;

};

Expand Down
84 changes: 61 additions & 23 deletions Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
#define _USE_MATH_DEFINES
#include <math.h>
#include <algorithm>
#include <signal.h>
#ifndef WIN64
#include <pthread.h>
#else
#include "WindowsErrors.h"
#endif
#include <signal.h>

using namespace std;

Expand Down Expand Up @@ -61,18 +63,21 @@ typedef int socklen_t;

string GetNetworkError() {

char msgbuf[512]; // for a message up to 512 bytes.
msgbuf[0] = '\0'; // Microsoft doesn't guarantee this on man page.

FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, // flags
NULL, // lpsource
WSAGetLastError(), // message id
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // languageid
msgbuf, // output buffer
sizeof(msgbuf), // size of msgbuf, bytes
NULL); // va_list of arguments
int err = WSAGetLastError();
bool found = false;
int i=0;
while(!found && i<NBWSAERRORS) {
found = (WSAERRORS[i].errCode == err);
if(!found) i++;
}

return string(msgbuf);
if(!found) {
char ret[256];
sprintf(ret,"WSA Error code %d",err);
return string(ret);
} else {
return string(WSAERRORS[i].mesg);
}

}

Expand All @@ -95,7 +100,7 @@ string GetNetworkError() {

void sig_handler(int signo) {
if(signo == SIGINT) {
::printf("Terminated\n");
::printf("\nTerminated\n");
if(serverSock>0) close_socket(serverSock);
#ifdef WIN64
WSACleanup();
Expand Down Expand Up @@ -123,7 +128,11 @@ int Kangaroo::WaitFor(SOCKET sock,int timeout,int mode) {

do
result = select((int)sock + 1,rd,wr,NULL,&tmout);
#ifdef WIN64
while(result < 0 && WSAGetLastError() == WSAEINTR);
#else
while(result < 0 && errno == EINTR);
#endif

if(result == 0) {
lastError = "The operation timed out";
Expand All @@ -150,7 +159,11 @@ int Kangaroo::Write(SOCKET sock,char *buf,int bufsize,int timeout) {
// Write
do
written = send(sock,buf,bufsize,0);
#ifdef WIN64
while(written == -1 && WSAGetLastError() == WSAEINTR);
#else
while(written == -1 && errno == EINTR);
#endif

if(written <= 0)
break;
Expand Down Expand Up @@ -189,8 +202,11 @@ int Kangaroo::Read(SOCKET sock,char *buf,int bufsize,int timeout) { // Timeout i
// Read
do
rd = recv(sock,buf,bufsize,0);
#ifdef WIN64
while(rd == -1 && WSAGetLastError() == WSAEINTR);
#else
while(rd == -1 && errno == EINTR);

#endif
if( rd <= 0 )
break;

Expand Down Expand Up @@ -342,8 +358,12 @@ DWORD WINAPI _acceptThread(LPVOID lpParam) {
void *_acceptThread(void *lpParam) {
#endif
TH_PARAM *p = (TH_PARAM *)lpParam;
p->obj->AddConnectedClient();
p->obj->HandleRequest(p);
p->obj->RemoveConnectedClient();
p->isRunning = false;
free(p->clientInfo);
free(p);
return 0;
}

Expand Down Expand Up @@ -378,11 +398,14 @@ void Kangaroo::AcceptConnections(SOCKET server_soc) {
TH_PARAM *p = (TH_PARAM *)malloc(sizeof(TH_PARAM));
char info[256];
::sprintf(info,"%s:%d",inet_ntoa(client_add.sin_addr),ntohs(client_add.sin_port));
#ifdef WIN64
p->clientInfo = ::_strdup(info);
#else
p->clientInfo = ::strdup(info);
#endif
p->obj = this;
p->isRunning = true;
p->clientSock = clientSock;
clients.push_back(p);
LaunchThread(_acceptThread,p);

}
Expand Down Expand Up @@ -477,7 +500,7 @@ void Kangaroo::RunServer() {
// Client part
// ------------------------------------------------------------------------------------------------------

// Connection to server
// Connection to the server
bool Kangaroo::ConnectToServer(SOCKET *retSock) {

lastError = "";
Expand Down Expand Up @@ -535,7 +558,11 @@ bool Kangaroo::ConnectToServer(SOCKET *retSock) {

int connectStatus = connect(sock,(struct sockaddr *)&server,sizeof(server));

#ifdef WIN64
if((connectStatus < 0) && (WSAGetLastError() != WSAEINPROGRESS)) {
#else
if((connectStatus < 0) && (errno != EINPROGRESS)) {
#endif
lastError = "Cannot connect to host: " + GetNetworkError();
close_socket(sock);
return false;
Expand Down Expand Up @@ -594,10 +621,11 @@ bool Kangaroo::ConnectToServer(SOCKET *retSock) {

}

// Wait while server is not running
// Wait while server is not ready
void Kangaroo::WaitForServer() {

int nbRead;
int nbWrite;
int32_t status;
bool ok = false;

Expand All @@ -615,19 +643,21 @@ void Kangaroo::WaitForServer() {
while(isConnected && !ok) {

char cmd = SERVER_STATUS;
nbWrite = Write(serverConn,&cmd,1,ntimeout);
if( nbWrite<=0 ) {

if( Write(serverConn,&cmd,1,ntimeout)<=0 ) {

::printf("\nSendToServer(Status): %s\n",lastError.c_str());
if(nbWrite<0)
::printf("\nSendToServer(Status): %s\n",lastError.c_str());
serverStatus = "Not OK";
close_socket(serverConn);
isConnected = false;

} else {

nbRead = Read(serverConn,(char *)(&status),sizeof(int32_t),ntimeout);
if( nbRead<0 ) {
::printf("\nRecvFromServer(Status): %s\n",lastError.c_str());
if( nbRead<=0 ) {
if(nbRead<0)
::printf("\nRecvFromServer(Status): %s\n",lastError.c_str());
serverStatus = "Fault";
close_socket(serverConn);
isConnected = false;
Expand Down Expand Up @@ -661,7 +691,7 @@ void Kangaroo::WaitForServer() {

}

// Send DP to
// Send DP to Server
bool Kangaroo::SendToServer(std::vector<ITEM> &dps) {

int nbRead;
Expand Down Expand Up @@ -710,6 +740,14 @@ bool Kangaroo::SendToServer(std::vector<ITEM> &dps) {

}

void Kangaroo::AddConnectedClient() {
connectedClient++;
}

void Kangaroo::RemoveConnectedClient() {
connectedClient--;
}

// Get configuration from server
bool Kangaroo::GetConfigFromServer() {

Expand Down
11 changes: 2 additions & 9 deletions Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ void Kangaroo::ProcessServer() {

t0 = Timer::get_tick();

// Get back all dps
LOCK(ghMutex);
localCache.clear();
// Get back all dps
localCache.clear();
for(int i=0;i<(int)recvDP.size();i++)
localCache.push_back(recvDP[i]);
recvDP.clear();
Expand All @@ -199,13 +199,6 @@ void Kangaroo::ProcessServer() {
free(dp.dp);
}

// Number of active client
int connectedClient = 0;
for(int i=0;i<(int)clients.size();i++) {
if(clients[i]->isRunning)
connectedClient++;
}

t1 = Timer::get_tick();

double toSleep = SEND_PERIOD - (t1-t0);
Expand Down
1 change: 1 addition & 0 deletions VC_CUDA10/Kangaroo.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<ClInclude Include="..\SECPK1\SECP256k1.h" />
<ClInclude Include="..\Timer.h" />
<ClInclude Include="..\Kangaroo.h" />
<ClInclude Include="..\WindowsErrors.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\Backup.cpp" />
Expand Down
1 change: 1 addition & 0 deletions VC_CUDA10/Kangaroo.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Filter>GPU</Filter>
</ClInclude>
<ClInclude Include="..\Constants.h" />
<ClInclude Include="..\WindowsErrors.h" />
</ItemGroup>
<ItemGroup>
<Filter Include="SECPK1">
Expand Down

0 comments on commit dd5a68d

Please sign in to comment.