Advertisement
atm959

A

Mar 1st, 2020
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. #include <WinSock2.h>
  2. #include <stdio.h>
  3.  
  4. #pragma comment(lib, "Ws2_32.lib")
  5.  
  6. typedef unsigned int uint32;
  7. typedef unsigned short uint16;
  8. typedef float float32;
  9.  
  10. struct IP_Endpoint {
  11.     uint32 address;
  12.     uint16 port;
  13. };
  14.  
  15. enum {
  16.     PACKETTYPE_JOIN = 1,
  17.     PACKETTYPE_LEAVE = 2,
  18.     PACKETTYPE_SCORE = 3
  19. };
  20.  
  21. enum {
  22.     PACKETTYPE_JOINRESULT = 1,
  23.     PACKETTYPE_PLAYERSCORE = 2
  24. };
  25.  
  26. #define MAX_CLIENTS 2
  27.  
  28. IP_Endpoint client_endpoints[MAX_CLIENTS];
  29. int client_scores[MAX_CLIENTS];
  30.  
  31. int main(int argc, char** argv) {
  32.     WORD winsock_version = 0x202;
  33.     WSADATA winsock_data;
  34.     if (WSAStartup(winsock_version, &winsock_data)) {
  35.         printf("WSAStartup failed: %d\n", WSAGetLastError());
  36.         return -1;
  37.     }
  38.  
  39.     int address_family = AF_INET;
  40.     int type = SOCK_DGRAM;
  41.     int protocol = IPPROTO_UDP;
  42.     SOCKET sock = socket(address_family, type, protocol);
  43.     if (sock == INVALID_SOCKET) {
  44.         printf("socket failed: %d\n", WSAGetLastError());
  45.         return -1;
  46.     }
  47.  
  48.     SOCKADDR_IN local_address;
  49.     local_address.sin_family = AF_INET;
  50.     local_address.sin_port = htons(11000);
  51.     local_address.sin_addr.s_addr = INADDR_ANY;
  52.     if (bind(sock, (SOCKADDR*)&local_address, sizeof(local_address)) == SOCKET_ERROR) {
  53.         printf("bind failed: %d\n", WSAGetLastError());
  54.         return -1;
  55.     }
  56.  
  57.     u_long enabled = 1;
  58.     ioctlsocket(sock, FIONBIO, &enabled);
  59.  
  60.     char buffer[1024];
  61.  
  62.     while (true) {
  63.         int flags = 0;
  64.         SOCKADDR_IN from;
  65.         int from_size = sizeof(from);
  66.         int bytes_received = recvfrom(sock, buffer, 1024, flags, (SOCKADDR*)&from, &from_size);
  67.  
  68.         if (bytes_received != SOCKET_ERROR) {
  69.             int error = WSAGetLastError();
  70.             if (error == WSAEWOULDBLOCK) {
  71.                 printf("recvfrom returned SOCKET_ERROR, WSAGetLastError() %d", WSAGetLastError());
  72.             } else {
  73.  
  74.                 IP_Endpoint from_endpoint;
  75.                 from_endpoint.address = from.sin_addr.S_un.S_addr;
  76.                 from_endpoint.port = from.sin_port;
  77.  
  78.                 switch (buffer[0]) {
  79.                 case PACKETTYPE_JOIN:
  80.                     printf("PACKETTYPE_JOIN\n");
  81.  
  82.                     uint16 slot;
  83.                     slot = uint16(-1);
  84.                     for (uint16 i = 0; i < MAX_CLIENTS; ++i) {
  85.                         if (client_endpoints[i].address == 0) {
  86.                             slot = i;
  87.                             break;
  88.                         }
  89.                     }
  90.  
  91.                     printf("Use slot %d\n", slot);
  92.  
  93.                     buffer[0] = PACKETTYPE_JOINRESULT;
  94.                     if (slot != uint16(-1)) {
  95.                         buffer[1] = 1;
  96.                         memcpy(&buffer[2], &slot, 2);
  97.  
  98.                         flags = 0;
  99.                         if (sendto(sock, buffer, 4, flags, (SOCKADDR*)&from, from_size) != SOCKET_ERROR) {
  100.                             client_endpoints[slot] = from_endpoint;
  101.                             client_scores[slot] = 0;
  102.                         }
  103.                     } else {
  104.                         buffer[1] = 0;
  105.  
  106.                         flags = 0;
  107.                         sendto(sock, buffer, 2, flags, (SOCKADDR*)&from, from_size);
  108.                     }
  109.                     break;
  110.  
  111.                 case PACKETTYPE_LEAVE:
  112.                     printf("PACKETTYPE_LEAVE\n");
  113.                     uint16 slot2;
  114.                     memcpy(&slot2, &buffer[1], 2);
  115.  
  116.                     printf("Clear slot %d\n", slot2);
  117.  
  118.                     client_endpoints[slot2] = {};
  119.                     break;
  120.  
  121.                 case PACKETTYPE_SCORE:
  122.                     printf("PACKETTYPE_SCORE\n");
  123.                     uint16 slot3;
  124.                     memcpy(&slot3, &buffer[1], 2);
  125.                     int score;
  126.                     memcpy(&score, &buffer[3], 4);
  127.                     client_scores[slot3] = score;
  128.  
  129.                     printf("Slot %d score: %d\n", slot3, score);
  130.  
  131.                     SOCKADDR_IN to;
  132.                     to.sin_family = AF_INET;
  133.                     int to_length = sizeof(to);
  134.  
  135.                     buffer[0] = PACKETTYPE_PLAYERSCORE;
  136.  
  137.                     for (uint16 i = 0; i < MAX_CLIENTS; i++) {
  138.                         if (client_endpoints[i].address) {
  139.                             to.sin_addr.S_un.S_addr = client_endpoints[i].address;
  140.                             to.sin_port = client_endpoints[i].port;
  141.  
  142.                             sendto(sock, buffer, 7, flags, (SOCKADDR*)&to, to_length);
  143.                         }
  144.                     }
  145.                     break;
  146.                 }
  147.             }
  148.         }
  149.  
  150.         memset(&buffer, 0, sizeof(buffer));
  151.     }
  152.  
  153.     return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement