-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommunicator.cpp
64 lines (50 loc) · 1.63 KB
/
Communicator.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
//
// Communicator.cpp
// HaxClient
//
// Created by Andrej Karadzic on 11/26/15.
// Copyright © 2015 Andrej Karadzic. All rights reserved.
//
#include "Communicator.hpp"
Communicator::Communicator(string serverHostname, short serverBroadcastPort,
short serverListenPort, string username)
: serverHostname(serverHostname), username(username),
serverBroadcastPort(serverBroadcastPort),
serverListenPort(serverListenPort) {}
void Communicator::sendlogin() {
long messageLen = username.length() + 2;
char message[messageLen];
sprintf(message, "l;%s", username.c_str());
sendRaw(message);
}
void Communicator::sendCommand(int keys) {
long messageLen = username.length() + 10;
char message[messageLen];
sprintf(message, "p;%s;%d", username.c_str(), keys);
sendRaw(message);
}
void Communicator::sendRaw(const char *message) {
long messageLen = strlen(message);
try {
PracticalSocket::UDPSocket sock;
sock.sendTo(message, (int)messageLen, serverHostname, serverListenPort);
} catch (PracticalSocket::SocketException &e) {
std::cerr << e.what() << std::endl;
exit(1);
}
}
char *Communicator::receiveRaw() {
try {
PracticalSocket::UDPSocket sock(serverBroadcastPort);
string sourceAddress;
unsigned short sourcePort;
int bytesRcvd =
sock.recvFrom(receivedMessage, BUFFSIZE, sourceAddress, sourcePort);
receivedMessage[bytesRcvd] = '\0';
// cout << "Received from " << sourceAddress << ":" << sourcePort;
} catch (PracticalSocket::SocketException &e) {
std::cerr << e.what() << std::endl;
exit(1);
}
return receivedMessage;
}