Advertisement
Benjamin_Loison

cat Extensions/Network/Main/client.cpp

Jul 8th, 2017
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. altiscraft@overclock3000:~/DEV/AltisCraft.fr/Client$ cat Extensions/Network/Main/client.cpp
  2. #ifdef __linux__
  3.     #include <sys/types.h>
  4.     #include <sys/socket.h>
  5.     #include <netinet/in.h>
  6.     #include <netinet/ip.h>
  7.     #include <unistd.h>
  8.     #include <stdlib.h>
  9.     #include <arpa/inet.h>
  10.     #include <string.h>
  11.     #include <fcntl.h>
  12.     #define SOCKET int
  13.     #define SOCKADDR_IN struct sockaddr_in
  14. #endif
  15. #ifdef _WIN32
  16.     #include <winsock2.h>
  17. #endif
  18. #include <cstdio>
  19. #include <iostream>
  20. #include <thread>
  21. #include <string>
  22. #include "../Chat/chat.h"
  23. #include "../../Logger/logger.h"
  24. #include "../../AltisCraft.fr/Map/map.h"
  25. using namespace std;
  26.  
  27. void initializeNetwork(), init(), connection(), receive(), sendStr(), closeConnection(), verifyDisconnexion();
  28. SOCKET socketId;
  29. SOCKADDR_IN destination;
  30. char buffer[65535];
  31. thread network, disconnected;
  32. bool connected = true;
  33.  
  34. void initializeNetwork()
  35. {
  36.     network = thread(&init);
  37.     initializeChat();
  38. }
  39.  
  40. void init()
  41. {
  42.     #ifdef _WIN32
  43.         WSADATA initWin32;
  44.         WSAStartup(MAKEWORD(2, 2), &initWin32);
  45.     #endif
  46.     socketId = socket(AF_INET, SOCK_STREAM, 0);
  47.     destination.sin_family = AF_INET; // inet_addr("199.188.236.18")
  48.     destination.sin_addr.s_addr = *(u_long*)gethostbyname("altiscraft.fr")->h_addr_list[0];
  49.     // inet_ntoa(addr) <- ipv4
  50.     destination.sin_port = htons(33333);
  51.     connection();
  52.     disconnected = thread(&verifyDisconnexion);
  53.     receive();
  54.     closeConnection();
  55. }
  56.  
  57. void sendStr(string str)
  58. {
  59.     strcpy(buffer, str.c_str());
  60.     send(socketId, buffer, strlen(buffer), 0);
  61. }
  62.  
  63. void connection()
  64. {
  65.     if(connect(socketId, (struct sockaddr*)&destination, sizeof(destination)) != 0)
  66.     {
  67.         log("Server not connected");
  68.         connected = 0;
  69.     }
  70. }
  71.  
  72. void receive()
  73. {
  74.     #ifdef _WIN32
  75.         u_long mode = 1;
  76.         ioctlsocket(socketId, FIONBIO, &mode);
  77.     #endif
  78.     #ifdef __linux__
  79.         fcntl(socketId, F_SETFL, O_NONBLOCK);
  80.     #endif
  81.     sendStr("MAJ Map");
  82.     while(1)
  83.     {
  84.         int length = recv(socketId, buffer, 1515, 0);
  85.         if(length > 0)
  86.         {
  87.             buffer[length] = 0;
  88.             string prefix("MAJ Map\n"), msg = buffer;
  89.             log("Received: " + msg);
  90.             if(!msg.compare(0, prefix.size(), prefix))
  91.                 loadMap(msg.substr(prefix.size()));
  92.             else
  93.             {
  94.                 log("Msg: " + msg);
  95.                 messageList.push_back(msg);
  96.             }
  97.         }
  98.         if(connected == 0)
  99.             break;
  100.     }
  101. }
  102.  
  103. void verifyDisconnexion()
  104. {
  105.     char bufferTmp[1];
  106.     while(1)
  107.     {
  108.         int lengthTemp = recv(socketId, bufferTmp, 1, 0), error = WSAGetLastError();
  109.         if(lengthTemp == 0 && error == 0)
  110.             sendStr(" ");
  111.         if(error == 10053)
  112.         {
  113.             log("Server closed");
  114.             connected = 0;
  115.             break;
  116.         }
  117.     }
  118. }
  119.  
  120. void closeConnection() /// FINE ? (like server)
  121. {
  122.     shutdown(socketId, 2);
  123.     #ifdef _WIN32
  124.         closesocket(socketId);
  125.         WSACleanup();
  126.     #endif
  127.     #ifdef __linux__
  128.         close(socketId);
  129.     #endif
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement