1 |
ab11 |
1.1 |
#include "SmallNet.h" |
2 |
|
|
|
3 |
ab11 |
1.3 |
SmallNet::SmallNet(int printDebug){ |
4 |
|
|
|
5 |
|
|
debug = printDebug; |
6 |
|
|
|
7 |
|
|
if ( debug == 1 ){ |
8 |
|
|
std::cout << "SmallNet Constructor\n"; |
9 |
|
|
} |
10 |
ab11 |
1.1 |
|
11 |
|
|
return; |
12 |
|
|
|
13 |
ab11 |
1.3 |
} // SmallNet |
14 |
ab11 |
1.1 |
|
15 |
|
|
|
16 |
|
|
int SmallNet::connectHeartBeat(string host, int port){ |
17 |
|
|
|
18 |
|
|
|
19 |
ab11 |
1.3 |
if ( debug == 1 ){ |
20 |
|
|
std::cout << "SmallNet::connectHeartBeat(" << host << "," << port << ")\n"; |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
socket = new iosockinet(sockbuf::sock_stream); |
24 |
|
|
// create a pointer to the object |
25 |
|
|
|
26 |
|
|
// connect the socket |
27 |
|
|
|
28 |
|
|
// std::cout << socket; |
29 |
ab11 |
1.1 |
|
30 |
ab11 |
1.3 |
return (*socket).rdbuf()->connect(host.c_str(), port); |
31 |
|
|
|
32 |
|
|
} // connectHeartBeat |
33 |
ab11 |
1.1 |
|
34 |
|
|
string SmallNet::heartBeatSend(string text){ |
35 |
|
|
|
36 |
ab11 |
1.3 |
char buf[1024]; |
37 |
|
|
|
38 |
|
|
if ( debug == 1 ){ |
39 |
|
|
std::cout << "SmallNet::heartBeatSend()\n"; |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
// std::cout << socket; |
43 |
|
|
// (*socket) << "BUNG BUNG BUNG" << flush; |
44 |
|
|
(*socket) << text.c_str() << "\n" << flush; // |
45 |
|
|
|
46 |
|
|
if ( debug == 1 ){ |
47 |
|
|
std::cout << "SmallNet::heartBeatSend->Sent:" << text << "\n"; |
48 |
|
|
} |
49 |
ab11 |
1.1 |
|
50 |
|
|
|
51 |
ab11 |
1.3 |
(*socket).getline (buf, 1024); |
52 |
|
|
|
53 |
|
|
return buf; |
54 |
|
|
|
55 |
|
|
} // heartBeatSend |
56 |
ab11 |
1.1 |
|
57 |
|
|
|
58 |
|
|
void SmallNet::closeHeartBeatConnection(){ |
59 |
ab11 |
1.3 |
if ( debug == 1 ){ |
60 |
|
|
std::cout << "SmallNet::closeHeartBeatConnection()\n"; |
61 |
|
|
} |
62 |
ab11 |
1.1 |
|
63 |
ab11 |
1.3 |
// closes automatically |
64 |
|
|
// socket.close(); |
65 |
ab11 |
1.1 |
return; |
66 |
|
|
|
67 |
ab11 |
1.3 |
} // closeHeartBeatConnection |
68 |
ab11 |
1.1 |
|
69 |
|
|
|
70 |
ab11 |
1.3 |
int SmallNet::connectConfig(string host, int port){ |
71 |
|
|
if ( debug == 1 ){ |
72 |
|
|
std::cout << "SmallNet::connectConfig(" << host << "," << port << ")\n"; |
73 |
|
|
} |
74 |
ab11 |
1.1 |
|
75 |
ab11 |
1.3 |
return connectHeartBeat(host,port); |
76 |
ab11 |
1.1 |
|
77 |
ab11 |
1.3 |
} // makeConfigConnection |
78 |
ab11 |
1.1 |
|
79 |
|
|
string SmallNet::configSend(string text){ |
80 |
ab11 |
1.3 |
if ( debug == 1 ){ |
81 |
|
|
std::cout << "SmallNet::configSend()\n"; |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
return heartBeatSend(text); |
85 |
|
|
|
86 |
|
|
} // configSend |
87 |
|
|
|
88 |
|
|
void SmallNet::closeConfigConnection(){ |
89 |
|
|
if ( debug == 1 ){ |
90 |
|
|
std::cout << "SmallNet::closeConfigConnection()\n"; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
closeHeartBeatConnection(); |
94 |
|
|
|
95 |
|
|
return; |
96 |
|
|
|
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
void SmallNet::sendUPDPacket( string host, int port, string message ){ |
100 |
|
|
|
101 |
|
|
if ( debug == 1 ){ |
102 |
|
|
std::cout << "SmallNet::sendUPDPacket:" << host << ":" << port << "\n"; |
103 |
|
|
std::cout << message << "\n"; |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
osockinet udpSocket(sockbuf::sock_dgram); |
107 |
|
|
int udpSize; |
108 |
|
|
udpSize = 8192; |
109 |
|
|
udpSocket->setopt(sockbuf::so_sndbuf , &udpSize, sizeof(udpSize), sockbuf::sol_socket); |
110 |
|
|
udpSocket->connect (host.c_str(), port); |
111 |
|
|
udpSocket << message.c_str() << endl; |
112 |
|
|
|
113 |
|
|
return; |
114 |
|
|
} |