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

📄 socketapi.cpp

📁 天之炼狱1服务器端源文件游戏服务端不完整
💻 CPP
📖 第 1 页 / 共 3 页
字号:
			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 listen function was not invoked prior to accept.");		case WSAEMFILE : 			throw Error("The queue is nonempty upon entry to accept and there are no descriptors 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 a type that supports connection-oriented service.");		case WSAEWOULDBLOCK : 			throw NonBlockingIOException();		default :			throw UnknownError("accept()");		}//end of switch#endif	} else {/*		struct timeval tm;		struct timeval tm2;//		int time = getsockopt( client, SOL_SOCKET, SO_SNDTIMEO, &tm, &socklen );//		int time2 = getsockopt( client, SOL_SOCKET, SO_RCVTIMEO, &tm2, &socklen2 );//		cout << "Socket Option Time Out Sec Value : " << tm.tv_sec << endl;//		cout << "Socket Option Time Out Usec Value : " << tm.tv_usec << endl;		// Send Time out//		tm.tv_sec = 0;//		tm.tv_usec = 20;//		socklen_t socklen = sizeof( tm );		// Recv Time out		tm2.tv_sec = 0;		tm2.tv_usec = 10000;		socklen_t socklen2 = sizeof( tm2 );		socklen2 = sizeof( tm2 );//		setsockopt( client, SOL_SOCKET, SO_SNDTIMEO, &tm, socklen );		setsockopt( client, SOL_SOCKET, SO_RCVTIMEO, &tm2, socklen2 );*/	}	return client;	__END_CATCH}////////////////////////////////////////////////////////////////////////// void SocketAPI::getsockopt_ex ( SOCKET s , int level , int optname , void * optval , uint * optlen )//      throw ( Error );//// exception version of getsockopt()//// Parameters//     s       - socket descriptor//     level   - socket option level ( SOL_SOCKET , ... )//     optname - socket option name ( SO_REUSEADDR , SO_LINGER , ... )//     optval  - pointer to contain option value//     optlen  - length of optval//// Return//     none//// Exceptions//     Error////////////////////////////////////////////////////////////////////////void SocketAPI::getsockopt_ex ( SOCKET s , int level , int optname , void * optval , uint * optlen )     throw ( Error ){	__BEGIN_TRY#if __LINUX__	if ( getsockopt( s , level , optname , optval , optlen ) == SOCKET_ERROR ) {		switch ( errno ) {		case EBADF : 			throw Error("Bad descriptor.");		case ENOTSOCK : 			throw Error("Not a socket.");		case ENOPROTOOPT : 			throw Error("The option is unknown at the level indicated.");		case EFAULT : 			throw Error("The address pointed to by optval is not in a valid part of the process address space. For getsockopt, this error may also be returned if optlen is not in a valid part of the process address space.");		default :			throw UnknownError(strerror(errno),errno);		}//end of switch	}#elif __WINDOWS__	if ( getsockopt( s , level , optname , (char*)optval , (int*)optlen ) == 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 WSAEFAULT : 			throw Error("One of the optval or the optlen parameters is not a valid part of the user address space, or the optlen parameter is too small.");		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 level parameter is unknown or invalid.");		case WSAENOPROTOOPT : 			throw Error("The option is unknown or unsupported by the indicated protocol family.");		case WSAENOTSOCK : 			throw Error("The descriptor is not a socket.");		default : 			throw UnknownError("getsockopt()");		}//end of switch	}#endif		__END_CATCH}uint SocketAPI::getsockopt_ex2 ( SOCKET s , int level , int optname , void * optval , uint * optlen )     throw ( Error ){	__BEGIN_TRY#if __LINUX__	if ( getsockopt( s , level , optname , optval , optlen ) == SOCKET_ERROR ) {		switch ( errno ) {		case EBADF : 			return 1;		case ENOTSOCK : 			return 2;		case ENOPROTOOPT : 			return 3;		case EFAULT : 			return 4;		default :			return 5;		}//end of switch	}	return 0;#endif	__END_CATCH}////////////////////////////////////////////////////////////////////////// void SocketAPI::setsockopt_ex ( SOCKET s , int level , int optname , const void * optval , uint optlen )//      throw ( Error );//// exception version of setsockopt()//// Parameters//     s       - socket descriptor//     level   - socket option level ( SOL_SOCKET , ... )//     optname - socket option name ( SO_REUSEADDR , SO_LINGER , ... )//     optval  - pointer to contain option value//     optlen  - length of optval//// Return//     none//// Exceptions//     Error////////////////////////////////////////////////////////////////////////void SocketAPI::setsockopt_ex ( SOCKET s , int level , int optname , const void * optval , uint optlen )     throw ( Error ){	__BEGIN_TRY#if __LINUX__	if ( setsockopt( s , level , optname , optval , optlen ) == SOCKET_ERROR ) {		switch ( errno ) {			case EBADF : 				throw Error("Bad descriptor.");			case ENOTSOCK : 				throw Error("Not a socket.");			case ENOPROTOOPT : 				throw Error("The option is unknown at the level indicated.");			case EFAULT : 				throw Error("The address pointed to by optval is not in a valid part of the process address space. For getsockopt, this error may also be returned if optlen is not in a valid part of the process address space.");			default :				throw UnknownError(strerror(errno),errno);		}//end of switch	}#elif __WINDOWS__	if ( setsockopt( s , level , optname , (char*)optval , optlen ) == 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 WSAEFAULT : 			throw Error("optval is not in a valid part of the process address space or optlen parameter is too small.");		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("level is not valid, or the information in optval is not valid.");		case WSAENETRESET : 			throw Error("Connection has timed out when SO_KEEPALIVE is set.");		case WSAENOPROTOOPT : 			throw Error("The option is unknown or unsupported for the specified provider or socket (see SO_GROUP_PRIORITY limitations).");		case WSAENOTCONN : 			throw Error("Connection has been reset when SO_KEEPALIVE is set.");		case WSAENOTSOCK : 			throw Error("The descriptor is not a socket.");		default :			throw UnknownError("setsockopt()");		}//end of switch	}#endif		__END_CATCH}////////////////////////////////////////////////////////////////////////// uint SocketAPI::send_ex ( SOCKET s , const void * buf , uint len , uint flags )//      throw ( NonBlockingIOException , ConnectException , Error )// // exception version of send()// // Parameters //     s     - socket descriptor//     buf   - input buffer//     len   - input data length//     flags - send flag (MSG_OOB,MSG_DONTROUTE)// // Return //     length of bytes sent// // Exceptions //     NonBlockingIOException//     ConnectException//     Error // //////////////////////////////////////////////////////////////////////uint SocketAPI::send_ex ( SOCKET s , const void * buf , uint len , uint flags )     throw ( NonBlockingIOException ,             ConnectException ,             Error,			 ProtocolException ){	__BEGIN_TRY	int nSent;	try {#if __LINUX__	nSent = send(s,buf,len,flags);#elif __WINDOWS__	nSent = send(s,(const char *)buf,len,flags);#endif	if ( nSent == 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 ENOTSOCK : 		case EFAULT : 		case EMSGSIZE : 		case ENOBUFS : 			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 WSAEACCES : 			throw Error("The requested address is a broadcast address, but the appropriate flag was not set. Call setsockopt with the SO_BROADCAST parameter to allow the use of the broadcast address.");		case WSAEINTR : 			throw Error("A blocking Windows Sockets 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 WSAEFAULT : 			throw Error("The buf parameter is not completely contained in a valid part of the user address space.");		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 WSAENOBUFS : 			throw Error("No buffer space is available.");		case WSAENOTCONN : 			throw Error("The socket is not connected.");		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 receive operations.");		case WSAESHUTDOWN : 			throw ConnectException("The socket has been shut down; it is not possible to send on a socket after shutdown has been invoked with how set to SD_SEND or SD_BOTH.");		case WSAEWOULDBLOCK : 			throw NonBlockingIOException("The socket is marked as nonblocking and the requested operation would block.");		case WSAEMSGSIZE : 			throw Error("The socket is message oriented, and the message is larger than the maximum supported by the underlying transport.");		case WSAEHOSTUNREACH : 			throw ConnectException("The remote host cannot be reached from this host at this time.");		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.");		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 WSAECONNRESET : 			throw ConnectException("The virtual circuit was reset by the remote side executing a 'hard' or 'abortive' close. For UPD sockets, the remote host was unable to deliver a previously sent UDP datagram and responded with a 'Port Unreachable' ICMP packet. 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 system on the other end went down without notice.");		default :			throw UnknownError("send()");		}//end of switch#endif	} 	else if ( nSent == 0 )	{		throw ConnectException("connect closed.");	}	} catch ( Throwable & t ) {		cout << "SocketAPI::send_ex Exception Check!" << endl;		cout << t.toString() << endl;		throw InvalidProtocolException("揪官 穿啊 谗扁畴");	}	return nSent;		__END_CATCH}//////////////////////////////////////////////////////////////////////// exception version of sendto()//////////////////////////////////////////////////////////////////////uint SocketAPI::sendto_ex ( SOCKET s , const void * buf , int len , unsigned int flags , const struct sockaddr * to , int tolen )    throw ( NonBlockingIOException , ConnectException , Error ){	__BEGIN_TRY#if __LINUX__	int nSent = sendto(s,buf,len,flags,to,tolen);#elif __WINDOWS__	int nSent = sendto(s,(const char *)buf,len,flags,to,tolen);#endif	if ( nSent == 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 ENOTSOCK : 		case EFAULT : 		case EMSGSIZE : 		case ENOBUFS : 			throw Error(strerror(errno));		default : 			//throw UnknownError(strerror(errno),errno);			throw ConnectException(strerror(errno));		}	#elif __WINDOWS__#endif	}	return nSent;	__END_CATCH}////////////////////////////////////////////////////////////////////////// uint SocketAPI::recv_ex ( SOCKET s , void * buf , uint len , uint flags )//      throw ( NonBlockingIOException , ConnectException , Error )//// exception version of recv()//// Parameters //     s     - socket descriptor//     buf   - input buffer//     len   - input data length//     flags - send flag (MSG_OOB,MSG_DONTROUTE)// // Return //     length of bytes received// // Exceptions //     NonBlockingIOException//     ConnectException//     Error ////////////////////////////////////////////////////////////////////////uint SocketAPI::recv_ex ( SOCKET s , void * buf , uint len , uint flags )     throw ( NonBlockingIOException ,             ConnectException ,             Error ){	__BEGIN_TRY#if __LINUX__	int nrecv = recv(s,buf,len,flags);#elif __WINDOWS__	int nrecv = recv(s,(char*)buf,len,flags);#endif	if ( nrecv == SOCKET_ERROR ) {#if __LINUX__		switch ( errno ) {		case EWOULDBLOCK : 			// by sigi. 2002.5.17			return 0;			//throw NonBlockingIOException();		case ECONNRESET :		case EPIPE :			throw ConnectException(strerror(errno));

⌨️ 快捷键说明

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