-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTeamConnection.cpp
161 lines (139 loc) · 4.11 KB
/
TeamConnection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "TeamConnection.h"
#include <iostream>
#include <process.h>
#include "GameState.h"
#include <fstream>
#include "Gametime.h"
#include "HockeyGame.h"
#include "Limits.h"
UDPSocket *listeningSocket = NULL;
Team::Team(Connection source) {
connection = source;
socket = new UDPSocket();
lastAlive = 0;
}
Team::~Team() {
if (socket != NULL) {
delete socket;
socket = NULL;
}
}
void Team::send(int* toSend,const int bufLength){//skickar speltillstånd till spelare
char msg[4*39];//antal chars som beskriver speltillståndet
for (int i=0; i<bufLength; i++) {//dekonstruerar ett intfält till ett charfält
int x = toSend[i];
int j = i << 2;
msg[j++] = (byte) ((x >> 0) & 0xff); // läser en byte i taget i varje int
msg[j++] = (byte) ((x >> 8) & 0xff);
msg[j++] = (byte) ((x >> 16) & 0xff);
msg[j++] = (byte) ((x >> 24) & 0xff);
}
/*for(int k=4*9;k<4*10;k++){
int a=msg[k];
cout<<a<<"\t";
}
cout<<endl;*/
socket->sendTo(msg,4*bufLength,connection.adress,connection.port);//Skickar meddelandet till AI-modul
}
void Team::sendBytes(char *msg, const int bufLength) {
socket->sendTo(msg,bufLength,connection.adress,connection.port);//Skickar meddelandet till AI-modul
}
bool Team::fromSource(Connection source){
return connection==source;
}
void Team::reportAlive() {
lastAlive = getGametime();
}
bool Team::isAlive() {
int currentTime = getGametime();
return (currentTime - lastAlive) < 10000;
}
bool Connection::operator==(Connection b){//likamed operator för anslitning
return b.adress==adress&&b.port==port;
}
Team* homeTeam=0;
Team* awayTeam=0;
Team *teamFromConnection(Connection *pConnection) {
if (homeTeam->fromSource(*pConnection))
return homeTeam;
else if(awayTeam->fromSource(*pConnection))
return awayTeam;
return NULL;
}
unsigned __stdcall recieverThread(void* sock){
//###### Player comands are saved here
char homeCmd[30];
char awayCmd[30];
//######
if(homeTeam==NULL||awayTeam==NULL){
cerr<<"teams not defined"<<endl;
exit(1);
}
Connection source=Connection();
UDPSocket* socket=(UDPSocket*) sock;
char buf[BUFLENGTH];//läst meddelande
char msg[BUFLENGTH/4+1];//formaterat medelande
ofstream myfile=ofstream(); //DEBUGVERKTYG
myfile.open ("commands.txt"); //öpnnar fil där kommandon sparas
homeSerial->write(NULL,0);//slutar kalibrara mikrokontrollerna när spelet börjar
awaySerial->write(NULL,0);
for(;;){// recieves commands and passes them on to correct microprocessor
int rcvBytes=socket->recvFrom(buf,BUFLENGTH,source.adress,source.port); //tar emot meddelande
Team *team = teamFromConnection(&source);
if (team != NULL) {
if (rcvBytes == 1) {
if (buf[0] == 'N')
team->reportAlive();
}
else if ((rcvBytes % (5 * 4)) == 0) {
int index = 0;
int cmdIndex = 0;
for (int cmdIndex = 0; cmdIndex < rcvBytes / 5 / 4; cmdIndex++) {
myfile << getGametime() << "\t"; //sparar speltiden när kommandot mottogs
char cmd[5];
for (int i = 0; i < 5; i++) { //läser intfält som ett charfält av kommandon
cmd[i] = buf[cmdIndex * 5 * 4 + i * 4];
myfile << (int)(i == 3 ? (signed char)msg[i] : (unsigned char)msg[i]) << "\t";
}
// one command should now be in msg
if (isCommandOkay(team == homeTeam ? 0 : 1, cmd)) {
memcpy(msg + index, cmd, 5);
index += 5;
myfile << "+";
}
else myfile << "-";
myfile<<endl;
}
if (team == homeTeam)//skickar vidare kommandot beroende på vartifrån meddelandet kom ifrån
homeSerial->write(msg, index);//skriver till mikrokontroller
else if(team == awayTeam)
awaySerial->write(msg, index);
}
}
}
}
bool checkClient(Team *pTeam) {
char c = 'D';
int i = 0;
cout << "Checking team alive..." << endl;
while (!pTeam->isAlive() && i < 5) {
pTeam->sendBytes(&c, 1);
i++;
Sleep(1000);
}
if (!pTeam->isAlive()) {
cout << "Team not alive!" << endl;
return false;
}
cout << "Team alive :-)" << endl;
return true;
}
unsigned __stdcall checkClientsProc(void *param) {
while (true) {
if (!checkClient(homeTeam))
((HockeyGame *)param)->stopGame();
if (!checkClient(awayTeam))
((HockeyGame *)param)->stopGame();
Sleep(10000);
}
}