summaryrefslogtreecommitdiffstats
path: root/source/cSocket.cpp
diff options
context:
space:
mode:
authorfaketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6>2011-10-23 02:18:44 +0200
committerfaketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6>2011-10-23 02:18:44 +0200
commitcf94994f8e3b34bc871e2c3b2558f8301146810c (patch)
tree4af0a1b7161e077689f077bb5e14083b8c51b638 /source/cSocket.cpp
parentFound something that calls a function when a segfault happens, might be useful. (diff)
downloadcuberite-cf94994f8e3b34bc871e2c3b2558f8301146810c.tar
cuberite-cf94994f8e3b34bc871e2c3b2558f8301146810c.tar.gz
cuberite-cf94994f8e3b34bc871e2c3b2558f8301146810c.tar.bz2
cuberite-cf94994f8e3b34bc871e2c3b2558f8301146810c.tar.lz
cuberite-cf94994f8e3b34bc871e2c3b2558f8301146810c.tar.xz
cuberite-cf94994f8e3b34bc871e2c3b2558f8301146810c.tar.zst
cuberite-cf94994f8e3b34bc871e2c3b2558f8301146810c.zip
Diffstat (limited to 'source/cSocket.cpp')
-rw-r--r--source/cSocket.cpp113
1 files changed, 110 insertions, 3 deletions
diff --git a/source/cSocket.cpp b/source/cSocket.cpp
index edc57c1ce..427441145 100644
--- a/source/cSocket.cpp
+++ b/source/cSocket.cpp
@@ -1,12 +1,20 @@
#include "cSocket.h"
+#include "cMCLogger.h"
#ifndef _WIN32
-#include <netdb.h>
+#include <netdb.h>
#include <unistd.h>
+#include <errno.h>
+//#include <sys/socket.h>
+//#include <netinet/in.h>
+#include <arpa/inet.h> //inet_ntoa()
+#else
+#define socklen_t int
#endif
cSocket::cSocket( xSocket a_Socket )
: m_Socket( a_Socket )
+ , m_IPString( 0 )
{
}
@@ -38,7 +46,106 @@ void cSocket::CloseSocket()
#ifdef _WIN32
closesocket(m_Socket);
#else
- shutdown(m_Socket, SHUT_RDWR);//SD_BOTH);
- close(m_Socket);
+ if( shutdown(m_Socket, SHUT_RDWR) != 0 )//SD_BOTH);
+ LOGWARN("Error on shutting down socket (%s)", m_IPString );
+ if( close(m_Socket) != 0 )
+ LOGWARN("Error closing socket (%s)", m_IPString );
#endif
+}
+
+const char* cSocket::GetLastErrorString()
+{
+#define CASE_AND_RETURN( x ) case x: return #x
+
+#ifdef _WIN32
+ switch( WSAGetLastError() )
+ {
+ CASE_AND_RETURN( WSANOTINITIALISED );
+ CASE_AND_RETURN( WSAENETDOWN );
+ CASE_AND_RETURN( WSAEFAULT );
+ CASE_AND_RETURN( WSAENOTCONN );
+ CASE_AND_RETURN( WSAEINTR );
+ CASE_AND_RETURN( WSAEINPROGRESS );
+ CASE_AND_RETURN( WSAENETRESET );
+ CASE_AND_RETURN( WSAENOTSOCK );
+ CASE_AND_RETURN( WSAEOPNOTSUPP );
+ CASE_AND_RETURN( WSAESHUTDOWN );
+ CASE_AND_RETURN( WSAEWOULDBLOCK );
+ CASE_AND_RETURN( WSAEMSGSIZE );
+ CASE_AND_RETURN( WSAEINVAL );
+ CASE_AND_RETURN( WSAECONNABORTED );
+ CASE_AND_RETURN( WSAETIMEDOUT );
+ CASE_AND_RETURN( WSAECONNRESET );
+ }
+ return "No Error";
+#else
+ return "GetLastErrorString() only works on Windows";
+#endif
+}
+
+int cSocket::SetReuseAddress()
+{
+#ifdef _WIN32
+ char yes = 1;
+#else
+ int yes = 1;
+#endif
+ return setsockopt( m_Socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int) );
+}
+
+int cSocket::WSAStartup()
+{
+#ifdef _WIN32
+ WSADATA wsaData;
+ memset( &wsaData, 0, sizeof( wsaData ) );
+ return ::WSAStartup( MAKEWORD(2, 2),&wsaData);
+#else
+ return 0;
+#endif
+}
+
+cSocket cSocket::CreateSocket()
+{
+ return socket(AF_INET,SOCK_STREAM,0);
+}
+
+int cSocket::Bind( SockAddr_In& a_Address )
+{
+ sockaddr_in local;
+
+ if( a_Address.Family == ADDRESS_FAMILY_INTERNET )
+ local.sin_family = AF_INET;
+
+ if( a_Address.Address == INTERNET_ADDRESS_ANY )
+ local.sin_addr.s_addr = INADDR_ANY;
+
+ local.sin_port=htons( (u_short)a_Address.Port );
+
+ return bind( m_Socket, (sockaddr*)&local, sizeof(local));
+}
+
+int cSocket::Listen( int a_Backlog )
+{
+ return listen( m_Socket, a_Backlog );
+}
+
+cSocket cSocket::Accept()
+{
+ sockaddr_in from;
+ socklen_t fromlen=sizeof(from);
+
+ cSocket SClient = accept( m_Socket, (sockaddr*)&from, &fromlen);
+
+ if( from.sin_addr.s_addr && SClient.IsValid() ) // Get IP in string form
+ {
+ SClient.m_IPString = inet_ntoa(from.sin_addr);
+ //LOG("cSocket::Accept() %s", SClient.m_IPString );
+ }
+
+ return SClient;
+}
+
+int cSocket::Receive( char* a_Buffer, unsigned int a_Length, unsigned int a_Flags )
+{
+ return recv(m_Socket, a_Buffer, a_Length, a_Flags);
} \ No newline at end of file