2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
39 * Implements functions of imdsocket.h.
41 * This file re-implements vmdsock.c functions from original IMD API from scratch,
42 * see imdsocket.h for references to the IMD API.
44 * \author Martin Hoefling, Carsten Kutzner <ckutzne@gwdg.de>
51 #include "imdsocket.h"
59 #include "gromacs/imd/imd.h"
60 #include "gromacs/utility/fatalerror.h"
61 #include "gromacs/utility/smalloc.h"
65 #if GMX_NATIVE_WINDOWS
70 //! Constant for passing no flags
71 constexpr int c_noFlags
= 0;
72 /*! \brief Define socklen type on Windows. */
73 typedef int socklen_t
;
78 #include <netinet/in.h>
79 #include <sys/select.h>
80 #include <sys/socket.h>
93 * IMD (interactive molecular dynamics) socket structure
99 struct sockaddr_in address
; /**< address of socket */
100 int sockfd
; /**< socket file descriptor */
104 /*! \brief Define a function to initialize winsock. */
105 int imdsock_winsockinit()
107 #if GMX_IMD && GMX_NATIVE_WINDOWS
108 fprintf(stderr
, "%s Initializing winsock.\n", IMDstr
);
113 /* We use winsock 1.1 compatibility for now. Though I guess no one will try on Windows 95. */
114 ret
= WSAStartup(MAKEWORD(1, 1), &wsd
);
122 /*! \brief Simple error handling. */
123 #if GMX_NATIVE_WINDOWS
124 #define ERR_ARGS __FILE__, __LINE__, NULL
126 #define ERR_ARGS __FILE__, __LINE__, strerror(errno)
130 /*! \brief Currently only 1 client connection is supported. */
131 constexpr int c_maxConnections
= 1;
134 /*! \brief Print a nice error message on UNIX systems, using errno.h. */
135 static void print_IMD_error(const char *file
, int line
, char *msg
)
137 fprintf(stderr
, "%s Error in file %s on line %d.\n", IMDstr
, file
, line
);
141 fprintf(stderr
, "%s\n", msg
);
146 IMDSocket
* imdsock_create()
148 IMDSocket
*sock
= nullptr;
153 /* Try to create socket: */
154 if ((sock
->sockfd
= socket(PF_INET
, SOCK_STREAM
, 0)) == -1)
156 print_IMD_error(ERR_ARGS
);
168 void imd_sleep(unsigned int seconds
)
171 #if GMX_NATIVE_WINDOWS
177 GMX_UNUSED_VALUE(seconds
);
182 int imdsock_bind(IMDSocket
*sock
, int port
)
188 memset(&(sock
->address
), 0, sizeof(sock
->address
));
189 sock
->address
.sin_family
= PF_INET
;
190 sock
->address
.sin_port
= htons(port
);
192 /* Try to bind to address and port ...*/
193 ret
= bind(sock
->sockfd
, reinterpret_cast<struct sockaddr
*>(&sock
->address
), sizeof(sock
->address
));
195 GMX_UNUSED_VALUE(port
);
196 GMX_UNUSED_VALUE(sock
);
202 print_IMD_error(ERR_ARGS
);
209 int imd_sock_listen(IMDSocket
*sock
)
215 /* Try to set to listening state */
216 ret
= listen(sock
->sockfd
, c_maxConnections
);
218 GMX_UNUSED_VALUE(c_maxConnections
);
219 GMX_UNUSED_VALUE(sock
);
225 print_IMD_error(ERR_ARGS
);
232 IMDSocket
* imdsock_accept(IMDSocket
*sock
)
236 socklen_t length
= sizeof(sock
->address
);
237 int ret
= accept(sock
->sockfd
, reinterpret_cast<struct sockaddr
*>(&sock
->address
), &length
);
239 /* successful, redirect to distinct clientsocket */
245 newsock
->address
= sock
->address
;
246 newsock
->sockfd
= ret
;
252 GMX_UNUSED_VALUE(sock
);
255 print_IMD_error(ERR_ARGS
);
262 int imdsock_getport(IMDSocket
*sock
, int *port
)
269 len
= sizeof(struct sockaddr_in
);
270 ret
= getsockname(sock
->sockfd
, reinterpret_cast<struct sockaddr
*>(&(sock
->address
)), &len
);
273 fprintf(stderr
, "%s getsockname failed with error %d.\n", IMDstr
, ret
);
274 print_IMD_error(ERR_ARGS
);
278 *port
= ntohs(sock
->address
.sin_port
);
281 GMX_UNUSED_VALUE(port
);
282 GMX_UNUSED_VALUE(sock
);
283 gmx_incons("imdsock_getport called without IMD support.");
291 int imd_htonl(int src
)
300 int imd_ntohl(int src
)
309 int imdsock_write(IMDSocket
*sock
, const char *buffer
, int length
)
312 /* No read and write on windows, we have to use send and recv instead... */
313 #if GMX_NATIVE_WINDOWS
314 return send(sock
->sockfd
, (const char *) buffer
, length
, c_noFlags
);
316 return write(sock
->sockfd
, buffer
, length
);
319 GMX_UNUSED_VALUE(buffer
);
320 GMX_UNUSED_VALUE(length
);
321 GMX_UNUSED_VALUE(sock
);
327 int imdsock_read(IMDSocket
*sock
, char *buffer
, int length
)
331 #if GMX_NATIVE_WINDOWS
332 return recv(sock
->sockfd
, (char *) buffer
, length
, c_noFlags
);
334 return read(sock
->sockfd
, buffer
, length
);
337 GMX_UNUSED_VALUE(buffer
);
338 GMX_UNUSED_VALUE(length
);
339 GMX_UNUSED_VALUE(sock
);
345 void imdsock_shutdown(IMDSocket
*sock
)
350 /* is the socket already NULL? */
357 /* If not, try to properly shut down. */
358 ret
= shutdown(sock
->sockfd
, 1);
363 fprintf(stderr
, "%s Failed to shutdown socket. Did the client already disconnect?\n", IMDstr
);
364 print_IMD_error(ERR_ARGS
);
369 int imdsock_destroy(IMDSocket
*sock
)
380 #if GMX_NATIVE_WINDOWS
381 ret
= closesocket(sock
->sockfd
);
383 ret
= close(sock
->sockfd
);
390 print_IMD_error(ERR_ARGS
);
399 int imdsock_tryread(IMDSocket
*sock
, int timeoutsec
, int timeoutusec
)
406 /* Create new time structure with sec and usec. */
407 struct timeval
*tval
;
414 /* add the socket to the read set */
415 FD_SET(sock
->sockfd
, &readfds
);
417 /* set the timeout */
418 tval
->tv_sec
= timeoutsec
;
419 tval
->tv_usec
= timeoutusec
;
422 /* check the set for read readiness. */
423 ret
= select(sock
->sockfd
+ 1, &readfds
, nullptr, nullptr, tval
);
424 /* redo on system interrupt */
426 while (ret
< 0 && errno
== EINTR
);
430 GMX_UNUSED_VALUE(sock
);
431 GMX_UNUSED_VALUE(timeoutsec
);
432 GMX_UNUSED_VALUE(timeoutusec
);
437 print_IMD_error(ERR_ARGS
);