1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/*
* Copyright 2016 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/
/*
* Compile with:
*
* gcc -Wall `sdl-config --cflags` sdl2_net_client.c -o sdl2_net_client `sdl-config --libs` -lSDL_net
*
* or
*
* emcc -Wall sdl2_net_client.c -s USE_SDL_NET=2 -s USE_SDL=2 -o sdl2_net_client.js
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "SDL_net.h"
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
typedef enum {
MSG_READ,
MSG_WRITE
} msg_state_t;
typedef struct {
TCPsocket sd; /* Socket descriptor */
msg_state_t msg_state;
int msg_i;
} state_t;
state_t state;
void finish(int result) {
if (state.sd) {
SDLNet_TCP_Close(state.sd);
SDLNet_Quit();
}
#ifdef __EMSCRIPTEN__
REPORT_RESULT(result);
emscripten_force_exit(result);
#else
exit(result);
#endif
}
char *msgs[] = {
"testmsg1",
"anothertestmsg",
"exit",
};
void main_loop()
{
char *sendbuf = msgs[state.msg_i];
char recvbuf[256] = {0};
int actual = 0, len = strlen(sendbuf) + 1;
printf("main loop with string %s and len %d\n", sendbuf, len);
if (state.msg_state == MSG_WRITE) {
printf("trying to send %s\n", sendbuf);
if ((actual = SDLNet_TCP_Send(state.sd, (void *)sendbuf, len)) != len)
{
fprintf(stderr, "SDLNet_TCP_Send: count:%d/%d errno:%d msg:%s\n",
actual, len, errno, SDLNet_GetError());
if (errno == EAGAIN) {
if (actual > 0) {
assert(0);
}
return;
}
finish(EXIT_FAILURE);
}
printf("send success\n");
state.msg_state = MSG_READ;
}
if (state.msg_state == MSG_READ) {
printf("trying to receive %s\n", sendbuf);
if ((actual = SDLNet_TCP_Recv(state.sd, (void *)recvbuf, len)) != len)
{
fprintf(stderr, "SDLNet_TCP_Recv: count:%d/%d errno:%d msg:%s\n",
actual, len, errno, SDLNet_GetError());
if (errno == EAGAIN) {
if (actual > 0) {
assert(0);
}
return;
}
finish(EXIT_FAILURE);
}
printf("receive success\n");
assert(strcmp(sendbuf, recvbuf) == 0);
if (!strcmp(recvbuf, "exit")) {
finish(EXIT_SUCCESS);
}
state.msg_i++;
state.msg_state = MSG_WRITE;
}
}
int main(int argc, char **argv)
{
IPaddress ip; /* Server address */
memset(&state, 0, sizeof(state_t));
state.msg_state = MSG_WRITE;
if (SDLNet_Init() < 0)
{
fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError());
finish(EXIT_FAILURE);
}
/* Resolve the host we are connecting to */
if (SDLNet_ResolveHost(&ip, "localhost", SOCKK) < 0)
{
fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError());
finish(EXIT_FAILURE);
}
/* Open a connection with the IP provided */
if (!(state.sd = SDLNet_TCP_Open(&ip)))
{
fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError());
finish(EXIT_FAILURE);
}
/* Send messages */
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(main_loop, 60, 0);
#else
while (1) main_loop();
#endif
return EXIT_SUCCESS;
}
|