Commit 4b47dbdb authored by Robert Scott James's avatar Robert Scott James

Initial commit

 Changes to be committed:
	new file:   .gitignore
	new file:   Makefile
	new file:   README.md
	new file:   main.cpp

Simple connection to sever for now. Later add actual useful output
parents
*.o
*.app
CC=g++
CFLAGS=-O2 -g0 -std=gnu++11 -pedantic -Wall -Wextra
LDFLAGS=-lm -lenet
INCLUDES=-I/usr/local/include -I.
BinaryName=sauerbot.app
OBJS=main.o
all: $(OBJS) $(BinaryName)
%.o: %.cpp
$(CC) $(CFLAGS) $(INCLUDES) -c $<
$(BinaryName):
$(CC) -o $(BinaryName) $(CFLAGS) $(LDFLAGS) $(INCLUDES) $(OBJS)
clean:
rm -rf *.o $(BinaryName)
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <string>
#include <inttypes.h>
#include <signal.h>
#include <enet/enet.h>
void deinitialise(int s){
int signal = s;
std::cout << "Caught signal: " << signal << std::endl;
exit(1);
}
int main() {
fprintf( stderr, "Shutting up\n");
//Enet Initilize
if (enet_initialize () != 0) {
fprintf (stderr, "An error occurred while initializing ENet.\n");
return EXIT_FAILURE;
}
atexit (enet_deinitialize);
// Catch sigint
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = deinitialise;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
//Connection to client, start with the client's host, (how cube does it)
std::cerr << "Connecting" << std::endl;
ENetHost *clienthost = NULL;
ENetPeer *connpeer = NULL;
ENetAddress address;
enet_address_set_host(&address,"games");
address.port = 28785;
if(!clienthost) {
clienthost = enet_host_create(NULL, 2, 3, 0, 0);
}
if(clienthost){
connpeer = enet_host_connect(clienthost, &address, 2, 0);
enet_host_flush(clienthost);
std::cerr << "Connecion good?" << std::endl;
} else {
std::cerr << "Could not connect to server" << std::endl;
return EXIT_FAILURE;
}
//Read
int r;
if (!clienthost){
std::cerr << "Bad clienthost?" << std::endl;
}
while (clienthost){
ENetEvent event;
while(( r=enet_host_service(clienthost, &event, 0))>0){
switch(event.type) {
case ENET_EVENT_TYPE_CONNECT:
std::cout << "connected to server" << std::endl;
break;
case ENET_EVENT_TYPE_RECEIVE:
if (event.channelID == 1){
std::cout << event.packet->data << std::endl;
enet_packet_destroy (event.packet);
} else {
std::cerr << "Data over bad channel" << std::endl;
}
break;
case ENET_EVENT_TYPE_DISCONNECT:
std::cout << "Disconnect event" << std::endl;
event.peer->data = NULL;
clienthost = NULL;
break;
default: {
break;
}
}
}
}
enet_host_destroy(clienthost);
std::cout << "Shutting down\n" << std::endl;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment