forked from critterandguitari/Organelle_OS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUdpSocket.cpp
More file actions
executable file
·118 lines (96 loc) · 3.74 KB
/
Copy pathUdpSocket.cpp
File metadata and controls
executable file
·118 lines (96 loc) · 3.74 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
/***************************************************/
/*! \class UdpSocket
\brief STK UDP socket server/client class.
This class provides a uniform cross-platform UDP socket
server/client interface. Methods are provided for reading or
writing data buffers. The constructor creates a UDP socket and
binds it to the specified port. Note that only one socket can be
bound to a given port on the same machine.
UDP sockets provide unreliable, connection-less service. Messages
can be lost, duplicated, or received out of order. That said,
data transmission tends to be faster than with TCP connections and
datagrams are not potentially combined by the underlying system.
The user is responsible for checking the values returned by the
read/write methods. Values less than or equal to zero indicate
the occurence of an error.
by Perry R. Cook and Gary P. Scavone, 1995--2014.
*/
/***************************************************/
#include "UdpSocket.h"
#include <cstring>
#include <sstream>
#ifdef __APPLE__
const int SOCK_CLOEXEC=0;
#endif
//namespace stk {
UdpSocket :: UdpSocket(int port )
{
validAddress_ = false;
#if defined(__OS_WINDOWS__) // windoze-only stuff
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(1,1);
WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested) {
std::cout << "UdpSocket: Incompatible Windows socket library version!";
// handleError( StkError::PROCESS_SOCKET );
}
#endif
// Create the UDP socket
// sock_cloexec so any child processes don't retain the address
soket_ = ::socket( AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP );
if ( soket_ < 0 ) {
std::cout << "UdpSocket: Couldn't create UDP socket!";
//handleError( StkError::PROCESS_SOCKET );
}
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( port );
int flags = fcntl(soket_, F_GETFL);
flags |= O_NONBLOCK;
fcntl(soket_, F_SETFL, flags);
// Bind socket to the appropriate port and interface (INADDR_ANY)
if ( bind(soket_, (struct sockaddr *)&address, sizeof(address)) < 0 ) {
std::cout << "UdpSocket: Couldn't bind socket in constructor!";
// handleError( StkError::PROCESS_SOCKET );
}
port_ = port;
}
UdpSocket :: ~UdpSocket()
{
}
void UdpSocket :: setDestination( int port, std::string hostname )
{
this->setAddress( &address_, port, hostname );
validAddress_ = true;
}
void UdpSocket :: setAddress( struct sockaddr_in *address, int port, std::string hostname )
{
struct hostent *hostp;
if ( (hostp = gethostbyname( hostname.c_str() )) == 0 ) {
std::cout << "UdpSocket::setAddress: unknown host (" << hostname << ")!";
//handleError( StkError::PROCESS_SOCKET_IPADDR );
}
// Fill in the address structure
address->sin_family = AF_INET;
memcpy((void *)&address->sin_addr, hostp->h_addr, hostp->h_length);
address->sin_port = htons( port );
}
int UdpSocket :: writeBuffer( const void *buffer, long bufferSize, int flags )
{
if ( !isValid( soket_ ) || !validAddress_ ) return -1;
return sendto( soket_, (const char *)buffer, bufferSize, flags, (struct sockaddr *)&address_, sizeof(address_) );
}
int UdpSocket :: readBuffer( void *buffer, long bufferSize, int flags )
{
if ( !isValid( soket_ ) ) return -1;
return recvfrom( soket_, (char *)buffer, bufferSize, flags, NULL, NULL );
}
int UdpSocket :: writeBufferTo( const void *buffer, long bufferSize, int port, std::string hostname, int flags )
{
if ( !isValid( soket_ ) ) return -1;
struct sockaddr_in address;
this->setAddress( &address, port, hostname );
return sendto( soket_, (const char *)buffer, bufferSize, flags, (struct sockaddr *)&address, sizeof(address) );
}
//} // stk namespace