Skip to content

Commit

Permalink
fail gracefully if bot server is unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhughes27 committed Dec 27, 2016
1 parent 4b1b6df commit 9dbddef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
6 changes: 0 additions & 6 deletions README.md
Expand Up @@ -35,9 +35,3 @@ assuming this repo is located in a directory called `src` in your home directory
Testing
-------
An example server is included in `/test/server.py`. This file is a minimal example that allows you to hard code the joystick input. This example is intended to be extracted into a program that determines the appropriate input somehow.


ToDo
----
* correct the joytick input (follow the sdl plugin code)
* add rescue from failed to connect (should warn.)
20 changes: 18 additions & 2 deletions src/controller.c
Expand Up @@ -37,15 +37,31 @@ int socket_connect(char *host, int portno) {
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);

/* connect the socket */
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
DebugMessage(M64MSG_ERROR, "ERROR connecting");
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
DebugMessage(M64MSG_INFO, "ERROR connecting, please start bot server.");
return -1;
}

return sockfd;
}

void clear_controller() {
controller[0].buttons.X_AXIS = 0;
controller[0].buttons.Y_AXIS = 0;

controller[0].buttons.A_BUTTON = 0;
controller[0].buttons.B_BUTTON = 0;
controller[0].buttons.R_TRIG = 0;
}

void read_controller() {
int sockfd = socket_connect(HOST, PORT);

if (sockfd == -1) {
clear_controller();
return;
}

int bytes, sent, received, total;
char message[1024], response[4096]; // allocate more space than required.
sprintf(message, "GET / HTTP/1.0\r\n\r\n");
Expand Down

0 comments on commit 9dbddef

Please sign in to comment.