///--------------------------------------------------------------------------------- /// Join Request ///--------------------------------------------------------------------------------- NETWORK_MESSAGE_DEFINITION_WITH_OPTIONS( MSG_TYPE_JOIN_REQUEST, MO_RELIABLE ) { DeveloperConsole::WriteLine( "Join request from " + sender.m_addr.GetAddrStr(), INFO_TEXT_COLOR ); char* id = nullptr; msg.ReadString( id ); AddConnectionResult result = ACR_DENIED; NetworkConnection* conn = sender.m_session->AddConnection( sender.m_addr, std::string( id ), result ); if (conn && result == ACR_ACCEPTED) { NetworkMessage acceptMsg( MSG_TYPE_JOIN_ACCEPT ); uint8_t maxConnections = (uint8_t) sender.m_session->GetMaxConnections(); acceptMsg.WriteBytes( (void*)&maxConnections, sizeof( maxConnections ) ); uint8_t connIdx = conn->GetConnectionIndex(); acceptMsg.WriteBytes( (void*)&connIdx, sizeof( connIdx ) ); uint8_t hostIdx = 0; acceptMsg.WriteBytes( (void*)&hostIdx, sizeof( hostIdx ) ); std::string hostID = sender.m_session->GetHost()->GetUniqueID(); acceptMsg.WriteString( hostID.c_str() ); uint8_t invalidIdx = CONNECTION_INVALID_INDEX; acceptMsg.WriteBytes( (void*)&invalidIdx, sizeof( invalidIdx ) ); conn->AddMessage( acceptMsg ); } else { uint8_t denyReason; NetworkMessage* denyMsg = new NetworkMessage( MSG_TYPE_JOIN_DENY ); switch (result) { case ACR_DENIED: denyReason = 0; break; case ACR_FULL: denyReason = 1; break; case ACR_ID_TAKEN: denyReason = 2; break; default: break; } denyMsg->WriteBytes( (void*)&denyReason, sizeof( denyReason ) ); sender.m_session->SendDirectMessage( denyMsg, &sender.m_addr ); } }
static void *netrequest_cmd_thread(void *arg){ msg *message = NULL; char* me = "netrequest_cmd_thread"; while(1){ requestNo++; freeMsg(message); netlog("%s waiting to process request number %d\n", me, requestNo); message = acceptMsg(STDIN_FILENO); if(message == NULL){ netlog("%s readMsg failed; %p", me, arg); continue; } netlog("%s processing request:\n\"%s\"\n", me, message->data); } return NULL; }
void* handleRequest(void* args){ int from = -1, to = -1; echofds* fds = (echofds*)args; if(fds == NULL) goto fail; from = fds->from; to = fds->to; while(1){ msg* message = acceptMsg(from); if(message != NULL){ msg2netlog(message); pthread_mutex_lock(&netrequest_output_mutex); sendMsg(to, message); pthread_mutex_unlock(&netrequest_output_mutex); freeMsg(message); } else { netlog("handleRequest exiting gracefully!\n"); break; } } fail: if(from != -1){ close(from); } return NULL; }
int main(int argc, char** argv){ msg *messageIn = NULL, *messageOut = NULL; char *sender, *rest, *body, *cmd; int retval; if(argv == NULL){ fprintf(stderr, "didn't understand: (argv == NULL)\n"); exit(EXIT_FAILURE); } if(argc != 4){ fprintf(stderr, "didn't understand: (argc != 4)\n"); exit(EXIT_FAILURE); } self_debug_flag = SOCKET_DEBUG; self = argv[0]; sock = atoi(argv[1]); registry_fifo_in = argv[2]; registry_fifo_out = argv[3]; if(socket_install_handler() != 0){ perror("socket couldn't install handler\n"); exit(EXIT_FAILURE); } while(1){ requestNo++; if(messageIn != NULL){ freeMsg(messageIn); messageIn = NULL; } if(messageOut != NULL){ freeMsg(messageOut); messageOut = NULL; } announce("%s waiting to process request number %d\n", self, requestNo); messageIn = acceptMsg(STDIN_FILENO); if(messageIn == NULL){ perror("socket acceptMsg failed"); continue; } announce("%s processing request:\n\"%s\"\n", self, messageIn->data); retval = parseActorMsg(messageIn->data, &sender, &body); if(!retval){ fprintf(stderr, "didn't understand: (parseActorMsg)\n\t \"%s\" \n", messageIn->data); continue; } if(getNextToken(body, &cmd, &rest) <= 0){ fprintf(stderr, "didn't understand: (cmd)\n\t \"%s\" \n", body); continue; } if(!strcmp(cmd, "read")){ if(closed){ goto readfail; } else { char *bytes2read; int bytesDesired; if(getNextToken(rest, &bytes2read, &rest) <= 0){ fprintf(stderr, "didn't understand: (bytes2read)\n\t \"%s\" \n", rest); continue; } bytesDesired = atoi(bytes2read); if(bytesDesired == 0){ goto readfail; } else { messageOut = readMsg(sock); if(messageOut == NULL){ perror("socket readMsg failed"); goto readfail; } announce("%s\n%s\nreadOK\n%d\n%s\n", sender, self, messageOut->bytesUsed, messageOut->data); sendFormattedMsgFP(stdout, "%s\n%s\nreadOK\n%d\n%s\n", sender, self, messageOut->bytesUsed, messageOut->data); } continue; readfail: announce("%s\n%s\nreadFailure\n", sender, self); sendFormattedMsgFP(stdout, "%s\n%s\nreadFailure\n", sender, self); continue; } } else if(!strcmp(cmd, "write")){ if(closed){ fprintf(stderr, "closed\n"); goto writefail; } else { char *bytes2write; int bytesToSend, bytesSent; if(getNextToken(rest, &bytes2write, &rest) <= 0){ fprintf(stderr, "didn't understand: (bytes2write)\n\t \"%s\" \n", rest); continue; } bytesToSend = atoi(bytes2write); if(bytesToSend == 0){ fprintf(stderr, "(bytesToSend == 0)\n"); goto writefail; } else { int len = strlen(rest); bytesToSend = (len < bytesToSend ) ? len : bytesToSend; if((bytesSent = write(sock, rest, bytesToSend)) != bytesToSend){ fprintf(stderr, "(bytesSent = write(sock, rest, bytesToSend)) != bytesToSend\n"); goto writefail; } else { announce("%s\n%s\nwriteOK\n%d\n", sender, self, bytesSent); sendFormattedMsgFP(stdout, "%s\n%s\nwriteOK\n%d\n", sender, self, bytesSent); } } } continue; writefail: announce("%s\n%s\nwriteFailure\n", sender, self); sendFormattedMsgFP(stdout, "%s\n%s\nwriteFailure\n", sender, self); } else if(!strcmp(cmd, "close")){ int slotNumber = -1; if(!closed){ closed = 1; if(close(sock) < 0){ fprintf(stderr, "close failed in socket close case\n"); } announce("%s\n%s\ncloseOK\n", sender, self); sendFormattedMsgFP(stdout, "%s\n%s\ncloseOK\n", sender, self); iop_usleep(1); announce("Socket called %s unregistering\n", self); slotNumber = deleteFromRegistry(self); announce("Socket called %s removed from slot %d, now exiting\n", self, slotNumber); exit(EXIT_SUCCESS); } else { announce("%s\n%s\ncloseFailure\n", sender, self); sendFormattedMsgFP(stdout, "%s\n%s\ncloseFailure\n", sender, self); } } else { fprintf(stderr, "didn't understand: (command)\n\t \"%s\" \n", messageIn->data); continue; } } }
int main(int argc, char** argv){ if(argc != 1){ fprintf(stderr, "Usage: %s\n", argv[0]); } self_debug_flag = PVS_ACTOR_DEBUG; self = argv[0]; ec_neg1( iop_install_handlers(chld_handler, intr_handler) ); ec_neg1( pipe(pin) ); ec_neg1( pipe(perr) ); ec_neg1( pipe(pout) ); /*it's time to fork */ ec_neg1( child = fork() ); if(child == 0){ /* i'm destined to be pvs */ ec_neg1( dup2(pin[0], STDIN_FILENO) ); ec_neg1( dup2(perr[1], STDERR_FILENO) ); ec_neg1( dup2(pout[1], STDOUT_FILENO) ); ec_neg1( close(pin[0]) ); ec_neg1( close(perr[1]) ); ec_neg1( close(pout[1]) ); ec_neg1( execvp(pvs_exe, pvs_argv) ); } else { /* i'm the boss */ pthread_t errThread; fdBundle errFdB; /* for monitoring the error stream */ errFdB.fd = perr[0]; errFdB.exit = &child_died; ec_neg1( close(pin[0]) ); ec_neg1( close(perr[1]) ); ec_neg1( close(pout[1]) ); /* sleep(2); */ ec_rv( pthread_create(&errThread, NULL, echoErrorsSilently, &errFdB) ); announce("Listening to PVS\n"); parsePVSThenEcho("\ncl-user(", pout[0], STDOUT_FILENO); write(pin[1], "(in-package 'pvs)\n", strlen("(in-package 'pvs)\n")); announce("Listening to PVS\n"); parsePVSThenEcho("\npvs(", pout[0], STDOUT_FILENO); while(1){ int length, retval; msg *query = NULL, *response = NULL; char *sender, *command; announce("Listening to IO\n"); query = acceptMsg(STDIN_FILENO); if(query == NULL) continue; retval = parseActorMsg(query->data, &sender, &command); write(pin[1], command, strlen(command)); write(pin[1], "\n", 1); announce("Listening to PVS\n"); response = readPVSMsg("\npvs(", pout[0]); if(response != NULL){ length = parseString(response->data, response->bytesUsed); response->bytesUsed = length; sendFormattedMsgFP(stdout, "%s\n%s\n%s\n", sender, self, response->data); if(self_debug_flag)writeMsg(STDERR_FILENO, response); announce("\nparseThenEcho wrote %d bytes\n", response->bytesUsed); } freeMsg(query); freeMsg(response); } } exit(EXIT_SUCCESS); EC_CLEANUP_BGN exit(EXIT_FAILURE); EC_CLEANUP_END }
int main(int argc, char** argv){ msg *message = NULL; char childName[SIZE], fdName[SIZE], iopPid[SIZE]; int retval, portNo; char *port, *sender, *rest, *body, *cmd; if(argv == NULL){ fprintf(stderr, "didn't understand: (argv == NULL)\n"); exit(EXIT_FAILURE); } if(argc != 3){ fprintf(stderr, "didn't understand: (argc != 3)\n"); exit(EXIT_FAILURE); } iop_pid = getppid(); self_debug_flag = SOCKETFACTORY_DEBUG; self = argv[0]; registry_fifo_in = argv[1]; registry_fifo_out = argv[2]; if(iop_install_handler(SIGCHLD, 0, socketfactory_sigchild_handler) != 0){ perror("socketfactory could not install signal handler"); exit(EXIT_FAILURE); } while(1){ requestNo++; freeMsg(message); message = acceptMsg(STDIN_FILENO); if(message == NULL){ perror("socketfactory acceptMsg failed"); continue; } announce("Received message->data = \"%s\"\n", message->data); retval = parseActorMsg(message->data, &sender, &body); if(!retval){ fprintf(stderr, "didn't understand: (parseActorMsg)\n\t \"%s\" \n", message->data); continue; } if(getNextToken(body, &cmd, &rest) <= 0){ fprintf(stderr, "didn't understand: (cmd)\n\t \"%s\" \n", body); continue; } if(!strcmp(cmd, "openclient")){ char *host; announce("openclient case\n"); if(getNextToken(rest, &host, &rest) <= 0){ fprintf(stderr, "didn't understand: (host)\n\t \"%s\" \n", rest); continue; } if(getNextToken(rest, &port, &rest) <= 0){ fprintf(stderr, "didn't understand: (port)\n\t \"%s\" \n", rest); continue; } portNo = atoi(port); announce("port = %d\n", portNo); if(allocateSocket(portNo, host, &clientFd) != 1){ goto openclientfail; } snprintf(childName, SIZE, "%s%d", clientChildName, clientNo); snprintf(fdName, SIZE, "%d", clientFd); clientChildArgv[0] = childName; announce("clientChildArgv[0] = %s\n", clientChildArgv[0]); clientChildArgv[1] = fdName; announce("clientChildArgv[1] = %s\n", clientChildArgv[1]); clientChildArgv[2] = registry_fifo_in; announce("clientChildArgv[2] = %s\n", clientChildArgv[2]); clientChildArgv[3] = registry_fifo_out; announce("clientChildArgv[3] = %s\n", clientChildArgv[3]); clientChildArgv[4] = NULL; announce("clientChildArgv[4] = %s\n", clientChildArgv[4]); announce("Spawning actor\n"); newActor(1, clientChildExe, clientChildArgv); announce("%s\n%s\nopenClientOK\n%s\n", sender, self, childName); sendFormattedMsgFP(stdout, "%s\n%s\nopenClientOK\n%s\n", sender, self, childName); clientNo++; if(close(clientFd) < 0){ fprintf(stderr, "close failed in openclient case\n"); } continue; openclientfail: announce("%s\n%s\nopenClientFailure\n", sender, self); sendFormattedMsgFP(stdout, "%s\n%s\nopenClientFailure\n", sender, self); continue; } else if(!strcmp(cmd, "openlistener")){ announce("openlistener case\n"); if(getNextToken(rest, &port, &rest) <= 0){ fprintf(stderr, "didn't understand: (port)\n\t \"%s\" \n", rest); continue; } portNo = atoi(port); announce("port = %d\n", portNo); if(allocateListeningSocket(portNo, &listenerFd) != 1){ fprintf(stderr, "Couldn't listen on port\n"); goto openlistenerfail; } snprintf(childName, SIZE, "%s%d", listenerChildName, listenerNo); snprintf(fdName, SIZE, "%d", listenerFd); snprintf(iopPid, SIZE, "%d", iop_pid); listenerChildArgv[0] = childName; announce("listenerChildArgv[0] = %s\n", listenerChildArgv[0]); listenerChildArgv[1] = fdName; announce("listenerChildArgv[1] = %s\n", listenerChildArgv[1]); listenerChildArgv[2] = sender; announce("listenerChildArgv[2] = %s\n", listenerChildArgv[2]); listenerChildArgv[3] = registry_fifo_in; announce("listenerChildArgv[3] = %s\n", listenerChildArgv[3]); listenerChildArgv[4] = registry_fifo_out; announce("listenerChildArgv[4] = %s\n", listenerChildArgv[4]); listenerChildArgv[5] = iopPid; announce("listenerChildArgv[5] = %s\n", listenerChildArgv[5]); listenerChildArgv[6] = NULL; announce("listenerChildArgv[6] = %s\n", listenerChildArgv[6]); announce("Spawning actor\n"); newActor(1, listenerChildExe, listenerChildArgv); announce("Spawned actor\n"); announce("%s\n%s\nopenListenerOK\n%s\n", sender, self, childName); sendFormattedMsgFP(stdout, "%s\n%s\nopenListenerOK\n%s\n", sender, self, childName); listenerNo++; if(close(listenerFd) < 0){ fprintf(stderr, "close failed in openlistener case\n"); } continue; openlistenerfail: announce("%s\n%s\nopenListenerFailure\n", sender, self); sendFormattedMsgFP(stdout, "%s\n%s\nopenListenerFailure\n", sender, self); continue; } else { fprintf(stderr, "didn't understand: (command)\n\t \"%s\" \n", message->data); continue; } } }