sources/network/udpsocket.cpp

branch
protocol5
changeset 84
3bd32eec3d57
parent 81
a18aaf460648
child 88
08ccaf26cffd
--- a/sources/network/udpsocket.cpp	Sun May 17 22:07:48 2015 +0300
+++ b/sources/network/udpsocket.cpp	Wed May 27 21:15:52 2015 +0300
@@ -28,17 +28,24 @@
 	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
-#include <sys/socket.h>
+#include "udpsocket.h"
+
+#ifndef _WIN32
+# include <sys/socket.h>
+# include <netinet/in.h>
+#else
+# include <winsock2.h>
+# include <ws2tcpip.h>
+#endif
+
 #include <sys/types.h>
 #include <sys/time.h>
-#include <netinet/in.h>
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
-#include "udpsocket.h"
 #include "../huffman/huffman.h"
 
-static unsigned char g_huffmanBuffer[131072];
+static char g_huffmanBuffer[131072];
 
 // -----------------------------------------------------------------------------
 //
@@ -54,9 +61,9 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-UDPSocket::set_blocking (bool a) -> bool
+bool UDPSocket::set_blocking (bool a)
 {
+#ifndef _WIN32
 	int flags = fcntl (m_socket, F_GETFL, 0);
 	int newflags = a ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
 
@@ -67,6 +74,17 @@
 	}
 
 	return true;
+#else
+	unsigned long mode = a ? 0 : 1;
+
+	if (ioctlsocket (m_socket, FIONBIO, &mode) != 0)
+	{
+		m_error = strerror (errno);
+		return false;
+	}
+
+	return true;
+#endif
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -91,8 +109,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-METHOD
-UDPSocket::read (Datagram& datagram) -> bool
+bool UDPSocket::read (Datagram& datagram)
 {
 	sockaddr_in claddr;
 	socklen_t socklen = sizeof claddr;
@@ -109,7 +126,8 @@
 
 	unsigned char decodedPacket[MAX_DATAGRAM_LENGTH];
 	int decodedLength = sizeof decodedPacket;
-	HUFFMAN_Decode (g_huffmanBuffer, decodedPacket, length, &decodedLength);
+	HUFFMAN_Decode (reinterpret_cast<unsigned char*> (g_huffmanBuffer),
+		decodedPacket, length, &decodedLength);
 	datagram.from.host = ntohl (claddr.sin_addr.s_addr);
 	datagram.from.port = ntohs (claddr.sin_port);
 	datagram.data = Bytestream (decodedPacket, decodedLength);
@@ -122,7 +140,8 @@
 UDPSocket::send (const IPAddress& address, const Bytestream& data) -> bool
 {
 	int encodedlength = sizeof g_huffmanBuffer;
-	HUFFMAN_Encode (data.data(), g_huffmanBuffer, data.written_length(), &encodedlength);
+	HUFFMAN_Encode (data.data(), reinterpret_cast<unsigned char*> (g_huffmanBuffer),
+		data.written_length(), &encodedlength);
 	sockaddr_in claddr = address.to_sockaddr_in();
 	int res = ::sendto (m_socket, g_huffmanBuffer, encodedlength, 0,
 		reinterpret_cast<sockaddr*> (&claddr), sizeof claddr);

mercurial