forked from snoutmate/Sea-Defender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotinterface.cpp
71 lines (63 loc) · 1.5 KB
/
botinterface.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
#include "botinterface.h"
#include "snoutlib/misc.h"
#include <sstream>
BotInterface::BotInterface() :
m_listen_socket(2101)
{
m_listen_socket.set_non_blocking(true);
}
BotInterface::~BotInterface()
{
if(m_connected)
m_client_socket.close();
}
/** @brief async_accept
*
* Run a non-blocking call to accept one incoming bot connection.
*/
bool BotInterface::async_accept()
{
if(!m_connected && m_listen_socket.accept(m_client_socket)) {
m_client_socket.set_non_blocking(true);
m_connected = true;
return true;
}
return false;
}
/** @brief async_read
*
* read a line from the bot, if available.
* disconnect bot if any error occurs, unless there is no data available.
*/
bool BotInterface::async_read(std::string &msg)
{
if(!m_connected)
return false;
int status = m_client_socket.recv(msg);
if(status > 0) {
return true;
} else if(status < 0) {
m_client_socket.close();
m_connected = false;
}
return false;
}
/** @brief async_send
*
* send a line to the bot.
* disconnect bot on any error.
*/
bool BotInterface::async_send(float time, std::string event, vec2 pos, std::string params)
{
if(!m_connected)
return false;
// format message and send
stringstream msg;
msg << time << " " << pos[0] << "," << pos[1] << " " << event << " " << params << std::endl;
if(!m_client_socket.send(msg.str())) {
m_client_socket.close();
m_connected = false;
return false;
}
return true;
}