Skip to content
Snippets Groups Projects
Commit ee807822 authored by Christophe Geuzaine's avatar Christophe Geuzaine
Browse files

*** empty log message ***

parent c7146c10
No related branches found
No related tags found
No related merge requests found
...@@ -106,12 +106,8 @@ class GmshSocket{ ...@@ -106,12 +106,8 @@ class GmshSocket{
GmshSocket() : _sock(0), _sockname(0) GmshSocket() : _sock(0), _sockname(0)
{ {
#if defined(WIN32) && !defined(__CYGWIN__) #if defined(WIN32) && !defined(__CYGWIN__)
static bool first = true; WSADATA wsaData;
if(first){ WSAStartup(MAKEWORD(2, 2), &wsaData);
first = false;
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
}
#endif #endif
} }
~GmshSocket() ~GmshSocket()
...@@ -171,6 +167,14 @@ class GmshSocket{ ...@@ -171,6 +167,14 @@ class GmshSocket{
} }
return 0; return 0;
} }
void CloseSocket(int s)
{
#if !defined(WIN32) || defined(__CYGWIN__)
close(s);
#else
closesocket(s);
#endif
}
}; };
class GmshClient : public GmshSocket { class GmshClient : public GmshSocket {
...@@ -217,8 +221,10 @@ class GmshClient : public GmshSocket { ...@@ -217,8 +221,10 @@ class GmshClient : public GmshSocket {
strncpy(remote, sockname, remotelen); strncpy(remote, sockname, remotelen);
remote[remotelen] = '\0'; remote[remotelen] = '\0';
struct hostent *server; struct hostent *server;
if(!(server = gethostbyname(remote))) if(!(server = gethostbyname(remote))){
CloseSocket(_sock);
return -3; // Error: No such host return -3; // Error: No such host
}
struct sockaddr_in addr_in; struct sockaddr_in addr_in;
memset((char *) &addr_in, 0, sizeof(addr_in)); memset((char *) &addr_in, 0, sizeof(addr_in));
addr_in.sin_family = AF_INET; addr_in.sin_family = AF_INET;
...@@ -230,6 +236,7 @@ class GmshClient : public GmshSocket { ...@@ -230,6 +236,7 @@ class GmshClient : public GmshSocket {
_Sleep(100); _Sleep(100);
} }
} }
CloseSocket(_sock);
return -2; // Error: Couldn't connect return -2; // Error: Couldn't connect
} }
void Start() void Start()
...@@ -242,54 +249,23 @@ class GmshClient : public GmshSocket { ...@@ -242,54 +249,23 @@ class GmshClient : public GmshSocket {
#endif #endif
SendString(CLIENT_START, tmp); SendString(CLIENT_START, tmp);
} }
void Stop() void Stop(){ SendString(CLIENT_STOP, "Goodbye!"); }
{ void Info(const char *str){ SendString(CLIENT_INFO, str); }
SendString(CLIENT_STOP, "Goodbye!"); void Warning(const char *str){ SendString(CLIENT_WARNING, str); }
} void Error(const char *str){ SendString(CLIENT_ERROR, str); }
void Info(const char *str) void Progress(const char *str){ SendString(CLIENT_PROGRESS, str); }
{ // deprecated: use MergeFile instead
SendString(CLIENT_INFO, str); void View(const char *str){ SendString(CLIENT_MERGE_FILE, str); }
} void MergeFile(const char *str){ SendString(CLIENT_MERGE_FILE, str); }
void Warning(const char *str) void ParseString(const char *str){ SendString(CLIENT_PARSE_STRING, str); }
{ void SpeedTest(const char *str){ SendString(CLIENT_SPEED_TEST, str); }
SendString(CLIENT_WARNING, str);
}
void Error(const char *str)
{
SendString(CLIENT_ERROR, str);
}
void Progress(const char *str)
{
SendString(CLIENT_PROGRESS, str);
}
void View(const char *str)
{
// deprecated: use MergeFile(str) instead
SendString(CLIENT_MERGE_FILE, str);
}
void MergeFile(const char *str)
{
SendString(CLIENT_MERGE_FILE, str);
}
void ParseString(const char *str)
{
SendString(CLIENT_PARSE_STRING, str);
}
void Option(int num, const char *str) void Option(int num, const char *str)
{ {
if(num < 1) num = 1; if(num < 1) num = 1;
if(num > 5) num = 5; if(num > 5) num = 5;
SendString(CLIENT_OPTION_1 + num - 1, str); SendString(CLIENT_OPTION_1 + num - 1, str);
} }
void Disconnect() void Disconnect(){ CloseSocket(_sock); }
{
#if !defined(WIN32) || defined(__CYGWIN__)
close(_sock);
#else
closesocket(_sock);
#endif
}
}; };
class GmshServer : public GmshSocket{ class GmshServer : public GmshSocket{
...@@ -302,7 +278,7 @@ class GmshServer : public GmshSocket{ ...@@ -302,7 +278,7 @@ class GmshServer : public GmshSocket{
virtual int NonBlockingWait(int socket, int num, double waitint) = 0; virtual int NonBlockingWait(int socket, int num, double waitint) = 0;
int StartClient(const char *command, const char *sockname=0, int maxdelay=4) int StartClient(const char *command, const char *sockname=0, int maxdelay=4)
{ {
int justwait = (!command || !strlen(command)); bool justwait = (!command || !strlen(command));
_sockname = sockname; _sockname = sockname;
// no socket? launch the command directly // no socket? launch the command directly
...@@ -353,8 +329,10 @@ class GmshServer : public GmshSocket{ ...@@ -353,8 +329,10 @@ class GmshServer : public GmshSocket{
addr_in.sin_family = AF_INET; addr_in.sin_family = AF_INET;
addr_in.sin_addr.s_addr = INADDR_ANY; addr_in.sin_addr.s_addr = INADDR_ANY;
addr_in.sin_port = htons(_portno); addr_in.sin_port = htons(_portno);
if(bind(tmpsock, (struct sockaddr *)&addr_in, sizeof(addr_in)) < 0) if(bind(tmpsock, (struct sockaddr *)&addr_in, sizeof(addr_in)) < 0){
CloseSocket(tmpsock);
return -2; // Error: Couldn't bind socket to name return -2; // Error: Couldn't bind socket to name
}
} }
if(!justwait) if(!justwait)
...@@ -362,19 +340,25 @@ class GmshServer : public GmshSocket{ ...@@ -362,19 +340,25 @@ class GmshServer : public GmshSocket{
// listen on socket (queue up to 20 connections before having // listen on socket (queue up to 20 connections before having
// them automatically rejected) // them automatically rejected)
if(listen(tmpsock, 20)) if(listen(tmpsock, 20)){
CloseSocket(tmpsock);
return -3; // Error: Socket listen failed return -3; // Error: Socket listen failed
}
if(justwait){ if(justwait){
// wait indefinitely until we get data // wait indefinitely until we get data
if(NonBlockingWait(tmpsock, -1, 0.5)) if(NonBlockingWait(tmpsock, -1, 0.5)){
CloseSocket(tmpsock);
return -6; // not an actual error: we just stopped listening return -6; // not an actual error: we just stopped listening
}
} }
else{ else{
// Wait at most maxdelay seconds for data, issue error if no // Wait at most maxdelay seconds for data, issue error if no
// connection in that amount of time // connection in that amount of time
if(!Select(tmpsock, maxdelay)) if(!Select(tmpsock, maxdelay)){
CloseSocket(tmpsock);
return -4; // Error: Socket listening timeout return -4; // Error: Socket listening timeout
}
} }
// accept connection request // accept connection request
...@@ -397,14 +381,10 @@ class GmshServer : public GmshSocket{ ...@@ -397,14 +381,10 @@ class GmshServer : public GmshSocket{
len = sizeof(from_in); len = sizeof(from_in);
_sock = accept(tmpsock, (struct sockaddr *)&from_in, &len); _sock = accept(tmpsock, (struct sockaddr *)&from_in, &len);
} }
if(_sock < 0) return -5; // Error: Socket accept failed CloseSocket(tmpsock);
// close temporary socket if(_sock < 0)
#if !defined(WIN32) || defined(__CYGWIN__) return -5; // Error: Socket accept failed
close(tmpsock);
#else
closesocket(tmpsock);
#endif
return _sock; return _sock;
} }
int StopClient() int StopClient()
...@@ -412,10 +392,8 @@ class GmshServer : public GmshSocket{ ...@@ -412,10 +392,8 @@ class GmshServer : public GmshSocket{
#if !defined(WIN32) || defined(__CYGWIN__) #if !defined(WIN32) || defined(__CYGWIN__)
if(_portno < 0) if(_portno < 0)
unlink(_sockname); unlink(_sockname);
close(_sock);
#else
closesocket(_sock);
#endif #endif
CloseSocket(_sock);
return 0; return 0;
} }
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment