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

📄 socketapi.cpp

📁 天之炼狱1服务器端源文件游戏服务端不完整
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		case EBADF : 		case ENOTCONN : 		case ENOTSOCK : 		case EINTR : 		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 buf parameter is not completely contained in a valid part of the user address space.");		case WSAENOTCONN : 			throw Error("The socket is not connected.");		case WSAEINTR : 			throw Error("The (blocking) 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 WSAENETRESET : 			throw ConnectException("The connection has been broken due to the keep-alive activity detecting a failure while the operation was in progress.");		case WSAENOTSOCK : 			throw Error("The descriptor is not a socket.");		case WSAEOPNOTSUPP : 			throw Error("MSG_OOB was specified, but the socket is not stream-style such as type SOCK_STREAM, out-of-band data is not supported in the communication domain associated with this socket, or the socket is unidirectional and supports only send operations.");		case WSAESHUTDOWN : 			throw Error("The socket has been shut down; it is not possible to recv on a socket after shutdown has been invoked with how set to SD_RECEIVE or SD_BOTH.");		case WSAEWOULDBLOCK : 			throw NonBlockingIOException("The socket is marked as nonblocking and the receive operation would block.");		case WSAEMSGSIZE : 			throw Error("The message was too large to fit into the specified buffer and was truncated.");		case WSAEINVAL : 			throw Error("The socket has not been bound with bind, or an unknown flag was specified, or MSG_OOB was specified for a socket with SO_OOBINLINE enabled or (for byte stream sockets only) len was zero or negative.");		case WSAECONNABORTED : 			throw ConnectException("The virtual circuit was terminated due to a time-out or other failure. The application should close the socket as it is no longer usable.");		case WSAETIMEDOUT : 			throw ConnectException("The connection has been dropped because of a network failure or because the peer system failed to respond.");		case WSAECONNRESET : 			throw ConnectException("The virtual circuit was reset by the remote side executing a 'hard' or 'abortive' close. The application should close the socket as it is no longer usable. On a UDP datagram socket this error would indicate that a previous send operation resulted in an ICMP 'Port Unreachable' message.");		default :			throw UnknownError("recv()");		}//end of switch#endif	} 	else if ( nrecv == 0 )	{		throw ConnectException("connect closed.");	}	return nrecv;		__END_CATCH}/////////////////////////////////////////////////////////////////////// exception version of recvfrom()/////////////////////////////////////////////////////////////////////uint SocketAPI::recvfrom_ex ( SOCKET s , void * buf , int len , uint flags , struct sockaddr * from , uint * fromlen )    throw ( NonBlockingIOException , ConnectException , Error ){	__BEGIN_TRY#if __LINUX__	int nReceived = recvfrom(s,buf,len,flags,from,fromlen);	//SOCKADDR_IN* sa = (SOCKADDR_IN*)from;	//cout << "recvfrom_ex : " << inet_ntoa(sa->sin_addr) << ":" << sa->sin_port << endl;#elif __WINDOWS__	int nReceived = recvfrom(s,(char*)buf,len,flags,from,(int*)fromlen);#endif	if ( nReceived == SOCKET_ERROR ) {#if __LINUX__		switch ( errno ) {		case EWOULDBLOCK : 			//throw NonBlockingIOException();			// by sigi. 2002.5.17			return 0;		case ECONNRESET :		case EPIPE :			throw ConnectException(strerror(errno));		case EBADF : 		case ENOTCONN : 		case ENOTSOCK : 		case EINTR : 		case EFAULT : 			throw Error(strerror(errno));		default : 			throw UnknownError(strerror(errno),errno);		}//end of switch#elif __WINDOWS__#endif	}	return nReceived;	__END_CATCH}///////////////////////////////////////////////////////////////////////// void SocketAPI::closesocket_ex ( SOCKET s )//      throw ( Error )//// exception version of closesocket()//// Parameters//     s - socket descriptor//// Return//     none//// Exceptions//     Error///////////////////////////////////////////////////////////////////////void SocketAPI::closesocket_ex ( SOCKET s )     throw ( FileNotOpenedException, Error ){	__BEGIN_TRY#if __LINUX__	// using close_ex()	FileAPI::close_ex(s);#elif __WINDOWS__	if ( closesocket(s) == SOCKET_ERROR ) {		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 WSAENOTSOCK : 			throw FileNotOpenedException("The descriptor is not a socket.");		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 WSAEINTR : 			throw Error("The (blocking) Windows Socket 1.1 call was canceled through WSACancelBlockingCall.");		case WSAEWOULDBLOCK : 			throw Error("The socket is marked as nonblocking and SO_LINGER is set to a nonzero time-out value.");		default : 			throw UnknownError("closesocket()");		}//end of switch	}#endif		__END_CATCH}///////////////////////////////////////////////////////////////////////// void SocketAPI::ioctlsocket_ex ( SOCKET s , long cmd , ulong * argp )//      throw ( Error )//// exception version of ioctlsocket()///////////////////////////////////////////////////////////////////////void SocketAPI::ioctlsocket_ex ( SOCKET s , long cmd , ulong * argp )     throw ( Error ){	__BEGIN_TRY#if __LINUX__	throw UnsupportedError();#elif __WINDOWS__	if ( ioctlsocket(s,cmd,argp) == SOCKET_ERROR ) {		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 WSAEINPROGRESS : 			throw Error("A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function. ");		case WSAENOTSOCK : 			throw Error("The descriptor s is not a socket. ");		case WSAEFAULT : 			throw Error("The argp parameter is not a valid part of the user address space. ");		default :			throw UnknownError("ioctlsocket()");		}	}#endif		__END_CATCH} ////////////////////////////////////////////////////////////////////////// bool SocketAPI::getsocketnonblocking_ex ( SOCKET s ) //      throw ( Error );//// check if this socket is nonblocking mode//// Parameters//     s - socket descriptor//// Return//     true if nonblocking, false if blocking//// Exceptions//     Error////////////////////////////////////////////////////////////////////////bool SocketAPI::getsocketnonblocking_ex ( SOCKET s )     throw ( Error ){	__BEGIN_TRY#if __LINUX__	return FileAPI::getfilenonblocking_ex(s);#elif __WINDOWS__	throw UnsupportedError();#endif		__END_CATCH}////////////////////////////////////////////////////////////////////////// void SocketAPI::setsocketnonblocking_ex ( SOCKET s , bool on ) //      throw ( Error );//// make this socket blocking/nonblocking//// Parameters//     s  - socket descriptor//     on - true if nonblocking, false if blocking//// Return//     none//// Exceptions//     Error////////////////////////////////////////////////////////////////////////void SocketAPI::setsocketnonblocking_ex ( SOCKET s , bool on )     throw ( Error ){	__BEGIN_TRY#if __LINUX__	FileAPI::setfilenonblocking_ex(s,on);#elif __WINDOWS__	ulong argp = ( on == true ) ? 1 : 0;	ioctlsocket_ex(s,FIONBIO,&argp);#endif		__END_CATCH}////////////////////////////////////////////////////////////////////////// uint SocketAPI::availablesocket_ex ( SOCKET s )//      throw ( Error )//// get amount of data in socket input buffer//// Parameters//    s - socket descriptor//// Return//    amount of data in socket input buffer//// Exceptions//    Error////////////////////////////////////////////////////////////////////////uint SocketAPI::availablesocket_ex ( SOCKET s )     throw ( Error ){	__BEGIN_TRY#if __LINUX__	return availablefile_ex(s);#elif __WINDOWS__	ulong argp = 0;	ioctlsocket_ex(s,FIONREAD,&argp);	return argp;#endif		__END_CATCH}////////////////////////////////////////////////////////////////////////// void SocketAPI::shutdown_ex ( SOCKET s , uint how )// 	    throw ( Error )//// shutdown all or part of connection of socket//// Parameters//     s   - socket descriptor//     how - how to close ( all , send , receive )//// Return//     none//// Exceptions//     Error////////////////////////////////////////////////////////////////////////void SocketAPI::shutdown_ex ( SOCKET s , uint how )	 throw ( Error ){	__BEGIN_TRY	if ( shutdown(s,how) < 0 ) {#if __LINUX__		switch ( errno ) {		case EBADF : 			throw Error("s is not a valid descriptor.");		case ENOTSOCK : 			throw Error("s is a file, not a socket.");		case ENOTCONN : 			throw Error("The specified socket is not connected.");		default : 			throw UnknownError(strerror(errno),errno);		}#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 WSAEINVAL : 			throw Error("The how parameter is not valid, or is not consistent with the socket type. For example, SD_SEND is used with a UNI_RECV socket type.");		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 WSAENOTCONN : 			throw Error("The socket is not connected (connection-oriented sockets only).");		case WSAENOTSOCK : 			throw Error("The descriptor is not a socket.");		default :			throw UnknownError("shutdown()");		}#endif	}		__END_CATCH}////////////////////////////////////////////////////////////////////////// int SocketAPI::select_ex ( int maxfdp1 , fd_set * readset , fd_set * writeset , fd_set * exceptset , struct timeval * timeout )//		throw ( Error )//// system call for I/O multiplexing//// Parameters//     maxfdp1   - 抛胶飘且 颇老 叼胶农赋磐吝 啊厘 奴 蔼 + 1//     readset   - 涝仿捞 甸绢吭绰瘤 抛胶飘且 颇老 叼胶农赋磐狼 笼钦//     writeset  - 免仿阑 且 荐 乐绰瘤 抛胶飘且 颇老 叼胶农赋磐狼 笼钦//     exceptset - OOB 单捞鸥啊 甸绢吭绰瘤 抛胶飘且 颇老 叼胶农赋磐狼 笼钦//     timeout   - 倔付唱 扁促副 巴牢啊? //// Return//     positive count of ready descriptors//// Exceptions//     InterruptedException//     TimeoutException//     Error////////////////////////////////////////////////////////////////////////int SocketAPI::select_ex ( int maxfdp1 , fd_set * readset , fd_set * writeset , fd_set * exceptset , struct timeval * timeout )	throw ( TimeoutException , InterruptedException , Error ){	__BEGIN_TRY#if __LINUX__	int result;	try {		result = select( maxfdp1 , readset , writeset , exceptset , timeout );		if ( result == 0 )			// by sigi. 2002.5.17			return 0;			//throw TimeoutException();		/*	    // 林籍贸府 by sigi. 2002.5.17		if ( result < 0 ) {			switch ( errno ) {			case EINTR : 				throw InterruptedException("A non blocked signal was caught.");			case EBADF : 				throw Error("An invalid file descriptor was given in one of the sets.");			case EINVAL : 				throw Error("parameter maxfdp1 is negative.");			case ENOMEM : 				throw Error("select was unable to allocate memory for internal tables.");			default : 				throw UnknownError(strerror(errno),errno);			}		}		*/	} catch ( Throwable & t ) {		// 绢恫 俊矾啊 唱电 公矫茄促.//		cout << "伎泛飘俊辑 捞惑茄 俊矾啊 抄寸.." << endl;//		throw TimeoutException();	}	return result;#elif __WINDOWS__	throw UnsupportedError();#endif		__END_CATCH}

⌨️ 快捷键说明

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