Commit 7acc0bc1 authored by Robert Scott James's avatar Robert Scott James

Added simplistic class Bot that deals with the connection and event loop.

main() is now extremely clean and easy to read.
Need to invest some time into how to do a proper disconnect method so that we don't need to wait for timeout.

 Changes to be committed:
	modified:   Makefile
	new file:   bot.cfg
	new file:   bot.cpp
	new file:   bot.h
	modified:   create.cpp
	modified:   debug.h
	modified:   handledata.cpp
	deleted:    instactf
	modified:   main.cpp
parent 758461ab
CC=g++
CFLAGS=-O2 -g0 -std=gnu++11 -pedantic -Wall -Wextra
LDFLAGS=-lm -lenet
LDFLAGS=-lm -lenet -lconfig++
INCLUDES=-I/usr/local/include -I.
BinaryName=sauerbot.app
OBJS=main.o handledata.o create.o
OBJS=main.o handledata.o create.o bot.o
all: $(OBJS) $(BinaryName)
......@@ -17,4 +17,4 @@ $(BinaryName):
$(CC) -o $(BinaryName) $(CFLAGS) $(LDFLAGS) $(INCLUDES) $(OBJS)
clean:
rm -rf *.o $(BinaryName)
\ No newline at end of file
rm -rf *.o $(BinaryName)
# Bot Configuration File
name = "Lil Bot";
server = "games.sucs.org"
port = 28785;
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <inttypes.h>
#include <signal.h>
#include <enet/enet.h>
#include <libconfig.h++>
#include "bot.h"
#include "handler.h"
using namespace libconfig;
Bot::Bot(char *filename){
Config cfg;
try{
cfg.readFile(filename);
} catch(const FileIOException &fioex){
std::cerr << "I/O error while reading file." << std::endl;
} catch(const ParseException &pex){
std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine() << " - " << pex.getError() << std::endl;
}
// Read Config
std::string config_name;
std::string config_addr;
unsigned int config_port;
config_name = cfg.lookup("name").c_str();
config_addr= cfg.lookup("server").c_str();
config_port = cfg.lookup("port");
name = config_name;
server = config_addr;
port = config_port;
}
Bot::~Bot(){
// Clean up anything
}
int Bot::host_create(){
enet_address_set_host(&address,server.c_str());
address.port = port;
std::cout << "Trying to connect to: " << server.c_str() << ":" << port << std::endl;
clienthost = enet_host_create(NULL,2,3,0,0);
if(clienthost){
connpeer = enet_host_connect(clienthost, &address, 2, 0);
enet_host_flush(clienthost);
} else {
std::cerr << "Could not connect to server" << std::endl;
return EXIT_FAILURE;
}
if(!clienthost){
std::cerr << "Bad clienthost?" << std::endl;
return EXIT_FAILURE;
}
return(0);
}
void Bot::event_loop(){
while(clienthost){
while((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;
char *datarecv;
size_t packetlength = event.packet->dataLength;
datarecv = (char *)malloc(packetlength);
std::memcpy(datarecv, event.packet->data,packetlength);
enet_packet_destroy (event.packet);
//Handler Function
datahandler(datarecv,packetlength,connpeer);
enet_host_flush(clienthost);
free(datarecv);
} else {
//std::cerr << "Data over another channel: " << event.channelID << std::endl;
}
break;
case ENET_EVENT_TYPE_DISCONNECT:
std::cout << "Disconnect event" << std::endl;
event.peer->data = NULL;
enet_peer_reset (connpeer);
clienthost = NULL;
break;
default: {
std::cout << "Another type of Event" << std::endl;
break;
}
}
}
}
}
void Bot::host_clearup(){
enet_host_destroy(clienthost);
}
#ifndef BOT_H
#define BOT_H
#include <string>
#include <enet/enet.h>
class Bot {
public:
Bot(char *);
~Bot();
int host_create();
void event_loop();
void host_clearup();
private:
std::string name;
std::string server;
unsigned int port;
ENetHost *clienthost = NULL;
ENetPeer *connpeer = NULL;
ENetAddress address;
ENetEvent event;
};
#endif
......@@ -10,23 +10,24 @@
#include <signal.h>
#include <enet/enet.h>
#include <cstdarg>
#include "create.h"
#include "handler.h"
#include "sauer.h"
#include "create.h"
#include "bot.h"
std::string putint(int n){
if(n < 128 && n > -127){
std::string output(1, n);
return(output);
} else if (n < 0x8000 && n >= -0x8000) {
char *t = "..."; //This is ugly as fuck, check it rob
char t[3];
t[0] = 0x80;
t[1] = n & 0xFF;
t[2] = n >> 8;
std::string output(t,3);
return(output);
} else {
char *t = ".....";
char t[5];
t[0] = 0x80;
t[1] = n & 0xFF;
t[2] = (n >> 8) & 0xFF;
......@@ -86,4 +87,4 @@ void sendpacket(ENetPeer *connpeer,ENetPacket *packet){
enet_peer_send (connpeer, 1, packet);
}
\ No newline at end of file
}
char *N_REVERSE_DEBUG[] = {
#ifndef DEBUG_H
#define DEBUG_H
const char *N_REVERSE_DEBUG[112] = {
"N_CONNECT", "N_SERVINFO", "N_WELCOME", "N_INITCLIENT", "N_POS", "N_TEXT", "N_SOUND", "N_CDIS",
"N_SHOOT", "N_EXPLODE", "N_SUICIDE",
"N_DIED", "N_DAMAGE", "N_HITPUSH", "N_SHOTFX", "N_EXPLODEFX",
......@@ -24,4 +27,6 @@ char *N_REVERSE_DEBUG[] = {
"N_INITTOKENS", "N_TAKETOKEN", "N_EXPIRETOKENS", "N_DROPTOKENS", "N_DEPOSITTOKENS", "N_STEALTOKENS",
"N_SERVCMD",
"N_DEMOPACKET"
};
\ No newline at end of file
};
#endif
......@@ -7,13 +7,11 @@
#include <inttypes.h>
#include <signal.h>
#include <enet/enet.h>
#include "handler.h"
#include "debug.h"
#include "create.h"
#include "handler.h"
#include "sauer.h"
#include "debug.h"
void datahandler(char *data, long datalength, ENetPeer *connpeer){
int off = 0;
......@@ -136,7 +134,7 @@ void datahandler(char *data, long datalength, ENetPeer *connpeer){
{break;}
default:{
std::cout << " Unhandled Packet:" << N_REVERSE_DEBUG[data[0]] << " len:" << std::dec << datalength << std::endl;
std::cout << " Unhandled Packet:" << N_REVERSE_DEBUG[(unsigned int)data[0]] << " len:" << std::dec << datalength << std::endl;
/*for(int i = 0; i < datalength; i++){
std::cout << (int)(unsigned char)data[i] << " ";
}
......@@ -178,4 +176,4 @@ char* getstring(char *data,int& off){
out[++i] = '\x00';
off+=i;
return out;
}
\ No newline at end of file
}
connected to server
Server Info
Connected
clientNumber:1 protocol:259
sessionId:-22 passworded 0
. AuthInfo:
06c6927426f746c657201000
3b11
garden joined the game with client number 22 on team gardenV` with model 4
Unhandled Packet:N_SERVMSG len:41
35 87 101 108 99 111 109 101 32 116 111 32 116 104 101 32 83 85 67 83 32 83 97 117 101 114 98 114 97 116 101 110 32 83 101 114 118 101 114 33 0
Unhandled Packet:N_SPECTATOR len:3
59 1 1
......@@ -8,8 +8,11 @@
#include <inttypes.h>
#include <signal.h>
#include <enet/enet.h>
#include <libconfig.h++>
#include "create.h"
#include "handler.h"
#include "sauer.h"
#include "bot.h"
void deinitialise(int s){
int signal = s;
......@@ -17,10 +20,10 @@ void deinitialise(int s){
exit(1);
}
int main(int argc, char *argv[]) {
int main(int argc, char **argv) {
//Enet Initilize
if (enet_initialize () != 0) {
fprintf (stderr, "An error occurred while initializing ENet.\n");
std::cerr << "An error occurred while initializing ENet." << std::endl;
return EXIT_FAILURE;
}
atexit (enet_deinitialize);
......@@ -35,68 +38,19 @@ int main(int argc, char *argv[]) {
sigaction(SIGINT, &sigIntHandler, NULL);
//Connection to client, start with the client's host, (how cube does it)
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);
} else {
std::cerr << "Could not connect to server" << std::endl;
return EXIT_FAILURE;
// Create Bot Class that does clever things
if(argc != 2){
std::cerr << "Usage: " << argv[0] << " config.cfg" << std::endl;
exit(0);
}
if (!clienthost){
std::cerr << "Bad clienthost?" << std::endl;
}
while (clienthost){
ENetEvent event;
while( (enet_host_service(clienthost, &event, 0)) > 0 ){
switch(event.type) {
case ENET_EVENT_TYPE_CONNECT:
std::cout << "connected to server" << std::endl;
break;
Bot sauerkraut(argv[1]);
case ENET_EVENT_TYPE_RECEIVE:
if (event.channelID == 1){
//std::cout << event.packet->data << std::endl;
char *datarecv;
size_t packetlength = event.packet->dataLength;
datarecv = (char *)malloc(packetlength);
std::memcpy(datarecv, event.packet->data,packetlength);
enet_packet_destroy (event.packet);
//Handler Function
datahandler(datarecv,packetlength,connpeer);
enet_host_flush(clienthost);
free(datarecv);
} else {
//std::cerr << "Data over another channel: " << event.channelID << std::endl;
}
break;
case ENET_EVENT_TYPE_DISCONNECT:
std::cout << "Disconnect event" << std::endl;
event.peer->data = NULL;
enet_peer_reset (connpeer);
clienthost = NULL;
break;
default: {
std::cout << "Another type of Event" << std::endl;
break;
}
}
}
}
//Connection to client, start with the client's host, (how cube does it)
sauerkraut.host_create();
sauerkraut.event_loop();
enet_host_destroy(clienthost);
std::cout << "Shutting down\n" << std::endl;
}
\ No newline at end of file
sauerkraut.host_clearup();
}
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