Advertisement
Guest User

Server

a guest
Jun 9th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.11 KB | None | 0 0
  1. #include<SDL.h>
  2.  
  3. #include<iostream>
  4. #include<SDL.h>
  5. #include<SDL_net.h>
  6. #include<vector>
  7. #include<cstring>
  8. #include<stdio.h>
  9.  
  10. struct data{
  11.     TCPsocket socket;
  12.     Uint32 timeout;
  13.     int id;
  14.     data(TCPsocket sock, Uint32 t, int i) : socket(sock), timeout(t), id(i){}
  15. };
  16.  
  17. int main( int argc, char *argv[] ) {
  18.     SDL_Init(SDL_INIT_EVERYTHING);
  19.     SDLNet_Init();
  20.     int curid = 0;
  21.     int playernum = 0;
  22.     std::vector<data> socketvector;
  23.  
  24.     SDL_Event e;
  25.  
  26.     IPaddress ip;
  27.     SDLNet_ResolveHost(&ip, NULL, 7777);
  28.  
  29.     char tmp[1400];
  30.  
  31.     bool running = true;
  32.  
  33.     SDLNet_SocketSet sockets = SDLNet_AllocSocketSet(30);
  34.     TCPsocket server = SDLNet_TCP_Open(&ip);
  35.  
  36.     while(running){
  37.         while(SDL_PollEvent(&e)){
  38.             if(e.type == SDL_QUIT){
  39.                 running = false;
  40.             }
  41.         }
  42.  
  43.         TCPsocket tmpsocket = SDLNet_TCP_Accept(server);
  44.         if(tmpsocket){
  45.             if(playernum < 30){
  46.                 SDLNet_TCP_AddSocket(sockets, tmpsocket);
  47.                 socketvector.push_back(data(tmpsocket, SDL_GetTicks(), curid));
  48.                 playernum++;
  49.                 sprintf(tmp, "0 %d \n", curid);
  50.                 curid++;
  51.                 std::cout << "New connection " << curid <<std::endl;
  52.             }else{
  53.                 sprintf(tmp, "3 \n");
  54.             }
  55.             SDLNet_TCP_Send(tmpsocket, tmp, strlen(tmp) + 1);
  56.         }
  57.  
  58.         while(SDLNet_CheckSockets(sockets, 0) > 0){
  59.             for(int i = 0; i < socketvector.size(); i++){
  60.                 if(SDLNet_SocketReady(socketvector[i].socket)){
  61.                     socketvector[i].timeout = SDL_GetTicks();
  62.                     SDLNet_TCP_Recv(socketvector[i].socket, tmp, 1400);
  63.                     int num = tmp[0] - '0';
  64.                     int j=1;
  65.                     while(tmp[j] >='0' && tmp[j] <= '9'){
  66.                         num*=10;
  67.                         num+=tmp[j] - '0';
  68.                         j++;
  69.                     }
  70.                     switch(num){
  71.                     case 1:
  72.                         for(int k = 0; k < socketvector.size(); k++){
  73.                             if(k == i){
  74.                                 continue;
  75.                             }
  76.                             SDLNet_TCP_Send(socketvector[k].socket, tmp, strlen(tmp) + 1);
  77.                         }
  78.                     break;
  79.                     case 2:
  80.                         for(int k = 0; k < socketvector.size(); k++){
  81.                             if(k == i){
  82.                                 continue;
  83.                             }
  84.                             SDLNet_TCP_Send(socketvector[k].socket, tmp, strlen(tmp) + 1);
  85.                         }
  86.                         std::cout << "Player " << i << " disconnected" <<std::endl;
  87.                         SDLNet_TCP_DelSocket(sockets, socketvector[i].socket);
  88.                         SDLNet_TCP_Close(socketvector[i].socket);
  89.                         socketvector.erase(socketvector.begin() + i);
  90.                         playernum--;
  91.                     break;
  92.                     }
  93.                 }
  94.             }
  95.  
  96.         }
  97.  
  98.         for(int j = 0; j < socketvector.size(); j++){
  99.            if(SDL_GetTicks() - socketvector[j].timeout > 5000){
  100.                 sprintf(tmp, "2 %d \n", socketvector[j].id);
  101.                 for(int k = 0; k < socketvector.size(); k++){
  102.                     if(k == j){
  103.                         continue;
  104.                     }
  105.                     SDLNet_TCP_Send(socketvector[k].socket, tmp, strlen(tmp) + 1);
  106.                 }
  107.                 std::cout << "Player " << j << " disconnected" <<std::endl;
  108.                 SDLNet_TCP_DelSocket(sockets, socketvector[j].socket);
  109.                 SDLNet_TCP_Close(socketvector[j].socket);
  110.                 socketvector.erase(socketvector.begin() + j);
  111.                 playernum--;
  112.            }
  113.         }
  114.         SDL_Delay(1);
  115.     }
  116.  
  117.     for(int i=0; i < socketvector.size(); i++){
  118.         SDLNet_TCP_Close(socketvector[i].socket);
  119.     }
  120.  
  121.     SDLNet_FreeSocketSet(sockets);
  122.     SDLNet_TCP_Close(server);
  123.  
  124.     SDLNet_Quit();
  125.     SDL_Quit();
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement