int main() { startserver(); // run client code clientthread(); // stop server stopserver(); return 0; }
void ConsolidatedTests::CreateAndReleaseTests() { beginTest("Create and Release Tests"); initialize_field_manager(4); startserver(65002); register_callbacks(join_game, leave_game, take_cell, get_size, get_cell_player, is_there_a_winner); Thread::sleep(5); stopserver(); release_field_manager(); Thread::sleep(50); }
int main(int argc, char *argv[]) { const char *action = NULL; struct sigaction sa; ARGBEGIN { case 'l': case 'r': case 's': if (writefifo(fifofile, ARGC()) == -1) die("cannot write to fifo\n"); exit(EXIT_SUCCESS); default: usage(); } ARGEND; if (argc > 1) usage(); if (getuid() != 0) die("only root can run this program\n"); if (getlock(pidfile) == -1) die("cannot get exclusive lock\n"); if (createfifo(fifofile, 0666) == -1) die("cannot create fifo\n"); sa.sa_flags = SA_NOCLDSTOP; sa.sa_handler = catchsignal; sigemptyset(&sa.sa_mask); sigaction(SIGCHLD, &sa, NULL); signal(SIGINT, catchsignal); signal(SIGHUP, catchsignal); signal(SIGQUIT, catchsignal); signal(SIGTERM, catchsignal); signal(SIGKILL, catchsignal); signal(SIGPIPE, catchsignal); startserver(server, display); do { const char *user, *pass; char c; if (autologin && authenticate(autologin, NULL)) { autologin = NULL; goto login; } creategui(); do { rungui(&user, &pass); if (!strcmp(user, "reboot")) action = rebootcmd; else if (!strcmp(user, "shutdown")) action = shutdowncmd; } while (!action && !authenticate(user, pass)); destroygui(); if (action) break; login: childpid = startsession(child, env); c = readfifo(fifofile); if (c == 'r') action = rebootcmd; else if (c == 's') action = shutdowncmd; else if (!c) while (childpid) sleep(10); stopsession(); } while (!action); signal(SIGINT, SIG_IGN); signal(SIGHUP, SIG_IGN); signal(SIGQUIT, SIG_IGN); signal(SIGTERM, SIG_DFL); signal(SIGKILL, SIG_DFL); signal(SIGCHLD, SIG_DFL); signal(SIGPIPE, SIG_IGN); stopserver(); removefifo(fifofile); removelock(pidfile); return system(action); }
VMDCollab::~VMDCollab() { stopserver(); delete cmdbufstr; }
void Cserver::ThreadProc(void) { if(server==INVALID_SOCKET) return; int i; CEvent ServerEvent; CEvent ClientEvent[MAX_CLIENTS]; #define MAX_DATA 1024 // longest message a client can send char ClientData[MAX_CLIENTS][MAX_DATA]; char toparse[MAX_DATA]; HANDLE events[MAX_CLIENTS+2]; bool server_running=true; WSAEventSelect(server,ServerEvent,FD_ACCEPT); for(i=0;i<MAX_CLIENTS;i++) ClientData[i][0]=0; for(;;) { int count=0; events[count++]=ServerThreadEvent; if(server_running) events[count++]=ServerEvent; for(i=0;i<MAX_CLIENTS;i++) if(clients[i]!=INVALID_SOCKET) events[count++]=ClientEvent[i]; unsigned int res=WaitForMultipleObjects(count,events,FALSE,INFINITE); if(res==WAIT_OBJECT_0) { DEBUG("ServerThread terminating\n"); AfxEndThread(0); return; } else if(server_running && res==(WAIT_OBJECT_0+1)) { for(i=0;i<MAX_CLIENTS;i++) if(clients[i]==INVALID_SOCKET) break; if(i==MAX_CLIENTS) { DEBUG("Should have at least 1 empty client, but none found\n"); continue; } clients[i]=accept(server,NULL,NULL); if(clients[i]==INVALID_SOCKET) { DEBUG("accept() failed\n"); continue; } WSAEventSelect(clients[i],ClientEvent[i],FD_CLOSE|FD_READ); ClientEvent[i].ResetEvent(); ClientData[i][0]='\0'; DEBUG("Client connection %d accepted\n",i); for(i=0;i<MAX_CLIENTS;i++) if(clients[i]==INVALID_SOCKET) break; if(i==MAX_CLIENTS) { DEBUG("Server full. Closing server socket.\n"); stopserver(); server_running=false; } } else /* client closed or data received */ { count=server_running?2:1; for(i=0;i<MAX_CLIENTS;i++) { if(clients[i]!=INVALID_SOCKET) { if(res==(WAIT_OBJECT_0+(count++))) { /* either we got data or the connection closed */ int curlen=strlen(ClientData[i]); int maxlen=MAX_DATA-curlen-1; int bytes=recv( clients[i], ClientData[i]+curlen, maxlen, 0); if(bytes==0 || bytes==SOCKET_ERROR) { /* Connection was closed or something's screwy */ closesocket(clients[i]); clients[i]=INVALID_SOCKET; DEBUG("Client connection %d closed\n",i); if(server_running==false) { DEBUG("Slot open. Restarting server.\n"); if(startserver()==true) { WSAEventSelect(server,ServerEvent,FD_ACCEPT); server_running=true; } else { DEBUG("Server failed to restart.\n"); stopserver(); } } } else /* bytes > 0, we read data */ { ClientData[i][curlen+bytes]='\0'; char *cur=ClientData[i]; for(;;) { char *nl=strchr(cur,'\n'); if(nl==NULL) { if(cur!=ClientData[i]) memmove(ClientData[i],cur,strlen(cur)+1); break; } else { *nl='\0'; // ---------------------------- // Do something with the received line (cur) DEBUG("Got string: %s\n",cur); LRESULT copyDataResult=false; char *response=NULL; strcpy(toparse,cur); char *command=NULL; char *remotename=NULL; char *buttonname=NULL; char *repeats=NULL; if (toparse) command=strtok(toparse," \t\r"); if (!command) //ignore lines with only whitespace { cur=nl+1; break; } else if (stricmp(command,"VERSION")==0) { if (strtok(NULL," \t\r")==NULL) { copyDataResult = true; response = new char[strlen(id)+9]; if (response) sprintf(response,"DATA\n1\n%s\n",id); } else copyDataResult=-100; } else if (stricmp(command,"LIST")==0) { remotename=strtok(NULL," \t\r"); struct ir_remote *all=global_remotes; int n=0; if (!remotename) { copyDataResult = true; while(all) { n++; all=all->next; } if (n!=0) { response = new char[n*(LINE_LEN+2)+7]; sprintf(response,"DATA\n%d\n",n); all=global_remotes; while(all) { strcat(response,all->name); strcat(response,"\n"); all=all->next; } } } else { while (all!=NULL && stricmp(remotename,all->name)) all=all->next; if (all) { copyDataResult = true; struct ir_ncode *allcodes=all->codes; while (allcodes->name) { n++; allcodes++; } if (n!=0) { response = new char[n*(LINE_LEN+2)+7]; sprintf(response,"DATA\n%d\n",n); allcodes=all->codes; while(allcodes->name) { strcat(response,allcodes->name); strcat(response,"\n"); allcodes++; } } } else copyDataResult=-1; //unknown remote } } else if (!password.IsEmpty() && !password.CompareNoCase(command)) //only accept commands if a password is set and matches { CString incoming = (LPCSTR) (cur); int j=incoming.FindOneOf(" \t")+1; remotename=strtok(NULL," \t\r"); if (remotename==NULL) { response = new char[14]; if (response) sprintf(response,"DATA\n1\nremote missing\n"); } else { buttonname=strtok(NULL," \t\r"); if (buttonname==NULL) { response = new char[12]; if (response) sprintf(response,"DATA\n1\ncode missing\n"); } else { HWND pOtherWnd = FindWindow(NULL, "WinLirc"); if (pOtherWnd) { COPYDATASTRUCT cpd; cpd.dwData = 0; cpd.cbData = strlen(&cur[j]); cpd.lpData = (void*)&cur[j]; copyDataResult = SendMessage(pOtherWnd,WM_COPYDATA,NULL,(LPARAM)&cpd); } } } } else { response = new char[strlen(command)+26]; if (response) sprintf(response,"DATA\n1\n%s%s\n","unknown command: ",command); } if (copyDataResult==-1) { response = new char[strlen(remotename)+25]; if (response) sprintf(response,"DATA\n1\n%s%s\n","unknown remote: ",remotename); } else if (copyDataResult==-2) { response = new char[strlen(buttonname)+23]; if (response) sprintf(response,"DATA\n1\n%s%s\n","unknown code: ",buttonname); } else if (copyDataResult==-100) { response = new char[15]; if (response) sprintf(response,"DATA\n1\nbad send packet\n"); } reply(cur,i,copyDataResult==1,response); if (response) delete(response); cur=nl+1; } } } break; } } } } } }
void *handle_tcp_client(void *arg) { printf("SERVER: client thread started\n"); struct thread_arg *thread_data = (struct thread_arg *)arg; int client_socket = thread_data->client_socket; int idx = thread_data->thread_idx; free(arg); /* malloc was made before starting this thread */ char command[RCVBUFSIZE]; /* Buffer for square string */ char response[RCVBUFSIZE]; int recv_msg_size; /* Size of received message */ all_sockets[idx] = client_socket; int is_joined = 0; while (!shutdown_server) { /* Receive message from client */ command[0] = '\0'; recv_msg_size = recv(client_socket, command, RCVBUFSIZE - 1, 0); command[recv_msg_size] = '\0'; if (recv_msg_size == 0) { /* zero indicates end of transmission */ break; } printf("server recieved message: %s \n", command); if (shutdown_server) { break; } if (winner_established == 1) { say(all_sockets[idx], "END\n", 0); continue; } if(strncasecmp(command, "HELLO\n", 5) == 0) { if (is_joined) { //already joined, so skip this say(all_sockets[idx], "ALREADY JOINED DUMMY\n", 0); continue; } int field_size = size_callback(); sprintf(response, "SIZE %i \n", field_size); say(all_sockets[idx], response, 0); if (join_callback() == 0) { is_joined = 1; say(all_sockets[idx], "START\n", 1); }else{ say(all_sockets[idx], "NACK\n", 1); } }else if (strncasecmp(command, "SHUTDOWN\n", 7) == 0) { stopserver(); break; }else if (strncasecmp(command, "TAKE", 4)== 0) { int coords[2]; char name[1000]; parse_take(command, coords, name); int success = take_callback(coords[0], coords[1], idx); if (success == 1) { strcpy(all_names[idx], name); say(all_sockets[idx], "TAKEN \n", 1); }else { say(all_sockets[idx], "INUSE \n", 1); } }else if (strncasecmp(command, "STATUS", 6)== 0) { int coords[2]; parse_status(command, coords); int player_id = status_callback(coords[0], coords[1]); say(all_sockets[idx], all_names[player_id], 1); } command[recv_msg_size] = '\0'; } if (all_sockets[idx] > 0) { close(client_socket); /* Close client socket */ all_sockets[idx] = 0; } return NULL; }
void ConsolidatedTests::TwoPlayersSimple() { beginTest("Two Players Simple"); initialize_field_manager(4); set_delay(0); startserver(65002); register_callbacks(join_game, leave_game, take_cell, get_size, get_cell_player, is_there_a_winner); ScopedPointer<Client> frank = new Client(); frank->setName("Frank"); add_instruction(frank, "sleep", 10); add_instruction(frank, "join", ""); NamedValueSet* instruction = add_instruction(frank, "take", ""); instruction->set("x", 0); instruction->set("y", 1); instruction->set("name", "Frank"); instruction = add_instruction(frank, "take", ""); instruction->set("x", 0); instruction->set("y", 0); instruction->set("name", "Frank"); instruction = add_instruction(frank, "take", ""); instruction->set("x", 1); instruction->set("y", 0); instruction->set("name", "Frank"); instruction = add_instruction(frank, "take", ""); instruction->set("x", 1); instruction->set("y", 1); instruction->set("name", "Frank"); instruction = add_instruction(frank, "status", ""); instruction->set("x", 0); instruction->set("y", 1); add_instruction(frank, "sleep", 2); ScopedPointer<Client> susy = new Client(); susy->setName("Susy"); add_instruction(susy, "sleep", 5); add_instruction(susy, "join", ""); instruction = add_instruction(susy, "take", ""); instruction->set("x", 0); instruction->set("y", 1); instruction->set("name", "Susy"); instruction = add_instruction(susy, "take", ""); instruction->set("x", 0); instruction->set("y", 0); instruction->set("name", "Susy"); instruction = add_instruction(susy, "take", ""); instruction->set("x", 1); instruction->set("y", 0); instruction->set("name", "Susy"); instruction = add_instruction(susy, "take", ""); instruction->set("x", 1); instruction->set("y", 1); instruction->set("name", "Susy"); add_instruction(susy, "sleep", 3); for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { instruction = add_instruction(susy, "take", ""); instruction->set("x", i); instruction->set("y", j); instruction->set("name", "Susy"); } } pool.addJob(frank, false); pool.addJob(susy, false); while(pool.getNumJobs() > 0 ) { Thread::sleep(100); } stopserver(); while( server_running() > 0) { Thread::sleep(100); } release_field_manager(); Thread::sleep(1000); }
virtual void teardown(int roudnds_,int fourtytwo_,int random_) { int rc = ::close(clientsock); SOCK_ERRORCHECK(rc,"close"); stopserver(); }