⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketapi.cpp

📁 天之炼狱1服务器端源文件游戏服务端不完整
💻 CPP
📖 第 1 页 / 共 3 页
字号:
////////////////////////////////////////////////////////////////////////// SocketAPI.cpp//// by Reiot, the Fallen Lord of MUDMANIA(TM)//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// include files//////////////////////////////////////////////////#include "SocketAPI.h"#if __WINDOWS__#elif __LINUX__#include <sys/types.h>			// for accept()#include <sys/socket.h>#include <sys/time.h>#include <arpa/inet.h>			// for inet_xxx()#include <netinet/in.h>#include <errno.h>				// for errno#endif#include "FileAPI.h"//////////////////////////////////////////////////// external variable//////////////////////////////////////////////////#if __LINUX__extern int errno;#endifusing namespace FileAPI;////////////////////////////////////////////////////////////////////////// SOCKET SocketAPI::socket_ex ( int domain , int type , int protocol ) //		throw ( Error )//// exception version of socket()//// Parameters//     domain - AF_INET(internet socket), AF_UNIX(internal socket), ...//	   type  - SOCK_STREAM(TCP), SOCK_DGRAM(UDP), ...//     protocol - 0//// Return //     socket descriptor//// Exceptions//     Error////////////////////////////////////////////////////////////////////////SOCKET SocketAPI::socket_ex ( int domain , int type , int protocol ) 	throw ( Error ){	__BEGIN_TRY	SOCKET s = ::socket(domain,type,protocol);	if ( s == INVALID_SOCKET ) {#if __LINUX__		switch ( errno ) {		case EPROTONOSUPPORT :			throw Error("The protocol type or the specified protocol is not supported within this domain.");		case EMFILE : 			throw Error("The per-process descriptor table is full.");		case ENFILE : 			throw Error("The system file table is full.");		case EACCES : 			throw Error("Permission to create a socket of the specified type and/or protocol is denied.");		case ENOBUFS : 			throw Error("Insufficient buffer space is available. The socket cannot be created until sufficient resources are freed.");		default : 			throw UnknownError(strerror(errno),errno);		}//end of switch#elif __WINDOWS__		switch ( WSAGetLastError() ) {		case WSANOTINITIALISED : 			throw Error("A successful WSAStartup must occur before using this function.");		case WSAENETDOWN : 			throw Error("The network subsystem or the associated service provider has failed.");		case WSAEAFNOSUPPORT : 			throw Error("The specified address family is not supported.");		case WSAEINPROGRESS : 			throw Error("A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.");		case WSAEMFILE : 			throw Error("No more socket descriptors are available.");		case WSAENOBUFS : 			throw Error("No buffer space is available. The socket cannot be created.");		case WSAEPROTONOSUPPORT : 			throw Error("The specified protocol is not supported.");		case WSAEPROTOTYPE : 			throw Error("The specified protocol is the wrong type for this socket.");		case WSAESOCKTNOSUPPORT : 			throw Error("The specified socket type is not supported in this address family.");		default : 			throw UnknownError("socket()");		}//end of switch#endif	}	return s;		__END_CATCH}////////////////////////////////////////////////////////////////////////// void SocketAPI::bind_ex ( SOCKET s , const struct sockaddr * addr , uint addrlen ) //      throw ( MBindException , //              Error );//// exception version of bind()//// Parameters//     s       - socket descriptor //     addr    - socket address structure ( normally struct sockaddr_in )//     addrlen - length of socket address structure//// Return//     none//// Exceptions//     MBindException//     Error////////////////////////////////////////////////////////////////////////void SocketAPI::bind_ex ( SOCKET s , const struct sockaddr * addr , uint addrlen )      throw ( BindException ,              Error ){	__BEGIN_TRY	if ( bind ( s , addr , addrlen ) == SOCKET_ERROR ) {#if __LINUX__		switch ( errno ) {		case EADDRINUSE :			throw BindException("The address is already in use. kill another server or use another port. 家南狼 林家 趣篮 器飘啊 捞固 荤侩吝涝聪促. 扁粮狼 辑滚 家南阑 辆丰窍芭唱, 促弗 器飘甫 荤侩窍矫扁 官而聪促.");		case EINVAL : 			throw BindException("The socket is already bound to an address , or the addr_len was wrong, or the socket was not in the AF_UNIX family.");		case EACCES : 			throw BindException("The address is protected, and the user is not the super-user. or search permission is denied on a component of the path prefix.");		case ENOTSOCK : 			throw Error("Argument is a descriptor for a file, not a socket. The following errors are specific to UNIX domain (AF_UNIX) sockets:");		case EBADF : 			throw Error("sockfd is not a valid descriptor.");		case EROFS : 			throw Error("The socket inode would reside on a read-only file system.");		case EFAULT : 			throw Error("my_addr points outside your accessible address space.");		case ENAMETOOLONG : 			throw Error("my_addr is too long.");		case ENOENT : 			throw Error("The file does not exist.");		case ENOMEM : 			throw Error("Insufficient kernel memory was available.");		case ENOTDIR : 			throw Error("A component of the path prefix is not a directory.");		case ELOOP : 			throw Error("Too many symbolic links were encountered in resolving my_addr.");		default :			throw UnknownError(strerror(errno),errno);		}//end of switch#elif __WINDOWS__		switch ( WSAGetLastError() ) {		case WSANOTINITIALISED : 			throw Error("A successful WSAStartup must occur before using this function.");		case WSAENETDOWN : 			throw Error("The network subsystem has failed.");		case WSAEADDRINUSE : 			throw BindException("A process on the machine is already bound to the same fully-qualified address and the socket has not been marked to allow address re-use with SO_REUSEADDR. For example, IP address and port are bound in the af_inet case) . (See the SO_REUSEADDR socket option under setsockopt.)");		case WSAEADDRNOTAVAIL : 			throw BindException("The specified address is not a valid address for this machine.");		case WSAEFAULT : 			throw BindException("The name or the namelen parameter is not a valid part of the user address space, the namelen parameter is too small, the name parameter contains incorrect address format for the associated address family, or the first two bytes of the memory block specified by name does not match the address family associated with the socket descriptor s.");		case WSAEINPROGRESS : 			throw Error("A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.");		case WSAEINVAL : 			throw BindException("The socket is already bound to an address.");		case WSAENOBUFS : 			throw Error("Not enough buffers available, too many connections.");		case WSAENOTSOCK : 			throw Error("The descriptor is not a socket.");		default :			throw UnknownError("bind()");		}//end of switch#endif	}		__END_CATCH}////////////////////////////////////////////////////////////////////////// void SocketAPI::connect_ex ( SOCKET s , const struct sockaddr * addr , uint addrlen )//      throw ( ConnectException , //              NonBlockingIOException , //              Error );//// exception version of connect() system call//// Parameters//     s       - socket descriptor//     addr    - socket address structure//     addrlen - length of socket address structure//// Return//     none//// Exceptions//     ConnectException//     NonBlockingIOException//     Error////////////////////////////////////////////////////////////////////////void SocketAPI::connect_ex ( SOCKET s , const struct sockaddr * addr , uint addrlen )     throw ( ConnectException ,              NonBlockingIOException ,              Error ){	__BEGIN_TRY	if ( connect(s,addr,addrlen) == SOCKET_ERROR ) {#if __LINUX__		switch ( errno ) {		case EALREADY : 			throw NonBlockingIOException("The socket is non-blocking and a previous connection attempt has not yet been completed.");		case EINPROGRESS : 			throw ConnectException("The socket is non-blocking and the connection can not be completed immediately.");		case ECONNREFUSED : 			throw ConnectException("Connection refused at server.");		case EISCONN : 			throw ConnectException("The socket is already connected.");		case ETIMEDOUT : 			throw ConnectException("Timeout while attempting connection.");		case ENETUNREACH : 			throw ConnectException("Network is unreachable.");		case EADDRINUSE : 			throw ConnectException("Address is already in use.");		case EBADF : 			throw Error("Bad descriptor.");		case EFAULT : 			throw Error("The socket structure address is outside your address space.");		case ENOTSOCK : 			throw Error("The descriptor is not associated with a socket.");		default :			throw UnknownError(strerror(errno),errno);		}//end of switch#elif __WINDOWS__		switch ( WSAGetLastError() ) {		case WSANOTINITIALISED : 			throw Error("A successful WSAStartup must occur before using this function.");		case WSAENETDOWN : 			throw Error("The network subsystem has failed.");		case WSAEADDRINUSE : 			throw Error("The socket's local address is already in use and the socket was not marked to allow address reuse with SO_REUSEADDR. This error usually occurs when executing bind, but could be delayed until this function if the bind was to a partially wild-card address (involving ADDR_ANY) and if a specific address needs to be committed at the time of this function.");		case WSAEINTR : 			throw Error("The (blocking) Windows Socket 1.1 call was canceled through WSACancelBlockingCall.");		case WSAEINPROGRESS : 			throw Error("A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.");		case WSAEALREADY : 			throw Error("A nonblocking connect call is in progress on the specified socket. Note In order to preserve backward compatibility, this error is reported as WSAEINVAL to Windows Sockets 1.1 applications that link to either WINSOCK.DLL or WSOCK32.DLL."); 		case WSAEADDRNOTAVAIL : 			throw ConnectException("The remote address is not a valid address (such as ADDR_ANY).");		case WSAEAFNOSUPPORT : 			throw Error("Addresses in the specified family cannot be used with this socket.");		case WSAECONNREFUSED : 			throw ConnectException("The attempt to connect was forcefully rejected.");		case WSAEFAULT : 			throw Error("The name or the namelen parameter is not a valid part of the user address space, the namelen parameter is too small, or the name parameter contains incorrect address format for the associated address family.");		case WSAEINVAL : 			throw Error("The parameter s is a listening socket, or the destination address specified is not consistent with that of the constrained group the socket belongs to.");		case WSAEISCONN : 			throw ConnectException("The socket is already connected (connection-oriented sockets only).");		case WSAENETUNREACH : 			throw ConnectException("The network cannot be reached from this host at this time.");		case WSAENOBUFS : 			throw Error("No buffer space is available. The socket cannot be connected.");		case WSAENOTSOCK : 			throw Error("The descriptor is not a socket.");		case WSAETIMEDOUT : 			throw ConnectException("Attempt to connect timed out without establishing a connection.");		case WSAEWOULDBLOCK  : 			throw NonBlockingIOException("The socket is marked as nonblocking and the connection cannot be completed immediately.");		default :			throw UnknownError("connect()");		}//end of switch#endif	}		__END_CATCH}////////////////////////////////////////////////////////////////////////// void SocketAPI::listen_ex ( SOCKET s , uint backlog )//      throw ( Error );//// exception version of listen()//// Parameters//     s       - socket descriptor//     backlog - waiting queue length//// Return//     none//// Exceptions//     Error////////////////////////////////////////////////////////////////////////void SocketAPI::listen_ex ( SOCKET s , uint backlog )      throw ( Error ){	__BEGIN_TRY	if ( listen( s , backlog ) == SOCKET_ERROR ) {#if __LINUX__		switch ( errno ) {		case EBADF : 			throw Error("Bad descriptor.");		case ENOTSOCK :			throw Error("Not a socket.");		case EOPNOTSUPP :			throw Error("The socket is not of a type that supports the operation listen.");		default :			throw UnknownError(strerror(errno),errno);		}//end of switch#elif __WINDOWS__		switch ( WSAGetLastError() ) {		case WSANOTINITIALISED : 			throw Error("A successful WSAStartup must occur before using this function.");		case WSAENETDOWN : 			throw Error("The network subsystem has failed.");		case WSAEADDRINUSE : 			throw Error("The socket's local address is already in use and the socket was not marked to allow address reuse with SO_REUSEADDR. This error usually occurs during execution of the bind function, but could be delayed until this function if the bind was to a partially wild-card address (involving ADDR_ANY) and if a specific address needs to be 'committed' at the time of this function.");		case WSAEINPROGRESS : 			throw Error("A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.");		case WSAEINVAL : 			throw Error("The socket has not been bound with bind.");		case WSAEISCONN : 			throw Error("The socket is already connected.");		case WSAEMFILE : 			throw Error("No more socket descriptors are available.");		case WSAENOBUFS : 			throw Error("No buffer space is available.");		case WSAENOTSOCK : 			throw Error("The descriptor is not a socket.");		case WSAEOPNOTSUPP : 			throw Error("The referenced socket is not of a type that supports the listen operation.");		default :			throw UnknownError("listen()");		}//end of switch#endif	}		__END_CATCH}//////////////////////////////////////////////////////////////////////////SOCKET SocketAPI::accept_ex ( SOCKET s , struct sockaddr * addr , uint * addrlen ) //       throw ( NonBlockingIOException , //               ConnectException ,//               Error );//// exception version of accept()//// Parameters//     s       - socket descriptor//     addr    - socket address structure//     addrlen - length of socket address structure//// Return//     none//// Exceptions//     NonBlockingIOException//     Error////////////////////////////////////////////////////////////////////////SOCKET SocketAPI::accept_ex ( SOCKET s , struct sockaddr * addr , uint * addrlen )       throw ( NonBlockingIOException , ConnectException , Error ){	__BEGIN_TRY#if __LINUX__	SOCKET client = accept( s , addr , addrlen );#elif __WINDOWS__	SOCKET client = accept( s , addr , (int*)addrlen );#endif		if ( client == INVALID_SOCKET ) {#if __LINUX__		switch ( errno ) {		case EWOULDBLOCK : 			throw NonBlockingIOException();		case ECONNRESET :		case ECONNABORTED :		case EPROTO :		case EINTR :			// from UNIX Network Programming 2nd, 15.6			// with nonblocking-socket, ignore above errors			throw ConnectException(strerror(errno));		case EBADF : 		case ENOTSOCK : 		case EOPNOTSUPP : 		case EFAULT : 			throw Error(strerror(errno));		default :			throw UnknownError(strerror(errno),errno);		}//end of switch#elif __WINDOWS__		switch ( WSAGetLastError() ) {		case WSANOTINITIALISED : 			throw Error("A successful WSAStartup must occur before using this FUNCTION.");		case WSAENETDOWN : 			throw Error("The network subsystem has failed.");		case WSAEFAULT : 			throw Error("The addrlen parameter is too small or addr is not a valid part of the user address space.");		case WSAEINTR : 			throw Error("A blocking Windows Sockets 1.1 call was canceled through WSACancelBlockingCall.");		case WSAEINPROGRESS : 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -