-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDPSocket.cpp
More file actions
145 lines (116 loc) · 3.55 KB
/
Copy pathUDPSocket.cpp
File metadata and controls
145 lines (116 loc) · 3.55 KB
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
//
// Created by Matt on 10/09/2016.
//
#include <iostream>
#include "UDPSocket.h"
bool UDPSocket::initialiseSockets() {
#if PLATFORM == PLATFORM_WINDOWS
WSADATA WSAData;
return WSAStartup(MAKEWORD(2, 2), &WSAData) == NO_ERROR;
#else
return true;
#endif
}
void UDPSocket::shutDownSockets() {
#if PLATFORM == PLATFORM_WINDOWS
WSACleanup();
#endif
}
UDPSocket::UDPSocket() {}
UDPSocket::~UDPSocket() {
UDPSocket::close();
}
bool UDPSocket::open(unsigned short port) {
bool error = false;
//open Socket
handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (handle <= 0) {
std::cerr << "Failed to create socket" << std::endl;
error = true;
}
//Set address + port
sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons((unsigned short) port);
//Bind socket
if (bind(handle, (const sockaddr*) &address, sizeof(sockaddr_in)) < 0) {
std::cerr << "Failed to bind socket" << std::endl;
error = true;
}
//Set socket to non-blocking
#if PLATFORM == PLATFORM_WINDOWS
DWORD nonBlocking = 1;
if (ioctlsocket(handle, FIONBIO, &nonBlocking) != 0) {
std::cerr << "Failed to set socket to non-blocking mode" << std::endl;
error = true;
}
#else //Mac or UNIX
int nonBlocking = 1;
if (fcntl(handle, F_SETFL, O_NONBLOCK, nonBlocking) == -1) {
std::cerr << "Failed to set socket to non-blocking mode" << std::endl;
error = true;
}
#endif
opened = !error;
}
void UDPSocket::close() {
#if PLATFORM == PLATFORM_MAC || PLATFORM == PLATFORM_UNIX
close( socket );
#elif PLATFORM == PLATFORM_WINDOWS
closesocket(handle);
#endif
}
bool UDPSocket::isOpen() const {
return opened;
}
bool UDPSocket::send(UDPAddress destination, const void *data, int size) {
int sentBytes = sendto(handle, (const char *) data, size, 0, (sockaddr *) destination.getIntegerIP(),
sizeof(sockaddr_in));
return sentBytes == size;
}
int UDPSocket::receive() {
//Clear and reallocate buffer
if (recvBuffer != nullptr) {
free(recvBuffer);
}
recvBuffer = (char*)calloc(sizeof(char), 50);
int received = 0;
while ( true )
{
#if PLATFORM == PLATFORM_WINDOWS
typedef int socklen_t;
#endif
sockaddr_in sender;
socklen_t fromLength = sizeof(sender); //lastSender
int bytes = recvfrom(handle,
recvBuffer,
sizeof(char)*50,
0,
(sockaddr*)&sender,
&fromLength);
if ( bytes <= 0 ) {
break;
} else {
received = 1;
}
receivedDataSize = bytes;
unsigned int from_address = ntohl(sender.sin_addr.s_addr);
unsigned short from_port = ntohs(sender.sin_port);
std::cout << "Size: " << bytes << std::endl;
std::cout << "Data: " << recvBuffer << std::endl;
std::cout << "From address: " << from_address << ":" << from_port << std::endl;
std::cout << "Converting to human form" << std::endl;
lastSender = UDPAddress(from_address, from_port);
}
return received;
}
char *UDPSocket::getReceivedData() {
return recvBuffer;
}
UDPAddress UDPSocket::getLastSender() {
return lastSender;
}
int UDPSocket::getReceivedDataSize() const {
return receivedDataSize;
}