sources/network/udpsocket.cpp

Thu, 11 Dec 2014 06:13:32 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Thu, 11 Dec 2014 06:13:32 +0200
changeset 7
01e4e9ae323a
parent 6
c709bb1a08e4
child 8
8b697d30c49f
permissions
-rw-r--r--

- fixed some problems with the ip address

5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 #include <sys/socket.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 #include <sys/types.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 #include <sys/time.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 #include <netinet/in.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 #include <string.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 #include <fcntl.h>
6
c709bb1a08e4 - udp -> udpsocket
Teemu Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
7 #include "udpsocket.h"
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 UDPSocket::UDPSocket() :
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 m_socket (socket (AF_INET, SOCK_DGRAM, 0)) {}
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 UDPSocket::~UDPSocket() {}
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 UDPSocket::set_blocking (bool a) -> bool
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 int flags = fcntl (m_socket, F_GETFL, 0);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 int newflags = (a ? (flags | O_NONBLOCK) : (flags & ~O_NONBLOCK));
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 if (flags < 0 || fcntl (m_socket, F_SETFL, newflags) != 0)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 m_error = "Unable to set socket as non-blocking";
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 return false;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 return true;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 UDPSocket::bind (unsigned short port) -> bool
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 struct sockaddr_in svaddr;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 memset (&svaddr, 0, sizeof svaddr);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 svaddr.sin_family = AF_INET;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 svaddr.sin_port = htons (port);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 svaddr.sin_addr.s_addr = htonl (INADDR_ANY);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 if (::bind (m_socket, reinterpret_cast<struct sockaddr*> (&svaddr), sizeof svaddr) == -1)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 m_error = String ("Couldn't bind to port ") + String::from_number (port);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 return false;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 return true;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 UDPSocket::read (Datagram& datagram) -> bool
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 sockaddr_in claddr;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 socklen_t socklen = sizeof claddr;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 static unsigned char packet[MAX_DATAGRAM_LENGTH];
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 int length = ::recvfrom (m_socket, packet, sizeof packet, 0,
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 reinterpret_cast<struct sockaddr*> (&claddr), &socklen);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 if (length == -1)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 // We got an error, though EWOULDBLOCK is silent as it means no packets recieved.
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 if (errno != EWOULDBLOCK)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 m_error.sprintf ("recvfrom error: %s", strerror (errno));;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 return false;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 datagram.from.host = ntohl (claddr.sin_addr.s_addr);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 datagram.from.port = ntohs (claddr.sin_port);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 datagram.data = Bytestream (packet, length);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 return true;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 // -------------------------------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 //
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 METHOD
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 UDPSocket::send (const Bytestream& data, const IPAddress& addr) -> bool
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 struct sockaddr_in claddr = addr.to_sockaddr_in();
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 int res = ::sendto (m_socket, data.data(), data.written_length(), 0,
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 reinterpret_cast<struct sockaddr*> (&claddr), sizeof claddr);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 if (res == -1)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 m_error = String ("Unable to launch packet: ") + strerror (errno);
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 return false;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 return true;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 }

mercurial