forked from grfz/Socket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDP.cpp
190 lines (154 loc) · 5.55 KB
/
UDP.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* UDP.cpp
* This file is part of VallauriSoft
*
* Copyright (C) 2012 - Comina Francesco
*
* VallauriSoft is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* VallauriSoft is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VallauriSoft; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#ifndef _UDP_CPP_
#define _UDP_CPP_
#include "Socket.hpp"
namespace Socket
{
UDP::UDP(void) : CommonSocket(SOCK_DGRAM)
{
}
UDP::UDP(const UDP &udp) : CommonSocket()
{
this->_socket_id = udp._socket_id;
this->_opened = udp._opened;
this->_binded = udp._binded;
}
template <class T>
int UDP::send(Ip ip, Port port, const T *data, size_t len)
{
if (!this->_opened) this->open();
len *= sizeof(T);
if (len > (SOCKET_MAX_BUFFER_LEN * sizeof(T)))
{
stringstream error;
error << "[send] with [ip=" << ip << "] [port=" << port << "] [data=" << data << "] [len=" << len << "] Data length higher then max buffer len";
throw SocketException(error.str());
}
Address address(ip, port);
int ret;
if ((ret = sendto(this->_socket_id, (const char*)data, len, 0, (struct sockaddr*)&address, sizeof(struct sockaddr))) == -1)
{
stringstream error;
error << "[send] with [ip=" << ip << "] [port=" << port << "] [data=" << data << "] [len=" << len << "] Cannot send";
throw SocketException(error.str());
}
return ret;
}
template <class T>
int UDP::send(Address address, const T *data, size_t len)
{
return this->send<T>(address.ip(), address.port(), data, len);
}
template <class T>
int UDP::send(Ip ip, Port port, T data)
{
return this->send<T>(ip, port, &data, 1);
}
template <class T>
int UDP::send(Address address, T data)
{
return this->send<T>(address.ip(), address.port(), &data, 1);
}
template <>
int UDP::send<string>(Ip ip, Port port, string data)
{
return this->send<char>(ip, port, data.c_str(), data.length() + 1);
}
template <>
int UDP::send<string>(Address address, string data)
{
return this->send<char>(address.ip(), address.port(), data.c_str(), data.length() + 1);
}
template <class T>
int UDP::send(Ip ip, Port port, vector<T> data)
{
return this->send<T>(ip, port, data.data(), data.size());
}
template <class T>
int UDP::send(Address address, vector<T> data)
{
return this->send<T>(address.ip(), address.port(), data.data(), data.size());
}
template <class T>
int UDP::receive(Address *address, T *data, size_t len, unsigned int *received_elements)
{
if (!this->_opened) this->open();
if (!this->_binded) throw SocketException("[receive] Make the socket listening before receiving");
len *= sizeof(T);
if (len > (SOCKET_MAX_BUFFER_LEN * sizeof(T)))
{
stringstream error;
error << "[send] with [buffer=" << data << "] [len=" << len << "] Data length higher then max buffer length";
throw SocketException(error.str());
}
int received_bytes;
socklen_t size = sizeof(struct sockaddr);
if ((received_bytes = recvfrom(this->_socket_id, (char*)data, len, 0, (struct sockaddr*)address, (socklen_t*)&size)) == -1)
{
throw SocketException("[receive] Cannot receive");
}
*received_elements = (unsigned int)(received_bytes / sizeof(T));
return received_bytes;
}
template <class T>
Datagram<T*> UDP::receive(T *buffer, size_t len = SOCKET_MAX_BUFFER_LEN)
{
Datagram<T*> ret;
ret.received_bytes = this->receive<T>(&ret.address, buffer, len, &ret.received_elements);
ret.data = buffer;
return ret;
}
template <class T, size_t N>
Datagram<T[N]> UDP::receive(size_t len = N)
{
Datagram<T[N]> ret;
ret.received_bytes = this->receive<T>(&ret.address, ret.data, len, &ret.received_elements);
return ret;
}
template <class T>
Datagram<T> UDP::receive(void)
{
Datagram<T> ret;
ret.received_bytes = this->receive<T>(&ret.address, &ret.data, 1, &ret.received_elements);
return ret;
}
template <>
Datagram<string> UDP::receive<string>(void)
{
Datagram<string> ret;
char buffer[SOCKET_MAX_BUFFER_LEN];
ret.received_bytes = this->receive<char>(&ret.address, buffer, SOCKET_MAX_BUFFER_LEN, &ret.received_elements);
ret.data = buffer;
return ret;
}
template <class T>
Datagram<vector<T> > UDP::receive(size_t len)
{
Datagram<vector<T> > ret;
T buffer[len];
ret.received_bytes = this->receive<T>(&ret.address, buffer, len, &ret.received_elements);
for (int i = 0; i < ret.received_elements; i++) ret.data.push_back(buffer[i]);
return ret;
}
}
#endif