sources/network/udpsocket.cpp

Wed, 27 Jan 2021 19:27:23 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 27 Jan 2021 19:27:23 +0200
changeset 189
248d0b85cbda
parent 186
9330b93d9946
child 190
90bf9049e5eb
permissions
-rw-r--r--

various changes, better network error handling

9
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
1 /*
109
e4966d7e615d Happy new year 2016
Teemu Piippo <crimsondusk64@gmail.com>
parents: 88
diff changeset
2 Copyright 2014 - 2016 Teemu Piippo
9
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
3 All rights reserved.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
4
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
5 Redistribution and use in source and binary forms, with or without
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
6 modification, are permitted provided that the following conditions
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
7 are met:
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
8
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
9 1. Redistributions of source code must retain the above copyright
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
10 notice, this list of conditions and the following disclaimer.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
12 notice, this list of conditions and the following disclaimer in the
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
13 documentation and/or other materials provided with the distribution.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
14 3. Neither the name of the copyright holder nor the names of its
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
15 contributors may be used to endorse or promote products derived from
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
16 this software without specific prior written permission.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
17
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
20 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
21 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
22 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
23 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
29 */
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
30
81
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
31 #include "udpsocket.h"
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
32
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
33 #ifndef _WIN32
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
34 # include <sys/socket.h>
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
35 # include <netinet/in.h>
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 81
diff changeset
36 # include <sys/time.h>
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 81
diff changeset
37 # include <unistd.h>
81
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
38 #else
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
39 # include <winsock2.h>
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
40 # include <ws2tcpip.h>
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
41 #endif
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
42
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 #include <sys/types.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 #include <string.h>
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 #include <fcntl.h>
8
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 6
diff changeset
46 #include "../huffman/huffman.h"
8b697d30c49f - added huffman lib, now capable of initializing an rcon connection!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 6
diff changeset
47
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 81
diff changeset
48 BEGIN_ZFC_NAMESPACE
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 81
diff changeset
49
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
50 static char HuffmanBuffer[131072];
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
52 net::UDPSocket::UDPSocket() :
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
53 file_descriptor{::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)} {}
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
55 net::UDPSocket::~UDPSocket()
56
b4caacf567af - I think closing sockets is a good idea
Teemu Piippo <crimsondusk64@gmail.com>
parents: 31
diff changeset
56 {
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 81
diff changeset
57 #ifdef _WIN32
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
58 ::closesocket(m_socket);
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 81
diff changeset
59 #else
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
60 ::close(this->file_descriptor);
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 81
diff changeset
61 #endif
56
b4caacf567af - I think closing sockets is a good idea
Teemu Piippo <crimsondusk64@gmail.com>
parents: 31
diff changeset
62 }
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
64 bool net::UDPSocket::set_blocking(bool a, std::ostream& errors)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 {
81
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
66 #ifndef _WIN32
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
67 int flags = ::fcntl(this->file_descriptor, F_GETFL, 0);
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
68 int newflags = a ?(flags & ~O_NONBLOCK) :(flags | O_NONBLOCK);
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
69 if (flags < 0 || ::fcntl(this->file_descriptor, F_SETFL, newflags) != 0)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 {
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
71 errors << "Unable to set the UDP socket as non-blocking";
5
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 }
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
74 else
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
75 {
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
76 return true;
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
77 }
81
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
78 #else
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
79 unsigned long mode = a ? 0 : 1;
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
80
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
81 if (::ioctlsocket(m_socket, FIONBIO, &mode) != 0)
81
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
82 {
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
83 errors << strerror(errno);
81
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
84 return false;
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
85 }
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
86 else
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
87 {
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
88 return true;
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
89 }
81
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
90 #endif
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
93 bool net::UDPSocket::bind(const net::port_t port, std::ostream& errors)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 {
19
2046a1651c0b - removed needless 'struct's
Teemu Piippo <crimsondusk64@gmail.com>
parents: 10
diff changeset
95 sockaddr_in svaddr;
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
96 std::memset(&svaddr, 0, sizeof svaddr);
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 svaddr.sin_family = AF_INET;
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
98 svaddr.sin_port = htons(port);
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
99 svaddr.sin_addr.s_addr = htonl(INADDR_ANY);
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
100 if (::bind(this->file_descriptor, reinterpret_cast<sockaddr*>(&svaddr), sizeof svaddr) == -1)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 {
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
102 errors << "Couldn't bind to port "s + std::to_string(port);
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 return false;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 }
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
105 else
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
106 {
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
107 return true;
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
108 }
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
111 bool net::UDPSocket::read(Datagram& datagram, std::ostream& errors)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 sockaddr_in claddr;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 socklen_t socklen = sizeof claddr;
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
115 const int length = ::recvfrom(
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
116 this->file_descriptor,
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
117 zfc::HuffmanBuffer,
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
118 sizeof zfc::HuffmanBuffer,
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
119 0,
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
120 reinterpret_cast<sockaddr*>(&claddr),
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
121 &socklen
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
122 );
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 if (length == -1)
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 if (errno != EWOULDBLOCK)
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
126 {
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
127 errors << std::string("recvfrom error: ") + std::strerror(errno);
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
128 }
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 return false;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 }
10
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
131 unsigned char decodedPacket[MAX_DATAGRAM_LENGTH];
3874575d924d - begin work on rcon sessions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 9
diff changeset
132 int decodedLength = sizeof decodedPacket;
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
133 ::HUFFMAN_Decode(reinterpret_cast<unsigned char*>(HuffmanBuffer),
81
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
134 decodedPacket, length, &decodedLength);
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
135 datagram.address.host = ntohl(claddr.sin_addr.s_addr);
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
136 datagram.address.port = ntohs(claddr.sin_port);
180
2e7225dbd9b2 continue cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 158
diff changeset
137 datagram.message = ByteArray{&decodedPacket[0], &decodedPacket[decodedLength]};
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 return true;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
141 bool net::UDPSocket::send(const net::ip_address& address, const ByteArray& data, std::ostream& errors)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 {
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 81
diff changeset
143 int encodedlength = sizeof HuffmanBuffer;
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
144 ::HUFFMAN_Encode(data.data(), reinterpret_cast<unsigned char*>(HuffmanBuffer), data.size(), &encodedlength);
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
145 sockaddr_in claddr = net::ip_address_to_sockaddr_in(address);
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
146 const int send_result = ::sendto(
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
147 this->file_descriptor,
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
148 HuffmanBuffer,
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
149 encodedlength,
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
150 0,
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
151 reinterpret_cast<sockaddr*>(&claddr),
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
152 sizeof claddr
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
153 );
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
154 if (send_result == -1)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 {
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
156 errors << "Unable to launch packet: "s + std::strerror(errno);
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 return false;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 }
189
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
159 else
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
160 {
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
161 return true;
248d0b85cbda various changes, better network error handling
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
162 }
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163 }
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 81
diff changeset
164
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 81
diff changeset
165 END_ZFC_NAMESPACE

mercurial