handmade/test/test_SafeUDPpacket.cpp
Bubblegumdrop d2d7c82ca4 Split SafeUDPpacket and SafeUDPpacketV.
SafeUDPpacketV is much more complex due to the UDP packet splitting machinery.
2022-01-16 17:55:26 -05:00

143 lines
2.6 KiB
C++

#include "debug.h"
#include "glCheckErrors.h"
#include "lib_GL_common.h"
#include "lib_SDL_common.h"
#include "quad.h"
#include "shader.h"
#include "signal_common.h"
#include "unused.h"
#include "UDPbase.h"
#include "FileIO.h"
#include "SafeUDPpacketV.h"
#include "lib_GetOpt.h"
#include <iostream>
#include <fstream>
#include <memory>
#include <vector>
#include <stdlib.h> /* exit */
#include <string.h> /* memset */
#include <unistd.h> /* getopt */
#include <getopt.h> /* getopt */
#define MIN_FRAME_MS 7 /* 144 Hz */
static const char* host = "localhost";
static Uint16 port = 6666;
static int channel = -1;
static std::unique_ptr<UDPbase> client;
struct Test_CommonOpts : public Init_CommonOpts
{
std::string path;
};
static void my_GetOpt (int argc, char** argv, Test_CommonOpts*);
static void my_Usage (const char*);
int
main (int argc, char **argv)
{
{
Test_CommonOpts init_opts;
UDPsocket udpsock;
common_Signal_Init ();
my_GetOpt (argc, argv, &init_opts);
common_SDL_Init ();
client = std::make_unique<UDPbase> (host, port);
if (NULL == (udpsock = client->UDP_Open (0)))
{
SDL_Log ("Unable to open client port");
return -1;
}
if (-1 == (channel = client->UDP_Bind (udpsock, -1)))
{
SDL_Log ("Unable to bind client port");
return -1;
}
std::string buf;
if (init_opts.path == "-")
{
try
{
std::string line;
while (getline (std::cin, line))
{
buf += line + "\n";
}
}
catch (std::ifstream::failure& e)
{
// std::cout << e << std::endl;
}
}
else
{
FileIO f (init_opts.path, "r");
buf = f.ReadToString ();
}
IPaddress* addr = SDLNet_UDP_GetPeerAddress (udpsock, channel);
SafeUDPpacketV pV (&buf[0], buf.length (), DEFAULT_MAX_PACKET_SIZE);
pV.len (DEFAULT_MAX_PACKET_SIZE);
pV.address (*addr);
pV.channel (channel);
pV.Send (udpsock);
}
common_SDL_Quit ();
return 0;
}
static void
my_Usage (const char* argv0)
{
fprintf (stderr, "Usage: %s [-h] [-f [-|path]] [-s server] [-p port]\n", argv0);
}
void
my_GetOpt (int argc, char** argv, Test_CommonOpts* state)
{
int opt;
state->host = "localhost";
state->channel = -1;
state->mtu = DEFAULT_MAX_PACKET_SIZE;
state->port = 6666;
state->path = "test/test_SafeUDPpacket.cpp";
while ((opt = getopt (argc, argv, "f:hm:p:s:")) != -1)
{
switch (opt)
{
case 'f':
state->path = optarg;
break;
case 'm':
state->mtu = strtol (optarg, NULL, 0);
break;
case 'p':
state->port = strtol (optarg, NULL, 0);
break;
case 's':
state->host = optarg;
break;
default:
case 'h':
my_Usage (argv[0]);
exit (EXIT_FAILURE);
break;
}
}
}