forked from ivansafrin/Polycode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolySocket.cpp
More file actions
executable file
·153 lines (119 loc) · 4.28 KB
/
PolySocket.cpp
File metadata and controls
executable file
·153 lines (119 loc) · 4.28 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
146
147
148
149
150
151
152
153
/*
Copyright (C) 2011 by Ivan Safrin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "PolySocket.h"
#include "PolyLogger.h"
#ifndef _WINDOWS
#include <unistd.h>
#endif
using namespace Polycode;
using std::vector;
Address::Address(String ipAsString, unsigned int port) {
setAddress(ipAsString, port);
}
Address::Address(unsigned int ip, unsigned int port) {
setAddress(ip, port);
}
Address::Address() {
}
void Address::setAddress(unsigned int ip, unsigned int port) {
unsigned short destination_port = port;
sockAddress.sin_family = AF_INET;
sockAddress.sin_addr.s_addr = htonl(ip);
sockAddress.sin_port = htons( destination_port );
uintAddress = ip;
this->port = port;
}
void Address::setAddress(String ipAsString, unsigned int port) {
unsigned int a,b,c,d;
vector<String> values = ipAsString.split(".");
if(values.size() == 4) {
a = atoi(values[0].c_str());
b = atoi(values[1].c_str());
c = atoi(values[2].c_str());
d = atoi(values[3].c_str());
} else {
Logger::log("Invalid IP address!\n");
}
unsigned int destination_address = ( a << 24 ) | ( b << 16 ) | ( c << 8 ) | d;
unsigned short destination_port = port;
sockAddress.sin_family = AF_INET;
sockAddress.sin_addr.s_addr = htonl( destination_address );
sockAddress.sin_port = htons( destination_port );
uintAddress = destination_address;
this->port = port;
}
Address::~Address() {
}
Socket::Socket(int port) : EventDispatcher() {
sockId = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
if (sockId < 0) {
socketError("Error creating socket");
}
sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( (unsigned short) port );
if( bind(sockId, (const sockaddr*) &address, sizeof(sockaddr_in) ) < 0 ) {
socketError( "failed to bind socket");
}
#if PLATFORM == PLATFORM_MAC || PLATFORM == PLATFORM_UNIX
int nonBlocking = 1;
if ( fcntl( sockId, F_SETFL, O_NONBLOCK, nonBlocking ) == -1 ) {
socketError( "failed to set non-blocking socket");
}
#elif PLATFORM == PLATFORM_WINDOWS
DWORD nonBlocking = 1;
if ( ioctlsocket( sockId, FIONBIO, &nonBlocking ) != 0 ) {
socketError( "failed to set non-blocking socket");
}
#endif
}
bool Socket::sendData(const Address &address, char *data, unsigned int packetSize) {
int sent_bytes = sendto(sockId, (const char*)data, packetSize, 0, (sockaddr*)&address.sockAddress, sizeof(sockaddr_in));
if ( sent_bytes != packetSize ) {
socketError("failed to send packet");
return false;
}
return true;
}
int Socket::receiveData() {
SocketEvent *event = new SocketEvent();
sockaddr_in from;
socklen_t fromLength = sizeof( from );
int received_bytes = recvfrom( sockId, (char*)event->data, MAX_PACKET_SIZE,
0, (sockaddr*)&from, &fromLength );
if ( received_bytes <= 0 ) {
delete event;
return received_bytes;
}
event->dataSize = received_bytes;
event->fromAddress = Address(ntohl( from.sin_addr.s_addr ), ntohs( from.sin_port ));
dispatchEvent(event, SocketEvent::EVENT_DATA_RECEIVED);
return received_bytes;
}
Socket::~Socket() {
#if PLATFORM == PLATFORM_MAC || PLATFORM == PLATFORM_UNIX
close( sockId );
#elif PLATFORM == PLATFORM_WINDOWS
closesocket( sockId );
#endif
}
void Socket::socketError(String error) {
Logger::log("%s\n",error.c_str());
}