summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/Socket.h
diff options
context:
space:
mode:
authorTiger Wang <ziwei.tiger@hotmail.co.uk>2013-11-27 22:35:13 +0100
committerTiger Wang <ziwei.tiger@hotmail.co.uk>2013-11-27 22:35:13 +0100
commita6630d32394120a78af56bc612fa3c3449283248 (patch)
tree2c791266b0f213cd56961299da8d2258b8f85d8e /src/OSSupport/Socket.h
parentFixed spawn point being generally in an ocean (diff)
parentVoronoi-related biomegens use the new cVoronoiMap class. (diff)
downloadcuberite-a6630d32394120a78af56bc612fa3c3449283248.tar
cuberite-a6630d32394120a78af56bc612fa3c3449283248.tar.gz
cuberite-a6630d32394120a78af56bc612fa3c3449283248.tar.bz2
cuberite-a6630d32394120a78af56bc612fa3c3449283248.tar.lz
cuberite-a6630d32394120a78af56bc612fa3c3449283248.tar.xz
cuberite-a6630d32394120a78af56bc612fa3c3449283248.tar.zst
cuberite-a6630d32394120a78af56bc612fa3c3449283248.zip
Diffstat (limited to 'src/OSSupport/Socket.h')
-rw-r--r--src/OSSupport/Socket.h102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/OSSupport/Socket.h b/src/OSSupport/Socket.h
new file mode 100644
index 000000000..81bfd28fc
--- /dev/null
+++ b/src/OSSupport/Socket.h
@@ -0,0 +1,102 @@
+
+#pragma once
+
+
+
+
+
+class cSocket
+{
+public:
+ enum eFamily
+ {
+ IPv4 = AF_INET,
+ IPv6 = AF_INET6,
+ } ;
+
+#ifdef _WIN32
+ typedef SOCKET xSocket;
+#else
+ typedef int xSocket;
+ static const int INVALID_SOCKET = -1;
+#endif
+
+ cSocket(void) : m_Socket(INVALID_SOCKET) {}
+ cSocket(xSocket a_Socket);
+ ~cSocket();
+
+ bool IsValid(void) const { return IsValidSocket(m_Socket); }
+ void CloseSocket(void);
+
+ operator xSocket(void) const;
+ xSocket GetSocket(void) const;
+
+ bool operator == (const cSocket & a_Other) {return m_Socket == a_Other.m_Socket; }
+
+ void SetSocket(xSocket a_Socket);
+
+ /// Sets the address-reuse socket flag; returns true on success
+ bool SetReuseAddress(void);
+
+ /// Initializes the network stack. Returns 0 on success, or another number as an error code.
+ static int WSAStartup(void);
+
+ static AString GetErrorString(int a_ErrNo);
+ static int GetLastError();
+ static AString GetLastErrorString(void)
+ {
+ return GetErrorString(GetLastError());
+ }
+
+ /// Creates a new socket of the specified address family
+ static cSocket CreateSocket(eFamily a_Family);
+
+ inline static bool IsSocketError(int a_ReturnedValue)
+ {
+ #ifdef _WIN32
+ return (a_ReturnedValue == SOCKET_ERROR || a_ReturnedValue == 0);
+ #else
+ return (a_ReturnedValue <= 0);
+ #endif
+ }
+
+ static bool IsValidSocket(xSocket a_Socket);
+
+ static const unsigned short ANY_PORT = 0; // When given to Bind() functions, they will find a free port
+ static const int DEFAULT_BACKLOG = 10;
+
+ /// Binds to the specified port on "any" interface (0.0.0.0). Returns true if successful.
+ bool BindToAnyIPv4(unsigned short a_Port);
+
+ /// Binds to the specified port on "any" interface (::/128). Returns true if successful.
+ bool BindToAnyIPv6(unsigned short a_Port);
+
+ /// Binds to the specified port on localhost interface (127.0.0.1) through IPv4. Returns true if successful.
+ bool BindToLocalhostIPv4(unsigned short a_Port);
+
+ /// Sets the socket to listen for incoming connections. Returns true if successful.
+ bool Listen(int a_Backlog = DEFAULT_BACKLOG);
+
+ /// Accepts an IPv4 incoming connection. Blocks if none available.
+ cSocket AcceptIPv4(void);
+
+ /// Accepts an IPv6 incoming connection. Blocks if none available.
+ cSocket AcceptIPv6(void);
+
+ /// Connects to a localhost socket on the specified port using IPv4; returns true if successful.
+ bool ConnectToLocalhostIPv4(unsigned short a_Port);
+
+ /// Connects to the specified host or string IP address and port, using IPv4. Returns true if successful.
+ bool ConnectIPv4(const AString & a_HostNameOrAddr, unsigned short a_Port);
+
+ int Receive(char * a_Buffer, unsigned int a_Length, unsigned int a_Flags);
+ int Send (const char * a_Buffer, unsigned int a_Length);
+
+ unsigned short GetPort(void) const; // Returns 0 on failure
+
+ const AString & GetIPString(void) const { return m_IPString; }
+
+private:
+ xSocket m_Socket;
+ AString m_IPString;
+}; \ No newline at end of file