2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2016,2017,2018 by the GROMACS development team.
5 * Copyright (c) 2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
40 * Implements functions of imdsocket.h.
42 * This file re-implements vmdsock.c functions from original IMD API from scratch,
43 * see imdsocket.h for references to the IMD API.
45 * \author Martin Hoefling, Carsten Kutzner <ckutzne@gwdg.de>
52 #include "imdsocket.h"
60 #include "gromacs/imd/imd.h"
61 #include "gromacs/utility/fatalerror.h"
62 #include "gromacs/utility/smalloc.h"
66 # if GMX_NATIVE_WINDOWS
71 //! Constant for passing no flags
72 constexpr int c_noFlags
= 0;
73 /*! \brief Define socklen type on Windows. */
74 typedef int socklen_t
;
79 # include <netinet/in.h>
80 # include <sys/select.h>
81 # include <sys/socket.h>
82 # include <sys/time.h>
94 * IMD (interactive molecular dynamics) socket structure
100 struct sockaddr_in address
; /**< address of socket */
101 int sockfd
; /**< socket file descriptor */
105 /*! \brief Define a function to initialize winsock. */
106 int imdsock_winsockinit()
108 #if GMX_IMD && GMX_NATIVE_WINDOWS
109 fprintf(stderr
, "%s Initializing winsock.\n", IMDstr
);
114 /* We use winsock 1.1 compatibility for now. Though I guess no one will try on Windows 95. */
115 ret
= WSAStartup(MAKEWORD(1, 1), &wsd
);
123 /*! \brief Simple error handling. */
124 #if GMX_NATIVE_WINDOWS
125 # define ERR_ARGS __FILE__, __LINE__, NULL
127 # define ERR_ARGS __FILE__, __LINE__, strerror(errno)
131 /*! \brief Currently only 1 client connection is supported. */
132 constexpr int c_maxConnections
= 1;
135 /*! \brief Print a nice error message on UNIX systems, using errno.h. */
136 static void print_IMD_error(const char* file
, int line
, char* msg
)
138 fprintf(stderr
, "%s Error in file %s on line %d.\n", IMDstr
, file
, line
);
142 fprintf(stderr
, "%s\n", msg
);
147 IMDSocket
* imdsock_create()
149 IMDSocket
* sock
= nullptr;
154 /* Try to create socket: */
155 if ((sock
->sockfd
= socket(PF_INET
, SOCK_STREAM
, 0)) == -1)
157 print_IMD_error(ERR_ARGS
);
169 void imd_sleep(unsigned int seconds
)
172 # if GMX_NATIVE_WINDOWS
178 GMX_UNUSED_VALUE(seconds
);
183 int imdsock_bind(IMDSocket
* sock
, int port
)
189 memset(&(sock
->address
), 0, sizeof(sock
->address
));
190 sock
->address
.sin_family
= PF_INET
;
191 sock
->address
.sin_port
= htons(port
);
193 /* Try to bind to address and port ...*/
194 ret
= bind(sock
->sockfd
, reinterpret_cast<struct sockaddr
*>(&sock
->address
), sizeof(sock
->address
));
196 GMX_UNUSED_VALUE(port
);
197 GMX_UNUSED_VALUE(sock
);
203 print_IMD_error(ERR_ARGS
);
210 int imd_sock_listen(IMDSocket
* sock
)
216 /* Try to set to listening state */
217 ret
= listen(sock
->sockfd
, c_maxConnections
);
219 GMX_UNUSED_VALUE(c_maxConnections
);
220 GMX_UNUSED_VALUE(sock
);
226 print_IMD_error(ERR_ARGS
);
233 IMDSocket
* imdsock_accept(IMDSocket
* sock
)
237 socklen_t length
= sizeof(sock
->address
);
238 int ret
= accept(sock
->sockfd
, reinterpret_cast<struct sockaddr
*>(&sock
->address
), &length
);
240 /* successful, redirect to distinct clientsocket */
246 newsock
->address
= sock
->address
;
247 newsock
->sockfd
= ret
;
253 GMX_UNUSED_VALUE(sock
);
256 print_IMD_error(ERR_ARGS
);
263 int imdsock_getport(IMDSocket
* sock
, int* port
)
270 len
= sizeof(struct sockaddr_in
);
271 ret
= getsockname(sock
->sockfd
, reinterpret_cast<struct sockaddr
*>(&(sock
->address
)), &len
);
274 fprintf(stderr
, "%s getsockname failed with error %d.\n", IMDstr
, ret
);
275 print_IMD_error(ERR_ARGS
);
279 *port
= ntohs(sock
->address
.sin_port
);
282 GMX_UNUSED_VALUE(port
);
283 GMX_UNUSED_VALUE(sock
);
284 gmx_incons("imdsock_getport called without IMD support.");
292 int imd_htonl(int src
)
301 int imd_ntohl(int src
)
310 int imdsock_write(IMDSocket
* sock
, const char* buffer
, int length
)
313 /* No read and write on windows, we have to use send and recv instead... */
314 # if GMX_NATIVE_WINDOWS
315 return send(sock
->sockfd
, (const char*)buffer
, length
, c_noFlags
);
317 return write(sock
->sockfd
, buffer
, length
);
320 GMX_UNUSED_VALUE(buffer
);
321 GMX_UNUSED_VALUE(length
);
322 GMX_UNUSED_VALUE(sock
);
328 int imdsock_read(IMDSocket
* sock
, char* buffer
, int length
)
332 # if GMX_NATIVE_WINDOWS
333 return recv(sock
->sockfd
, (char*)buffer
, length
, c_noFlags
);
335 return read(sock
->sockfd
, buffer
, length
);
338 GMX_UNUSED_VALUE(buffer
);
339 GMX_UNUSED_VALUE(length
);
340 GMX_UNUSED_VALUE(sock
);
346 void imdsock_shutdown(IMDSocket
* sock
)
351 /* is the socket already NULL? */
358 /* If not, try to properly shut down. */
359 ret
= shutdown(sock
->sockfd
, 1);
364 fprintf(stderr
, "%s Failed to shutdown socket. Did the client already disconnect?\n", IMDstr
);
365 print_IMD_error(ERR_ARGS
);
370 int imdsock_destroy(IMDSocket
* sock
)
381 # if GMX_NATIVE_WINDOWS
382 ret
= closesocket(sock
->sockfd
);
384 ret
= close(sock
->sockfd
);
391 print_IMD_error(ERR_ARGS
);
400 int imdsock_tryread(IMDSocket
* sock
, int timeoutsec
, int timeoutusec
)
407 /* Create new time structure with sec and usec. */
408 struct timeval
* tval
;
415 /* add the socket to the read set */
416 FD_SET(sock
->sockfd
, &readfds
);
418 /* set the timeout */
419 tval
->tv_sec
= timeoutsec
;
420 tval
->tv_usec
= timeoutusec
;
423 /* check the set for read readiness. */
424 ret
= select(sock
->sockfd
+ 1, &readfds
, nullptr, nullptr, tval
);
425 /* 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
);