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