vmdsock.h File Reference

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <sys/file.h>

Go to the source code of this file.

Classes

struct  vmdsocket

Functions

int vmdsock_init (void)
void * vmdsock_create (void)
int vmdsock_bind (void *, int)
int vmdsock_listen (void *)
void * vmdsock_accept (void *)
int vmdsock_connect (void *, const char *, int)
int vmdsock_write (void *, const void *, int)
int vmdsock_read (void *, void *, int)
int vmdsock_selread (void *, int)
int vmdsock_selwrite (void *, int)
void vmdsock_destroy (void *)

Function Documentation

void* vmdsock_accept ( void *   ) 

Definition at line 128 of file vmdsock.C.

References vmdsocket::sd.

Referenced by GlobalMasterIMD::calculate().

00128                                {
00129   int rc;
00130   vmdsocket *new_s = NULL, *s = (vmdsocket *) v;
00131   socklen_t len;
00132 
00133   len = sizeof(s->addr);
00134   rc = accept(s->sd, (struct sockaddr *) &s->addr, &len);
00135   if (rc >= 0) {
00136     new_s = (vmdsocket *) malloc(sizeof(vmdsocket));
00137     if (new_s != NULL) {
00138       *new_s = *s;
00139       new_s->sd = rc;
00140     }
00141   }
00142   return (void *)new_s;
00143 }

int vmdsock_bind ( void *  ,
int   
)

Definition at line 114 of file vmdsock.C.

References vmdsocket::addr, and vmdsocket::sd.

Referenced by find_free_port().

00114                                      {
00115   vmdsocket *s = (vmdsocket *) v;
00116   memset(&(s->addr), 0, sizeof(s->addr)); 
00117   s->addr.sin_family = PF_INET;
00118   s->addr.sin_port = htons(port);
00119 
00120   return bind(s->sd, (struct sockaddr *) &s->addr, sizeof(s->addr));
00121 }

int vmdsock_connect ( void *  ,
const char *  ,
int   
)

Definition at line 92 of file vmdsock.C.

References vmdsocket::addr, and vmdsocket::sd.

00092                                                           {
00093   vmdsocket *s = (vmdsocket *) v;
00094   char address[1030];
00095   struct hostent *h;
00096 
00097   h=gethostbyname(host);
00098   if (h == NULL) 
00099     return -1;
00100   sprintf(address, "%d.%d.%d.%d",
00101     (unsigned char) h->h_addr_list[0][0],
00102     (unsigned char) h->h_addr_list[0][1],
00103     (unsigned char) h->h_addr_list[0][2],
00104     (unsigned char) h->h_addr_list[0][3]);
00105 
00106   memset(&(s->addr), 0, sizeof(s->addr)); 
00107   s->addr.sin_family = PF_INET;
00108   s->addr.sin_addr.s_addr = inet_addr(address);
00109   s->addr.sin_port = htons(port);  
00110 
00111   return connect(s->sd, (struct sockaddr *) &s->addr, sizeof(s->addr)); 
00112 }

void* vmdsock_create ( void   ) 

Definition at line 76 of file vmdsock.C.

References vmdsocket::sd.

Referenced by GlobalMasterIMD::GlobalMasterIMD().

00076                             {
00077   vmdsocket * s;
00078 
00079   s = (vmdsocket *) malloc(sizeof(vmdsocket));
00080   if (s != NULL)
00081     memset(s, 0, sizeof(vmdsocket)); 
00082 
00083   if ((s->sd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
00084     printf("Failed to open socket.");
00085     free(s);
00086     return NULL;
00087   }
00088 
00089   return (void *) s;
00090 }

void vmdsock_destroy ( void *   ) 

Definition at line 164 of file vmdsock.C.

References vmdsocket::sd.

Referenced by GlobalMasterIMD::calculate(), GlobalMasterIMD::get_vmd_forces(), GlobalMasterIMD::GlobalMasterIMD(), and GlobalMasterIMD::~GlobalMasterIMD().

00164                                {
00165   vmdsocket * s = (vmdsocket *) v;
00166   if (s == NULL)
00167     return;
00168 
00169 #if defined(WIN32) && !defined(__CYGWIN__)
00170   closesocket(s->sd);
00171 #else
00172   close(s->sd);
00173 #endif
00174   free(s);  
00175 }

int vmdsock_init ( void   ) 

Definition at line 52 of file vmdsock.C.

Referenced by GlobalMasterIMD::GlobalMasterIMD().

00052                        {
00053 #if defined(WIN32) && !defined(__CYGWIN__)
00054   int rc = 0;
00055 
00056 /*
00057 ** XXX static and global variables are unsafe for shared memory builds.
00058 ** The global and static vars should be eliminated.
00059 */
00060   static int initialized=0;
00061 
00062   if (!initialized) {
00063     WSADATA wsdata;
00064     rc = WSAStartup(MAKEWORD(1,1), &wsdata);
00065     if (rc == 0)
00066       initialized = 1;
00067   }
00068 
00069   return rc;
00070 #else   
00071   return 0;
00072 #endif
00073 }

int vmdsock_listen ( void *   ) 

Definition at line 123 of file vmdsock.C.

References vmdsocket::sd.

Referenced by GlobalMasterIMD::GlobalMasterIMD().

00123                              {
00124   vmdsocket *s = (vmdsocket *) v;
00125   return listen(s->sd, 5);
00126 }

int vmdsock_read ( void *  ,
void *  ,
int   
)

Definition at line 154 of file vmdsock.C.

References vmdsocket::sd.

Referenced by imd_readn().

00154                                                 {
00155   vmdsocket *s = (vmdsocket *) v;
00156 #if defined(WIN32) && !defined(__CYGWIN__)
00157   return recv(s->sd, (char*) buf, len, 0); // windows lacks the read() call
00158 #else
00159   return read(s->sd, buf, len);
00160 #endif
00161 
00162 }

int vmdsock_selread ( void *  ,
int   
)

Definition at line 177 of file vmdsock.C.

References vmdsocket::sd.

Referenced by GlobalMasterIMD::calculate(), GlobalMasterIMD::get_vmd_forces(), imd_recv_handshake(), and my_imd_connect().

00177                                       {
00178   vmdsocket *s = (vmdsocket *)v;
00179   fd_set rfd;
00180   struct timeval tv;
00181   int rc;
00182  
00183   FD_ZERO(&rfd);
00184   FD_SET(s->sd, &rfd);
00185   memset((void *)&tv, 0, sizeof(struct timeval));
00186   tv.tv_sec = sec;
00187   do {
00188     rc = select(s->sd+1, &rfd, NULL, NULL, &tv);
00189   } while (rc < 0 && errno == EINTR);
00190   return rc;
00191 
00192 }

int vmdsock_selwrite ( void *  ,
int   
)

Definition at line 194 of file vmdsock.C.

References vmdsocket::sd.

Referenced by GlobalMasterIMD::send_energies(), and GlobalMasterIMD::send_fcoords().

00194                                        {
00195   vmdsocket *s = (vmdsocket *)v;
00196   fd_set wfd;
00197   struct timeval tv;
00198   int rc;
00199  
00200   FD_ZERO(&wfd);
00201   FD_SET(s->sd, &wfd);
00202   memset((void *)&tv, 0, sizeof(struct timeval));
00203   tv.tv_sec = sec;
00204   do {
00205     rc = select(s->sd + 1, NULL, &wfd, NULL, &tv);
00206   } while (rc < 0 && errno == EINTR);
00207   return rc;
00208 }

int vmdsock_write ( void *  ,
const void *  ,
int   
)

Definition at line 145 of file vmdsock.C.

References vmdsocket::sd.

Referenced by imd_writen().

00145                                                        {
00146   vmdsocket *s = (vmdsocket *) v;
00147 #if defined(WIN32) && !defined(__CYGWIN__)
00148   return send(s->sd, (const char*) buf, len, 0);  // windows lacks the write() call
00149 #else
00150   return write(s->sd, buf, len);
00151 #endif
00152 }


Generated on 6 Dec 2019 for NAMD by  doxygen 1.6.1