IMDType imd_recv_header(void *s, int32_t *length) { IMDheader header; if (imd_readn(s, (char *)&header, HEADERSIZE) != HEADERSIZE) return IMD_IOERROR; swap_header(&header); *length = header.length; return (IMDType)header.type; }
int VMDCollab::check_event() { if (!clientsock) return FALSE; eval_in_progress = TRUE; char buf[VMDCOLLAB_MSGSIZE]; while (vmdsock_selread(clientsock, 0) > 0) { if (imd_readn(clientsock, buf, VMDCOLLAB_MSGSIZE) != VMDCOLLAB_MSGSIZE) { vmdsock_shutdown(clientsock); vmdsock_destroy(clientsock); clientsock = NULL; break; } runcommand(new TclEvalEvent(buf)); } eval_in_progress = FALSE; return TRUE; }
int imd_recv_fcoords(void *s, int32_t n, float *coords) { return (imd_readn(s, (char *)coords, 12*n) != 12*n); }
int imd_recv_energies(void *s, IMDEnergies *energies) { return (imd_readn(s, (char *)energies, sizeof(IMDEnergies)) != sizeof(IMDEnergies)); }
int imd_recv_mdcomm(void *s, int32_t n, int32_t *indices, float *forces) { if (imd_readn(s, (char *)indices, 4*n) != 4*n) return 1; if (imd_readn(s, (char *)forces, 12*n) != 12*n) return 1; return 0; }
void *VMDCollab::serverproc(void *serversock) { ResizeArray<void *>clients; char buf[VMDCOLLAB_MSGSIZE]; int i, j; while (1) { // if we have no clients, hang until someone connects // otherwise, just check for pending connections if (vmdsock_selread(serversock, 0) > 0) { msgInfo << "serversock became readable" << sendmsg; void *clientsock = vmdsock_accept(serversock); if (clientsock) { msgInfo << "VMDCollab accepting connection" << sendmsg; clients.append(clientsock); } } else if (vmdsock_selwrite(serversock, 0)) { msgInfo << "serversock became writable; exiting..." << sendmsg; break; } // Loop through one socket at a time. If incoming data is found, // drain it before moving on, on the assumption that we only want // commands from one VMD at a time to be propagated to the other // clients. for (i=0; i<clients.num(); i++) { void *client = clients[i]; while (vmdsock_selread(client, 0) > 0) { memset(buf, 0, VMDCOLLAB_MSGSIZE); if (imd_readn(client, buf, VMDCOLLAB_MSGSIZE) != VMDCOLLAB_MSGSIZE) { msgInfo << "client sent incomplete message, shutting it down" << sendmsg; vmdsock_shutdown(client); vmdsock_destroy(client); clients.remove(clients.find(client)); break; } // send to all other clients for (j=0; j<clients.num(); j++) { void *dest = clients[j]; if (dest != client) { imd_writen(clients[j], buf, VMDCOLLAB_MSGSIZE); } } // loop over clients other than sender } // while client is readable } // loop over clients vmd_msleep(10); } // if here, then the serversock got shut down, indicating that it's // time to die. msgInfo << "VMDCollab shutting down server" << sendmsg; for (i=0; i<clients.num(); i++) { void *client = clients[i]; strcpy(buf, "exit"); imd_writen(client, buf, VMDCOLLAB_MSGSIZE); vmdsock_shutdown(client); vmdsock_destroy(client); } vmdsock_destroy(serversock); return NULL; }