Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<SDL.h>
- #include<iostream>
- #include<SDL.h>
- #include<SDL_net.h>
- #include<vector>
- #include<cstring>
- #include<stdio.h>
- struct data{
- TCPsocket socket;
- Uint32 timeout;
- int id;
- data(TCPsocket sock, Uint32 t, int i) : socket(sock), timeout(t), id(i){}
- };
- int main( int argc, char *argv[] ) {
- SDL_Init(SDL_INIT_EVERYTHING);
- SDLNet_Init();
- int curid = 0;
- int playernum = 0;
- std::vector<data> socketvector;
- SDL_Event e;
- IPaddress ip;
- SDLNet_ResolveHost(&ip, NULL, 7777);
- char tmp[1400];
- bool running = true;
- SDLNet_SocketSet sockets = SDLNet_AllocSocketSet(30);
- TCPsocket server = SDLNet_TCP_Open(&ip);
- while(running){
- while(SDL_PollEvent(&e)){
- if(e.type == SDL_QUIT){
- running = false;
- }
- }
- TCPsocket tmpsocket = SDLNet_TCP_Accept(server);
- if(tmpsocket){
- if(playernum < 30){
- SDLNet_TCP_AddSocket(sockets, tmpsocket);
- socketvector.push_back(data(tmpsocket, SDL_GetTicks(), curid));
- playernum++;
- sprintf(tmp, "0 %d \n", curid);
- curid++;
- std::cout << "New connection " << curid <<std::endl;
- }else{
- sprintf(tmp, "3 \n");
- }
- SDLNet_TCP_Send(tmpsocket, tmp, strlen(tmp) + 1);
- }
- while(SDLNet_CheckSockets(sockets, 0) > 0){
- for(int i = 0; i < socketvector.size(); i++){
- if(SDLNet_SocketReady(socketvector[i].socket)){
- socketvector[i].timeout = SDL_GetTicks();
- SDLNet_TCP_Recv(socketvector[i].socket, tmp, 1400);
- int num = tmp[0] - '0';
- int j=1;
- while(tmp[j] >='0' && tmp[j] <= '9'){
- num*=10;
- num+=tmp[j] - '0';
- j++;
- }
- switch(num){
- case 1:
- for(int k = 0; k < socketvector.size(); k++){
- if(k == i){
- continue;
- }
- SDLNet_TCP_Send(socketvector[k].socket, tmp, strlen(tmp) + 1);
- }
- break;
- case 2:
- for(int k = 0; k < socketvector.size(); k++){
- if(k == i){
- continue;
- }
- SDLNet_TCP_Send(socketvector[k].socket, tmp, strlen(tmp) + 1);
- }
- std::cout << "Player " << i << " disconnected" <<std::endl;
- SDLNet_TCP_DelSocket(sockets, socketvector[i].socket);
- SDLNet_TCP_Close(socketvector[i].socket);
- socketvector.erase(socketvector.begin() + i);
- playernum--;
- break;
- }
- }
- }
- }
- for(int j = 0; j < socketvector.size(); j++){
- if(SDL_GetTicks() - socketvector[j].timeout > 5000){
- sprintf(tmp, "2 %d \n", socketvector[j].id);
- for(int k = 0; k < socketvector.size(); k++){
- if(k == j){
- continue;
- }
- SDLNet_TCP_Send(socketvector[k].socket, tmp, strlen(tmp) + 1);
- }
- std::cout << "Player " << j << " disconnected" <<std::endl;
- SDLNet_TCP_DelSocket(sockets, socketvector[j].socket);
- SDLNet_TCP_Close(socketvector[j].socket);
- socketvector.erase(socketvector.begin() + j);
- playernum--;
- }
- }
- SDL_Delay(1);
- }
- for(int i=0; i < socketvector.size(); i++){
- SDLNet_TCP_Close(socketvector[i].socket);
- }
- SDLNet_FreeSocketSet(sockets);
- SDLNet_TCP_Close(server);
- SDLNet_Quit();
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement