bool GameServer::initialize() { std::cout << "Listening on " << sf::IpAddress::getLocalAddress() << ":44000" << std::endl; // Create a listener to wait for incoming connections on port 44000 if (serverSocket.listen(44000) != sf::Socket::Done) { return false; } // Wait for client connections if (!waitForClients()) { return false; } // Don't block when checking for data from now on client[0].socket.setBlocking(false); client[1].socket.setBlocking(false); // Inform the clients the game is starting sf::Packet p; p << Message::GameStarted; // Always send message type first sendMessage(p); std::cout << "Starting Game" << std::endl; state = GameState::Preparatory; countdown = 3.0f; // Receive ball hit top/bottom notifications from Stage stage.setObserver(this); return true; }
void Server::startSession(const char *name) { sessCurrent = new Session(name); // Listen if((listen(fdServerSocket, SERVER_BACKLOG)) == -1) printErr("Listen"); waitForClients(); }
/** * @brief Main function * * This function is the main entry point of the program. - * it listens on a port provided by argv and accepts incoming tcp-connections * * \param argc the number of arguments * \param argv the arguments itselves (including the program name in argv[0]) * * @return exit status of program * @retval EXIT_FAILURE Program terminated due to a failure * @retval EXIT_SUCCESS Program terminated successfully * */ int main(int argc, const char * argv[]) { programName = argv[0]; if(argc < 2) { printUsage(); exit(EXIT_FAILURE); } const char *tcpPort = getTcpPort(argc, argv); if (tcpPort == NULL) { exit(EXIT_FAILURE); } INFO("main()", "using tcp port %s", tcpPort); struct addrinfo *addrInfoResult, hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; int result; if ((result = getaddrinfo(NULL, tcpPort, &hints, &addrInfoResult)) != SUCCESS) { fprintf(stderr, "%s: getaddrinfo(): %s\n", programName, gai_strerror(result)); exit(EXIT_FAILURE); } INFO("main()", "getaddrinfo succeeded %s", ""); int listening_socket_descriptor = 0; struct addrinfo *serverCandidate; int optionValue = 1; /* we use the first addrinfo which works */ for (serverCandidate = addrInfoResult; serverCandidate != NULL; serverCandidate = serverCandidate->ai_next) { listening_socket_descriptor = socket(serverCandidate->ai_family, serverCandidate->ai_socktype, serverCandidate->ai_protocol); if (listening_socket_descriptor == ERROR) { INFO("main()", "failed creating a socket for %d, %d, %d", serverCandidate->ai_family, serverCandidate->ai_socktype, serverCandidate->ai_protocol); continue; } /* Setting the SO_REUSEADDR option means that we can bind a socket to a local port even if another TCP is bound to the same port in either of the scenarios described at the start of this section. Most TCP servers should enable this option. The Linux Programming Interface , p1280 */ if (setsockopt(listening_socket_descriptor, SOL_SOCKET, SO_REUSEADDR, &optionValue, sizeof(int)) == ERROR) { fprintf(stderr, "%s: getaddrinfo(): %s\n", programName, gai_strerror(result)); freeaddrinfo(serverCandidate); freeaddrinfo(addrInfoResult); close(listening_socket_descriptor); exit(EXIT_FAILURE); } if (bind(listening_socket_descriptor, serverCandidate->ai_addr, serverCandidate->ai_addrlen) == ERROR) { /* could not bind, try next addrInfo */ INFO("main()", "failed to bind %s", ""); continue; } /* if we came here we had no error, so let's go on */ break; } INFO("main()", "freeing addrInfoResult %s", ""); freeaddrinfo(addrInfoResult); if (serverCandidate == NULL) { fprintf(stderr, "%s: failed to bind: %s\n", programName, strerror(errno)); close(listening_socket_descriptor); exit(EXIT_FAILURE); } INFO("main()", "start listening %s", ""); if (listen(listening_socket_descriptor, BACKLOG_SIZE) == ERROR) { fprintf(stderr, "%s: failed to listen: %s\n", programName, strerror(errno)); close(listening_socket_descriptor); exit(EXIT_FAILURE); } INFO("main()", "attaching signal handler %s", ""); struct sigaction onSignalAction; memset(&onSignalAction, 0, sizeof(onSignalAction)); onSignalAction.sa_handler = handleChildSignals; onSignalAction.sa_flags = SA_RESTART; /* register handler for SIGCHLD: child status has changed */ if (sigaction(SIGCHLD, &onSignalAction, NULL) == ERROR) { fprintf(stderr, "%s: failed to create signal handler: %s\n", programName, strerror(errno)); close(listening_socket_descriptor); exit(EXIT_FAILURE); } waitForClients(listening_socket_descriptor); /* not needed, here for convention */ exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { daemonize(); storepid();//Lagra pid nummer för att kunna stänga med stop-skript srand(time(NULL)); gameOver = true; ghostHitCount = 0; r3p1v.x = 180; r3p1v.y = 97; r3p1v.w = 16; r3p1v.h = 50; r3p1h.x = 440; r3p1h.y = 97; r3p1h.w = 16; r3p1h.h = 50; r2p0v.x = 0; r2p0v.y = 195; r2p0v.w = 16; r2p0v.h = 50; r2p0h.x = 270; r2p0h.y = 195; r2p0h.w = 16; r2p0h.h = 50; r2p1v.x = 370; r2p1v.y = 195; r2p1v.w = 16; r2p1v.h = 50; r2p1h.x = 622; r2p1h.y = 195; r2p1h.w = 16; r2p1h.h = 50; r1p1v.x = 190; r1p1v.y = 297; r1p1v.w = 16; r1p1v.h = 50; r1p1h.x = 450; r1p1h.y = 297; r1p1h.w = 16; r1p1h.h = 50; r0p0v.x = 0; r0p0v.y = 415; r0p0v.w = 16; r0p0v.h = 50; r0p0h.x = 622; r0p0h.y = 415; r0p0h.w = 16; r0p0h.h = 50; ghostRect1.x = 300; ghostRect1.y = 97; ghostRect1.w = 50; ghostRect1.h = 50; ghostRect2.x = 50; ghostRect2.y = 195; ghostRect2.w = 50; ghostRect2.h = 50; ghostRect3.x = 500; ghostRect3.y = 195; ghostRect3.w = 50; ghostRect3.h = 50; ghostRect4.x = 300; ghostRect4.y = 297; ghostRect4.w = 50; ghostRect4.h = 50; ghostRect5.x = 400; ghostRect5.y = 415; ghostRect5.w = 50; ghostRect5.h = 50; IPaddress ip, *remoteIP; int quit, quit2; char buffer[512]; bool clientInitiated; firstPosition = false; SDL_Thread *client1, *client2, *enemy1, *enemy2, *enemy3, *enemy4, *enemy5; positionSetMutex = SDL_CreateMutex(); ghostHitMutex = SDL_CreateMutex(); if(!positionSetMutex) { return 0; } if(!ghostHitMutex) { return 0; } initFunctions(&ip, &sd); //Initiera TCP för SDL enemy1 = SDL_CreateThread(nextMove, "ghost1", &ghostRect1); enemy2 = SDL_CreateThread(nextMove, "ghost1", &ghostRect2); enemy3 = SDL_CreateThread(nextMove, "ghost1", &ghostRect3); enemy4 = SDL_CreateThread(nextMove, "ghost1", &ghostRect4); enemy5 = SDL_CreateThread(nextMove, "ghost1", &ghostRect5); while(true) { if(gameOver == true) { waitForClients(&sd); // Väntar på 2 st klienter ska koppla upp sig resetVariables(); gameOver = false; client1Position = 0; client1 = SDL_CreateThread(startClient, "Client1", (void *)NULL); client2 = SDL_CreateThread(startClient, "Client2", (void *)NULL); SDL_DetachThread(client1); // Förhindrar att trÃ¥den tar upp minne efter att den stänger ner SDL_DetachThread(client2); } SDL_Delay(1000); } SDLNet_TCP_Close(sd); SDLNet_Quit(); return EXIT_SUCCESS; }