1 /*******************************************************************************************
2 *
3 *   raylib [network] example - UDP Client
4 *
5 *   This example has been created using raylib 3.0 (www.raylib.com)
6 *   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
7 *
8 *   Copyright (c) 2019-2020 Jak Barnes (@syphonx) and Ramon Santamaria (@raysan5)
9 *
10 ********************************************************************************************/
11 
12 #include "raylib.h"
13 
14 #define RNET_IMPLEMENTATION
15 #include "rnet.h"
16 
main(void)17 int main(void)
18 {
19     // Initialization
20     //--------------------------------------------------------------------------------------
21     const int screenWidth = 800;
22     const int screenHeight = 450;
23 
24     InitWindow(screenWidth, screenHeight, "raylib [network] example - udp client");
25 
26     InitNetworkDevice();    // Init network communications
27 
28     const char *pingmsg = "Ping!";
29     const char *pongmsg = "Pong!";
30 
31     bool ping = true;
32     bool pong = false;
33     float elapsed = 0.0f;
34     float delay = 1.0f;
35 
36     SocketConfig clientConfig = {
37         .host = "127.0.0.1",
38         .port = "4950",
39         .type = SOCKET_UDP,
40         .nonblocking = true
41     };
42 
43     SocketResult *clientResult = NULL;
44     SocketSet *socketSet = NULL;
45     char receiveBuffer[512] = { 0 };
46 
47     // Create the client: getaddrinfo + socket + setsockopt + connect (TCP only)
48     clientResult = LoadSocketResult();
49     if (!SocketCreate(&clientConfig, clientResult))
50     {
51         TraceLog(LOG_WARNING, "Failed to open client: status %d, errno %d", clientResult->status, clientResult->socket->status);
52     }
53 
54     //  Create and add sockets to the socket set
55     socketSet = LoadSocketSet(1);
56     AddSocket(socketSet, clientResult->socket);
57 
58     SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
59     //--------------------------------------------------------------------------------------
60 
61     // Main game loop
62     while (!WindowShouldClose())    // Detect window close button or ESC key
63     {
64         // Update
65         //----------------------------------------------------------------------------------
66         // Once connected to the network, check the sockets for pending information
67         // and when information is ready, send either a Ping or a Pong.
68 
69         // CheckSockets, if any of the sockets in the socketSet are pending (received data, or requests)
70         // then mark the socket as being ready. You can check this with IsSocketReady(clientResult->socket)
71         int active = CheckSockets(socketSet, 0);
72         if (active != 0) TraceLog(LOG_INFO, "There are currently %d socket(s) with data to be processed.", active);
73 
74         // IsSocketReady, if the socket is ready, attempt to receive data from the socket
75         int bytesRecv = 0;
76         if (IsSocketReady(clientResult->socket)) bytesRecv = SocketReceive(clientResult->socket, receiveBuffer, (int)strlen(pingmsg) + 1);
77 
78         // If we received data, was that data a "Ping!" or a "Pong!"
79         if (bytesRecv > 0)
80         {
81             if (strcmp(receiveBuffer, pingmsg) == 0) { pong = true; }
82             if (strcmp(receiveBuffer, pongmsg) == 0) { ping = true; }
83         }
84 
85         // After each delay has expired, send a response "Ping!" for a "Pong!" and vice-versa
86         elapsed += GetFrameTime();
87         if (elapsed > delay)
88         {
89             if (ping)
90             {
91                 ping = false;
92                 SocketSend(clientResult->socket, pingmsg, (int)strlen(pingmsg) + 1);
93             }
94             else if (pong)
95             {
96                 pong = false;
97                 SocketSend(clientResult->socket, pongmsg, (int)strlen(pongmsg) + 1);
98             }
99 
100             elapsed = 0.0f;
101         }
102         //----------------------------------------------------------------------------------
103 
104         // Draw
105         //----------------------------------------------------------------------------------
106         BeginDrawing();
107 
108             ClearBackground(RAYWHITE);
109 
110             // TODO: Draw relevant connection info
111 
112         EndDrawing();
113         //----------------------------------------------------------------------------------
114     }
115 
116     // De-Initialization
117     //--------------------------------------------------------------------------------------
118     CloseNetworkDevice();   // Close network communication
119 
120     CloseWindow();          // Close window and OpenGL context
121     //--------------------------------------------------------------------------------------
122 
123     return 0;
124 }