static int get_resp_value(int ret) { char resp[STD_RESP_LEN + 1], **argv; int count, result = -1, argc; count = read(serverfd, resp, STD_RESP_LEN); if (count > 0) { resp[count] = '\0'; argc = str_to_argv(resp, &argv); if (argc == -1) printf_die(stderr, "[ERROR] Cannot allocate buffer... exiting!\n", EXIT_FAILURE); if (argc >= 1 && str_isnumber(argv[0])) { eval_response(atoi(argv[0])); if (argc >= 2 && str_isnumber(argv[1])) result = atoi(argv[1]); else result = 0; } free(argv); } if (ret == -1) printf_die(stdout, "Server probably dead or you have been killed!\n"); return result; }
static int client_init(char * remotehost, char * port) { int result = 1; int ret, sock; struct addrinfo * ai; struct addrinfo hints; memset (&hints, '\0', sizeof(hints)); hints.ai_flags = AI_ADDRCONFIG; hints.ai_socktype = SOCK_STREAM; if(ret = getaddrinfo (remotehost, port, &hints, &ai)) printf_die(stderr, "[ERROR] getaddrinfo('%s', '%s'): %s\n", EXIT_FAILURE, remotehost, port, gai_strerror(ret)); if(!ai) printf_die(stderr, "[ERROR] getaddrinf(): couldn't fill the struct!\n", EXIT_FAILURE); struct addrinfo *runp = ai; while (runp) { sock = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol); if (sock != -1) { ndprintf(stdout, "[NETWORK] Connecting to server...\n"); if(connect (sock, runp->ai_addr, runp->ai_addrlen) == 0) { ndprintf(stdout, "[NETWORK] connected to server\n"); serverfd = sock; result = get_resp_value(0); break; } close(sock); } runp = runp->ai_next; } freeaddrinfo(ai); return result; }
static int client_init(char *remotehost, char *port) { int ret, sock = -1; struct addrinfo *ai, *runp; struct addrinfo hints; #ifdef WIN32 WSADATA wd; int err = WSAStartup(MAKEWORD(2,2), &wd); fprintf(stderr, "WSA startup: %d\n", err); if (err != 0) printf_die(stderr, "[ERROR] windows socket init failed: %d\n", EXIT_FAILURE, err); #endif memset(&hints, '\0', sizeof(hints)); hints.ai_flags = AI_ADDRCONFIG; hints.ai_socktype = SOCK_STREAM; ret = getaddrinfo(remotehost, port, &hints, &ai); if (ret) printf_die(stderr, "[ERROR] getaddrinfo('%s', '%s'): %s (%d)\n", EXIT_FAILURE, remotehost, port, gai_strerror(ret), ret); if (!ai) printf_die(stderr, "[ERROR] getaddrinfo(): no address returned\n", EXIT_FAILURE); runp = ai; while (runp) { sock = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol); if (sock != -1) { ndprintf(stdout, "[NETWORK] Connecting to server...\n"); if (connect(sock, runp->ai_addr, runp->ai_addrlen) == 0) { ndprintf(stdout, "[NETWORK] Connected.\n"); serverfd = sock; receive_config(); break; } close(sock); sock = -1; } runp = runp->ai_next; } freeaddrinfo(ai); return (sock < 0); }
static int get_resp_value(int ret) { int value = -1; get_resp(&value, 1); if (ret == -1) printf_die(stdout, "Server dead or you have been killed\n", 1); return value; }
static int get_resp(int *resp, int count) { char buf[STD_RESP_LEN + 1], *tmp; char **argv; int len, cmd, i; len = read(serverfd, buf, STD_RESP_LEN); if (len > 0) { buf[len] = '\0'; tmp = get_command(buf, &cmd); switch (cmd) { case DEAD: printf_die(stdout, "Killed: %s\n", 1, *tmp ? tmp : "unknown reason"); break; case OK: break; case ERROR: printf_die(stderr, "Error: %s\n", 2, *tmp ? tmp : "unknown error"); break; default: printf_die(stderr, "Error: unexpected response from the server\n", 2); break; } argv = malloc(count * sizeof(char *)); count = tokenize_args(tmp, count, argv); for (i = 0; i < count; i++) { if (str_isnumber(argv[i])) resp[i] = atoi(argv[i]); else resp[i] = -1; } free(argv); } else count = 0; return count; }
static int eval_response(int resp) { switch (resp) { case END: sockwrite(serverfd, END, NULL); printf_die(stdout, "You win!\n", 0); break; case DEAD: printf_die(stdout, "You are dead!\n", 1); break; case START: case OK: break; case DRAW: printf_die(stdout, "Game ended wih a draw!\n", 0); break; case ERROR: default: printf_die(stderr, "Error detected.. exiting!\n", 2); break; } return resp; }
void init_custom(char *img_path, int argc, char **argv) { int retval; char *remotehost = DEFAULT_REMOTEHOST; char *port = DEFAULT_PORT; while ((retval = getopt(argc, argv, "dn:hH:P:")) != -1) { switch (retval) { case 'H': remotehost = optarg; break; case 'P': port = optarg; break; case 'd': debug = 1; break; case 'h': usage(argv[0], EXIT_SUCCESS); break; default: break; } } if (argc > optind) usage(argv[0], EXIT_FAILURE); #ifndef WIN32 signal(SIGPIPE, SIG_IGN); #endif if (client_init(remotehost, port)) printf_die(stderr, "Could not connect to %s:%s\n", EXIT_FAILURE, remotehost, port); #ifdef WIN32 srand(time(NULL) + GetCurrentProcessId()); #else srandom(time(NULL) + getpid()); srand(time(NULL) + getpid()); #endif set_default_name(argv[0]); if (img_path) image(img_path); start(); }
void get_all(int *_loc_x, int *_loc_y, int *_damage, int *_speed, int *_elapsed) { int ret, count; int values[5]; ret = sockwrite(serverfd, GET_ALL, NULL); count = get_resp(values, 5); if (ret == -1 || count != 5) printf_die(stdout, "Server dead or you have been killed\n", 1); if (_loc_x) *_loc_x = values[0]; if (_loc_y) *_loc_y = values[1]; if (_damage) *_damage = values[2]; if (_speed) *_speed = values[3]; if (_elapsed) *_elapsed= values[4]; }
int main (int argc, char **argv) { int retval; char * remotehost = DEFAULT_REMOTEHOST, * port = DEFAULT_PORT; while ((retval = getopt(argc, argv, "dn:hH:P:")) != -1) { switch (retval) { case 'H': remotehost = optarg; break; case 'P': port = optarg; break; case 'd': debug = 1; break; case 'h': usage(argv[0], EXIT_SUCCESS); break; default: break; } } if (argc > optind) /* || !hostname || !port)*/ usage(argv[0], EXIT_FAILURE); signal(SIGPIPE, SIG_IGN); if(client_init(remotehost, port)) printf_die(stderr, "could not connect to : %s:%s\n", EXIT_FAILURE, remotehost, port); srandom(time(NULL) + getpid()); srand(time(NULL) + getpid()); rmain (); }