d7f0ce95482b4d4ba3f0938a8f56b0adc2d6dcfc
[alexxy/gromacs.git] / src / gromacs / imd / imdsocket.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35
36 /*! \libinternal \file
37  *
38  * \brief
39  * Implements the parts of the vmdsock.h interface needed for IMD communication.
40  *
41  * \author Martin Hoefling, Carsten Kutzner <ckutzne@gwdg.de>
42  *
43  * For more information, see https://www-s.ks.uiuc.edu/Research/vmd/imd/ for general
44  * IMD information and https://www-s.ks.uiuc.edu/Research/vmd/imd/code/imdapi.tar.gz
45  * for the IMD reference implementation and API.
46  *
47  * \inlibraryapi
48  * \ingroup module_imd
49  */
50
51 #ifndef GMX_IMD_IMDSOCKET_H
52 #define GMX_IMD_IMDSOCKET_H
53
54 #include "config.h"
55
56 /* Check if we can/should use winsock or standard UNIX sockets. */
57 #ifdef GMX_NATIVE_WINDOWS
58   #ifdef GMX_HAVE_WINSOCK
59   #include <Winsock.h>
60   #define GMX_IMD
61   #endif
62 #else
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #define GMX_IMD
66 #endif
67
68
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72
73
74
75 /*! \internal
76  *
77  * \brief
78  * IMD (interactive molecular dynamics) socket structure
79  *
80  */
81 typedef struct
82 {
83 #ifdef GMX_IMD
84     struct sockaddr_in address;      /**< address of socket                   */
85 #endif
86     int                sockfd;       /**< socket file descriptor              */
87 } IMDSocket;
88
89
90
91 #if defined(GMX_NATIVE_WINDOWS) && defined(GMX_HAVE_WINSOCK)
92 /*! \internal
93  *
94  * \brief Function to initialize winsock
95  *
96  * \returns 0 if successful.
97  */
98 extern int imdsock_winsockinit();
99 #endif
100
101
102 /*! \brief Create an IMD master socket.
103  *
104  * \returns  The IMD socket if successful. Otherwise prints an error message and returns NULL.
105  */
106 extern IMDSocket *imdsock_create();
107
108
109 /*! \brief Bind the IMD socket to address and port.
110  *
111  * Prints out an error message if unsuccessful.
112  * If port == 0, bind() assigns a free port automatically.
113  *
114  *
115  * \param sock      The IMD socket.
116  * \param port      The port to bind to.
117  *
118  * \returns 0 if successful.
119  */
120 extern int imdsock_bind(IMDSocket *sock, int port);
121
122
123 /*! \brief Set socket to listening state.
124  *
125  * Prints out an error message if unsuccessful.
126  *
127  * \param sock      The IMD socket.
128  *
129  * \returns 0 if successful.
130  *
131  */
132 extern int imd_sock_listen(IMDSocket *sock);
133
134
135 /*! \brief Accept incoming connection and redirect to client socket.
136  *
137  * Prints out an error message if unsuccessful.
138  *
139  * \param sock      The IMD socket.
140  *
141  * \returns IMD socket if successful, NULL otherwise.
142  */
143 extern IMDSocket *imdsock_accept(IMDSocket *sock);
144
145
146 /*! \brief Get the port number used for IMD connection.
147  *
148  * Prints out an error message if unsuccessful.
149  *
150  * \param sock      The IMD socket.
151  * \param port      The assigned port number.
152  *
153  * \returns 0 if successful, an error code otherwise.
154  */
155 extern int imdsock_getport(IMDSocket *sock, int *port);
156
157
158 /*! \brief  Write to socket.
159  *
160  *
161  * \param sock      The IMD socket.
162  * \param buffer    The data to write.
163  * \param length    Number of bytes to write.
164  *
165  * \returns The number of bytes written, or -1.
166  */
167 extern int imdsock_write(IMDSocket *sock, const char *buffer, int length);
168
169
170 /*! \brief  Read from socket.
171  *
172  * \param sock      The IMD socket.
173  * \param buffer    Buffer to put the read data.
174  * \param length    Number of bytes to read.
175  *
176  * \returns The number of bytes read, or -1 for errors.
177  */
178 extern int imdsock_read(IMDSocket *sock, char *buffer, int length);
179
180
181 /*! \brief Shutdown the socket.
182  *
183  * \param sock      The IMD socket.
184  *
185  */
186 extern void imdsock_shutdown(IMDSocket *sock);
187
188
189 /*! \brief Close the socket and free the sock struct memory.
190  *
191  * Writes an error message if unsuccessful.
192  *
193  * \param sock      The IMD socket.
194  *
195  * \returns 1 on success, or 0 if unsuccessful.
196  */
197 extern int imdsock_destroy(IMDSocket *sock);
198
199
200 /*! \brief Try to read from the socket.
201  *
202  * Time out after waiting the interval specified.
203  * Print an error message if unsuccessful.
204  *
205  * \param sock         The IMD socket.
206  * \param timeoutsec   Time out seconds
207  * \param timeoutusec  Time out microseconds
208  *
209  */
210 extern int imdsock_tryread(IMDSocket *sock, int timeoutsec, int timeoutusec);
211
212
213 #ifdef __cplusplus
214 }
215 #endif
216
217
218 #endif