93 lines
2.8 KiB
C
93 lines
2.8 KiB
C
|
#include "SS13ArcadeEnv.h"
|
||
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdbool.h>
|
||
|
#include <sys/socket.h>
|
||
|
#include <netinet/in.h>
|
||
|
#include <arpa/inet.h>
|
||
|
#include <unistd.h>
|
||
|
#include <errno.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
int SetupServer(int* socket_fd, int port) {
|
||
|
printf("Starting Server\n");
|
||
|
*socket_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||
|
struct sockaddr_in server_addr;
|
||
|
server_addr.sin_family = AF_INET;
|
||
|
server_addr.sin_port = htons(port);
|
||
|
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||
|
|
||
|
int bound = bind(*socket_fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
|
||
|
if(bound < 0) {
|
||
|
printf("Bind error: %s (%i)\n", strerror(errno), errno);
|
||
|
return -1;
|
||
|
}
|
||
|
listen(*socket_fd, 5);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int StartEnv(int port) {
|
||
|
int socket_fd;
|
||
|
int setup = SetupServer(&socket_fd, port);
|
||
|
if(setup < 0) {
|
||
|
printf("Error setting up server\n");
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
printf("Waiting for connections\n");
|
||
|
struct sockaddr_in client_addr;
|
||
|
int client_size = sizeof(client_addr);
|
||
|
int client_sock = accept(socket_fd, (struct sockaddr*)&client_addr, &client_size);
|
||
|
printf("Connection received\n");
|
||
|
|
||
|
int obs_size = sizeof(struct Observation);
|
||
|
struct SS13ArcadeEnv env;
|
||
|
CreateEnvironment(&env);
|
||
|
while(true) {
|
||
|
char buffer[5];
|
||
|
int bytes_received;
|
||
|
bytes_received = recv(client_sock, buffer, sizeof(buffer), 0);
|
||
|
if(bytes_received < 0) {
|
||
|
printf("Receive Error: %s (%d)\n", strerror(errno), errno);
|
||
|
} else if(bytes_received == 0) {
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
if(buffer[0] == 0x0) { // Step
|
||
|
uint32_t action;
|
||
|
memcpy(&action, buffer+1, sizeof(uint32_t));
|
||
|
action = ntohl(action);
|
||
|
struct Observation obs;
|
||
|
Step(&env, action, &obs);
|
||
|
|
||
|
//printf("Action: %i \n", action);
|
||
|
//printf("-- Observation --\n");
|
||
|
//printf("Player HP: %i Player MP: %i\n", obs.state.player_hp, obs.state.player_mp);
|
||
|
//printf("Enemy HP: %i Enemy MP: %i\n", obs.state.enemy_hp, obs.state.enemy_mp);
|
||
|
//printf("Steps: %i Reward: %i Done: %d\n", obs.state.steps, obs.reward, obs.done);
|
||
|
//printf("Action: %i\n", action);
|
||
|
|
||
|
char data[obs_size];
|
||
|
memcpy(&data, &obs, obs_size);
|
||
|
send(client_sock, data, obs_size, 0);
|
||
|
} else if(buffer[0] == 0x1) { // Reset
|
||
|
struct State state;
|
||
|
Reset(&env, &state);
|
||
|
char data[sizeof(state)];
|
||
|
memcpy(&data, &state, sizeof(state));
|
||
|
send(client_sock, data, obs_size, 0);
|
||
|
}
|
||
|
}
|
||
|
shutdown(client_sock, SHUT_RDWR);
|
||
|
close(client_sock);
|
||
|
}
|
||
|
|
||
|
int main(int argc, char* argv[]) {
|
||
|
int env = StartEnv(atoi(argv[1]));
|
||
|
if(env < 0) {
|
||
|
printf("Error starting environment");
|
||
|
return 1;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|