sources/network/udpsocket.cpp

branch
protocol5
changeset 106
7b156b764d11
parent 88
08ccaf26cffd
child 109
e4966d7e615d
child 130
9f54db6f9922
--- a/sources/network/udpsocket.cpp	Sat Jan 09 02:35:00 2016 +0200
+++ b/sources/network/udpsocket.cpp	Sat Jan 09 17:41:21 2016 +0200
@@ -33,19 +33,21 @@
 #ifndef _WIN32
 # include <sys/socket.h>
 # include <netinet/in.h>
+# include <sys/time.h>
+# include <unistd.h>
 #else
 # include <winsock2.h>
 # include <ws2tcpip.h>
 #endif
 
 #include <sys/types.h>
-#include <sys/time.h>
 #include <string.h>
 #include <fcntl.h>
-#include <unistd.h>
 #include "../huffman/huffman.h"
 
-static char g_huffmanBuffer[131072];
+BEGIN_ZFC_NAMESPACE
+
+char UDPSocket::HuffmanBuffer[131072];
 
 // -----------------------------------------------------------------------------
 //
@@ -56,7 +58,11 @@
 //
 UDPSocket::~UDPSocket()
 {
+#ifdef _WIN32
+	closesocket (m_socket);
+#else
 	close (m_socket);
+#endif
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -89,8 +95,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-UDPSocket::bind (unsigned short port) -> bool
+bool UDPSocket::bind (unsigned short port)
 {
 	sockaddr_in svaddr;
 	memset (&svaddr, 0, sizeof svaddr);
@@ -113,7 +118,7 @@
 {
 	sockaddr_in claddr;
 	socklen_t socklen = sizeof claddr;
-	int length = ::recvfrom (m_socket, g_huffmanBuffer, sizeof g_huffmanBuffer, 0,
+	int length = ::recvfrom (m_socket, HuffmanBuffer, sizeof HuffmanBuffer, 0,
 		reinterpret_cast<sockaddr*> (&claddr), &socklen);
 
 	if (length == -1)
@@ -126,7 +131,7 @@
 
 	unsigned char decodedPacket[MAX_DATAGRAM_LENGTH];
 	int decodedLength = sizeof decodedPacket;
-	HUFFMAN_Decode (reinterpret_cast<unsigned char*> (g_huffmanBuffer),
+	HUFFMAN_Decode (reinterpret_cast<unsigned char*> (HuffmanBuffer),
 		decodedPacket, length, &decodedLength);
 	datagram.from.host = ntohl (claddr.sin_addr.s_addr);
 	datagram.from.port = ntohs (claddr.sin_port);
@@ -136,14 +141,13 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-UDPSocket::send (const IPAddress& address, const Bytestream& data) -> bool
+bool UDPSocket::send (const IPAddress& address, const Bytestream& data)
 {
-	int encodedlength = sizeof g_huffmanBuffer;
-	HUFFMAN_Encode (data.data(), reinterpret_cast<unsigned char*> (g_huffmanBuffer),
+	int encodedlength = sizeof HuffmanBuffer;
+	HUFFMAN_Encode (data.data(), reinterpret_cast<unsigned char*> (HuffmanBuffer),
 		data.written_length(), &encodedlength);
 	sockaddr_in claddr = address.to_sockaddr_in();
-	int res = ::sendto (m_socket, g_huffmanBuffer, encodedlength, 0,
+	int res = ::sendto (m_socket, HuffmanBuffer, encodedlength, 0,
 		reinterpret_cast<sockaddr*> (&claddr), sizeof claddr);
 
 	if (res == -1)
@@ -154,3 +158,5 @@
 
 	return true;
 }
+
+END_ZFC_NAMESPACE
\ No newline at end of file

mercurial