sources/network/udpsocket.cpp

changeset 88
08ccaf26cffd
parent 81
a18aaf460648
child 109
e4966d7e615d
child 130
9f54db6f9922
equal deleted inserted replaced
87:53c2aecb9704 88:08ccaf26cffd
31 #include "udpsocket.h" 31 #include "udpsocket.h"
32 32
33 #ifndef _WIN32 33 #ifndef _WIN32
34 # include <sys/socket.h> 34 # include <sys/socket.h>
35 # include <netinet/in.h> 35 # include <netinet/in.h>
36 # include <sys/time.h>
37 # include <unistd.h>
36 #else 38 #else
37 # include <winsock2.h> 39 # include <winsock2.h>
38 # include <ws2tcpip.h> 40 # include <ws2tcpip.h>
39 #endif 41 #endif
40 42
41 #include <sys/types.h> 43 #include <sys/types.h>
42 #include <sys/time.h>
43 #include <string.h> 44 #include <string.h>
44 #include <fcntl.h> 45 #include <fcntl.h>
45 #include <unistd.h>
46 #include "../huffman/huffman.h" 46 #include "../huffman/huffman.h"
47 47
48 static char g_huffmanBuffer[131072]; 48 BEGIN_ZFC_NAMESPACE
49
50 char UDPSocket::HuffmanBuffer[131072];
49 51
50 // ----------------------------------------------------------------------------- 52 // -----------------------------------------------------------------------------
51 // 53 //
52 UDPSocket::UDPSocket() : 54 UDPSocket::UDPSocket() :
53 m_socket (socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) {} 55 m_socket (socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) {}
54 56
55 // ----------------------------------------------------------------------------- 57 // -----------------------------------------------------------------------------
56 // 58 //
57 UDPSocket::~UDPSocket() 59 UDPSocket::~UDPSocket()
58 { 60 {
61 #ifdef _WIN32
62 closesocket (m_socket);
63 #else
59 close (m_socket); 64 close (m_socket);
65 #endif
60 } 66 }
61 67
62 // ------------------------------------------------------------------------------------------------- 68 // -------------------------------------------------------------------------------------------------
63 // 69 //
64 bool UDPSocket::set_blocking (bool a) 70 bool UDPSocket::set_blocking (bool a)
87 #endif 93 #endif
88 } 94 }
89 95
90 // ------------------------------------------------------------------------------------------------- 96 // -------------------------------------------------------------------------------------------------
91 // 97 //
92 METHOD 98 bool UDPSocket::bind (unsigned short port)
93 UDPSocket::bind (unsigned short port) -> bool
94 { 99 {
95 sockaddr_in svaddr; 100 sockaddr_in svaddr;
96 memset (&svaddr, 0, sizeof svaddr); 101 memset (&svaddr, 0, sizeof svaddr);
97 svaddr.sin_family = AF_INET; 102 svaddr.sin_family = AF_INET;
98 svaddr.sin_port = htons (port); 103 svaddr.sin_port = htons (port);
111 // 116 //
112 bool UDPSocket::read (Datagram& datagram) 117 bool UDPSocket::read (Datagram& datagram)
113 { 118 {
114 sockaddr_in claddr; 119 sockaddr_in claddr;
115 socklen_t socklen = sizeof claddr; 120 socklen_t socklen = sizeof claddr;
116 int length = ::recvfrom (m_socket, g_huffmanBuffer, sizeof g_huffmanBuffer, 0, 121 int length = ::recvfrom (m_socket, HuffmanBuffer, sizeof HuffmanBuffer, 0,
117 reinterpret_cast<sockaddr*> (&claddr), &socklen); 122 reinterpret_cast<sockaddr*> (&claddr), &socklen);
118 123
119 if (length == -1) 124 if (length == -1)
120 { 125 {
121 if (errno != EWOULDBLOCK) 126 if (errno != EWOULDBLOCK)
124 return false; 129 return false;
125 } 130 }
126 131
127 unsigned char decodedPacket[MAX_DATAGRAM_LENGTH]; 132 unsigned char decodedPacket[MAX_DATAGRAM_LENGTH];
128 int decodedLength = sizeof decodedPacket; 133 int decodedLength = sizeof decodedPacket;
129 HUFFMAN_Decode (reinterpret_cast<unsigned char*> (g_huffmanBuffer), 134 HUFFMAN_Decode (reinterpret_cast<unsigned char*> (HuffmanBuffer),
130 decodedPacket, length, &decodedLength); 135 decodedPacket, length, &decodedLength);
131 datagram.from.host = ntohl (claddr.sin_addr.s_addr); 136 datagram.from.host = ntohl (claddr.sin_addr.s_addr);
132 datagram.from.port = ntohs (claddr.sin_port); 137 datagram.from.port = ntohs (claddr.sin_port);
133 datagram.data = Bytestream (decodedPacket, decodedLength); 138 datagram.data = Bytestream (decodedPacket, decodedLength);
134 return true; 139 return true;
135 } 140 }
136 141
137 // ------------------------------------------------------------------------------------------------- 142 // -------------------------------------------------------------------------------------------------
138 // 143 //
139 METHOD 144 bool UDPSocket::send (const IPAddress& address, const Bytestream& data)
140 UDPSocket::send (const IPAddress& address, const Bytestream& data) -> bool
141 { 145 {
142 int encodedlength = sizeof g_huffmanBuffer; 146 int encodedlength = sizeof HuffmanBuffer;
143 HUFFMAN_Encode (data.data(), reinterpret_cast<unsigned char*> (g_huffmanBuffer), 147 HUFFMAN_Encode (data.data(), reinterpret_cast<unsigned char*> (HuffmanBuffer),
144 data.written_length(), &encodedlength); 148 data.written_length(), &encodedlength);
145 sockaddr_in claddr = address.to_sockaddr_in(); 149 sockaddr_in claddr = address.to_sockaddr_in();
146 int res = ::sendto (m_socket, g_huffmanBuffer, encodedlength, 0, 150 int res = ::sendto (m_socket, HuffmanBuffer, encodedlength, 0,
147 reinterpret_cast<sockaddr*> (&claddr), sizeof claddr); 151 reinterpret_cast<sockaddr*> (&claddr), sizeof claddr);
148 152
149 if (res == -1) 153 if (res == -1)
150 { 154 {
151 m_error = String ("Unable to launch packet: ") + strerror (errno); 155 m_error = String ("Unable to launch packet: ") + strerror (errno);
152 return false; 156 return false;
153 } 157 }
154 158
155 return true; 159 return true;
156 } 160 }
161
162 END_ZFC_NAMESPACE

mercurial