sources/network/udpsocket.cpp

changeset 8
8b697d30c49f
parent 6
c709bb1a08e4
child 9
e7a09ceb4505
--- a/sources/network/udpsocket.cpp	Thu Dec 11 06:13:32 2014 +0200
+++ b/sources/network/udpsocket.cpp	Thu Dec 11 07:18:11 2014 +0200
@@ -5,6 +5,9 @@
 #include <string.h>
 #include <fcntl.h>
 #include "udpsocket.h"
+#include "../huffman/huffman.h"
+
+static unsigned char g_huffmanBuffer[131072];
 
 // -----------------------------------------------------------------------------
 //
@@ -21,7 +24,7 @@
 UDPSocket::set_blocking (bool a) -> bool
 {
 	int flags = fcntl (m_socket, F_GETFL, 0);
-	int newflags = (a ? (flags | O_NONBLOCK) : (flags & ~O_NONBLOCK));
+	int newflags = (a ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK));
 
 	if (flags < 0 || fcntl (m_socket, F_SETFL, newflags) != 0)
 	{
@@ -60,7 +63,7 @@
 	sockaddr_in claddr;
 	socklen_t socklen = sizeof claddr;
 	static unsigned char packet[MAX_DATAGRAM_LENGTH];
-	int length = ::recvfrom (m_socket, packet, sizeof packet, 0,
+	int length = ::recvfrom (m_socket, g_huffmanBuffer, sizeof packet, 0,
 		reinterpret_cast<struct sockaddr*> (&claddr), &socklen);
 
 	if (length == -1)
@@ -72,20 +75,23 @@
 		return false;
 	}
 
+	int decodedlength = sizeof g_huffmanBuffer;
+	HUFFMAN_Decode (g_huffmanBuffer, packet, length, &decodedlength);
 	datagram.from.host = ntohl (claddr.sin_addr.s_addr);
 	datagram.from.port = ntohs (claddr.sin_port);
-	datagram.data = Bytestream (packet, length);
+	datagram.data = Bytestream (packet, decodedlength);
 	return true;
 }
 
 // -------------------------------------------------------------------------------------------------
 //
 METHOD
-UDPSocket::send (const Bytestream& data, const IPAddress& addr) -> bool
+UDPSocket::send (const IPAddress& address, const Bytestream& data) -> bool
 {
-	struct sockaddr_in claddr = addr.to_sockaddr_in();
-
-	int res = ::sendto (m_socket, data.data(), data.written_length(), 0,
+	int encodedlength = sizeof g_huffmanBuffer;
+	HUFFMAN_Encode (data.data(), g_huffmanBuffer, data.written_length(), &encodedlength);
+	struct sockaddr_in claddr = address.to_sockaddr_in();
+	int res = ::sendto (m_socket, g_huffmanBuffer, encodedlength, 0,
 		reinterpret_cast<struct sockaddr*> (&claddr), sizeof claddr);
 
 	if (res == -1)

mercurial