28 #include "GmshConfig.h"
40 #if !defined(WIN32) || defined(__CYGWIN__)
42 #include <sys/types.h>
43 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <netinet/tcp.h>
50 #if defined(HAVE_NO_SOCKLEN_T)
51 typedef int socklen_t;
56 typedef int socklen_t;
106 const char *buf = (
const char *)buffer;
108 long int remaining = bytes;
110 long int len = send(
_sock, buf + sofar, remaining, 0);
111 if(len < 0)
return -1;
114 }
while(remaining > 0);
121 char *buf = (
char *)buffer;
123 long int remaining = bytes;
125 long int len = recv(
_sock, buf + sofar, remaining, 0);
127 if(len < 0)
return -1;
130 }
while(remaining > 0);
137 char *x =
new char[size];
138 for(
int i = 0; i < n; i++) {
139 char *a = &
array[i * size];
141 for(
int c = 0;
c < size;
c++) a[size - 1 -
c] = x[
c];
148 #if !defined(WIN32) || defined(__CYGWIN__)
158 #if defined(WIN32) && !defined(__CYGWIN__)
160 WSAStartup(MAKEWORD(2, 2), &wsaData);
165 #if defined(WIN32) && !defined(__CYGWIN__)
173 int Select(
int seconds,
int microseconds,
int socket = -1)
175 int s = (socket < 0) ?
_sock : socket;
178 tv.tv_usec = microseconds;
184 return select(s + 1, &rfds,
nullptr,
nullptr, &tv);
245 #if !defined(WIN32) || defined(__CYGWIN__)
253 #if !defined(WIN32) || defined(__CYGWIN__)
254 shutdown(s, SHUT_RDWR);
267 if(strstr(sockname,
"/") || strstr(sockname,
"\\") ||
268 !strstr(sockname,
":")) {
269 #if !defined(WIN32) || defined(__CYGWIN__)
271 _sock = socket(PF_UNIX, SOCK_STREAM, 0);
272 if(
_sock < 0)
return -1;
274 struct sockaddr_un addr_un;
275 memset((
char *)&addr_un, 0,
sizeof(addr_un));
276 addr_un.sun_family = AF_UNIX;
277 strcpy(addr_un.sun_path, sockname);
278 for(
int tries = 0; tries < 5; tries++) {
279 if(connect(
_sock, (
struct sockaddr *)&addr_un,
sizeof(addr_un)) >= 0)
289 _sock = socket(AF_INET, SOCK_STREAM, 0);
290 if(
_sock < 0)
return -1;
293 setsockopt(
_sock, IPPROTO_TCP, TCP_NODELAY, &one,
sizeof(one));
295 const char *port = strstr(sockname,
":");
296 int portno = atoi(port + 1);
297 char *remote = strdup(sockname);
298 int remotelen = (int)(strlen(remote) - strlen(port));
299 if(remotelen > 0) strncpy(remote, sockname, remotelen);
300 if(remotelen >= 0) remote[remotelen] =
'\0';
301 struct hostent *server;
302 if(!(server = gethostbyname(remote))) {
308 struct sockaddr_in addr_in;
309 memset((
char *)&addr_in, 0,
sizeof(addr_in));
310 addr_in.sin_family = AF_INET;
311 memcpy((
char *)&addr_in.sin_addr.s_addr, (
char *)server->h_addr,
313 addr_in.sin_port = htons(portno);
314 for(
int tries = 0; tries < 5; tries++) {
315 if(connect(
_sock, (
struct sockaddr *)&addr_in,
sizeof(addr_in)) >= 0) {
327 #if !defined(WIN32) || defined(__CYGWIN__)
328 sprintf(tmp,
"%d", getpid());
330 sprintf(tmp,
"%d", _getpid());
346 const std::string &args) = 0;
348 int socket = -1) = 0;
351 int Start(
const std::string &exe,
const std::string &args,
352 const std::string &sockname,
double timeout)
360 #if !defined(WIN32) || defined(__CYGWIN__)
364 tmpsock = socket(PF_UNIX, SOCK_STREAM, 0);
365 if(tmpsock < 0)
throw std::runtime_error(
"Couldn't create socket");
367 struct sockaddr_un addr_un;
368 memset((
char *)&addr_un, 0,
sizeof(addr_un));
369 strcpy(addr_un.sun_path,
_sockname.c_str());
370 addr_un.sun_family = AF_UNIX;
371 if(bind(tmpsock, (
struct sockaddr *)&addr_un,
sizeof(addr_un)) < 0) {
373 throw std::runtime_error(
"Couldn't bind socket to name");
378 throw std::runtime_error(
"Unix sockets not available on Windows");
385 const char *port = strstr(
_sockname.c_str(),
":");
388 tmpsock = socket(AF_INET, SOCK_STREAM, 0);
390 setsockopt(tmpsock, IPPROTO_TCP, TCP_NODELAY, &one,
sizeof(one));
392 #if !defined(WIN32) || defined(__CYGWIN__)
395 if(tmpsock == (
int)INVALID_SOCKET)
397 throw std::runtime_error(
"Couldn't create socket");
399 struct sockaddr_in addr_in;
400 memset((
char *)&addr_in, 0,
sizeof(addr_in));
401 addr_in.sin_family = AF_INET;
402 addr_in.sin_addr.s_addr = INADDR_ANY;
403 addr_in.sin_port = htons(
_portno);
404 if(bind(tmpsock, (
struct sockaddr *)&addr_in,
sizeof(addr_in)) < 0) {
406 throw std::runtime_error(
"Couldn't bind socket to name");
409 socklen_t addrlen =
sizeof(addr_in);
410 getsockname(tmpsock, (
struct sockaddr *)&addr_in, &addrlen);
411 _portno = ntohs(addr_in.sin_port);
419 if(exe.size() || args.size()) {
421 sprintf(s, args.c_str(),
_sockname.c_str());
430 if(listen(tmpsock, 20)) {
432 throw std::runtime_error(
"Socket listen failed");
439 if(ret == 2) {
throw std::runtime_error(
"Socket listening timeout"); }
447 #if !defined(WIN32) || defined(__CYGWIN__)
448 struct sockaddr_un from_un;
449 socklen_t len =
sizeof(from_un);
450 _sock = accept(tmpsock, (
struct sockaddr *)&from_un, &len);
454 struct sockaddr_in from_in;
455 socklen_t len =
sizeof(from_in);
456 _sock = accept(tmpsock, (
struct sockaddr *)&from_in, &len);
458 setsockopt(
_sock, IPPROTO_TCP, TCP_NODELAY, &one,
sizeof(one));
461 if(
_sock < 0)
throw std::runtime_error(
"Socket accept failed");
466 #if !defined(WIN32) || defined(__CYGWIN__)