Socket.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:3k
源码类别:

模拟服务器

开发平台:

C/C++

  1. #include "Socket.h"
  2. /*
  3.  * namespace OnlineGameLib::Win32
  4.  */
  5. namespace OnlineGameLib {
  6. namespace Win32 {
  7. CSocket::CSocket()
  8. : m_socket( INVALID_SOCKET )
  9. {
  10. }
  11. CSocket::CSocket( SOCKET theSocket )
  12. : m_socket( theSocket )
  13. {
  14.    if ( INVALID_SOCKET == m_socket )
  15.    {
  16.       throw Exception( _T("CSocket::CSocket()"),  WSAENOTSOCK );
  17.    }
  18. }
  19.       
  20. CSocket::~CSocket()
  21. {
  22. if ( INVALID_SOCKET != m_socket )
  23. {
  24. try
  25. {
  26. AbortiveClose();
  27. }
  28. catch(...)
  29. {
  30. }
  31. }
  32. }
  33. void CSocket::Attach( SOCKET theSocket )
  34. {
  35. AbortiveClose();
  36. m_socket = theSocket;
  37. }
  38. SOCKET CSocket::Detatch()
  39. {
  40. SOCKET theSocket = m_socket;
  41. m_socket = INVALID_SOCKET;
  42. return theSocket;
  43. }
  44. void CSocket::Close()
  45. {
  46. if ( 0 != ::closesocket( m_socket ) )
  47. {
  48. throw Exception( _T("CSocket::Close()"), ::WSAGetLastError() );
  49. }
  50. }
  51. void CSocket::AbortiveClose()
  52. {
  53. LINGER lingerStruct;
  54. lingerStruct.l_onoff = 1;
  55. lingerStruct.l_linger = 0;
  56. if ( SOCKET_ERROR == ::setsockopt( m_socket, SOL_SOCKET, SO_LINGER, (char *)&lingerStruct, sizeof(lingerStruct) ) )
  57. {
  58. throw Exception( _T("CSocket::AbortiveClose()"), ::WSAGetLastError() );
  59. }
  60. Close();
  61. }
  62. void CSocket::Shutdown( int how )
  63. {
  64. if ( 0 != ::shutdown( m_socket, how ) )
  65. {
  66. throw Exception( _T("CSocket::Shutdown()"), ::WSAGetLastError() );
  67. }
  68. }
  69. void CSocket::Listen( int backlog )
  70. {
  71. if ( SOCKET_ERROR == ::listen( m_socket, backlog ) )
  72. {
  73. throw Exception( _T("CSocket::Listen()"), ::WSAGetLastError() );
  74. }
  75. }
  76. void CSocket::Bind( const SOCKADDR_IN &address )
  77. {
  78. if ( SOCKET_ERROR == ::bind( m_socket, reinterpret_cast<struct sockaddr *>( const_cast<SOCKADDR_IN *>( &address ) ), sizeof(SOCKADDR_IN) ) )
  79. {
  80. throw Exception( _T("CSocket::Bind()"), ::WSAGetLastError() );
  81. }
  82. }
  83. void CSocket::Bind( const struct sockaddr &address, size_t addressLength )
  84. {
  85. /*
  86.  * Loss of precision (arg. no. 3) (unsigned int to int)
  87.  */
  88. if ( SOCKET_ERROR == ::bind( m_socket, const_cast<struct sockaddr *>( &address ), addressLength ) )
  89. {
  90. throw Exception( _T("CSocket::Bind()"), ::WSAGetLastError() );
  91. }
  92. }
  93. /*
  94.  * CSocket::InternetAddress
  95.  */
  96. CSocket::InternetAddress::InternetAddress( unsigned long address, unsigned short port )
  97. {
  98. sin_family = AF_INET;
  99. sin_port = htons( port );
  100. sin_addr.s_addr = htonl( address ); 
  101. /*
  102.  * member 'sockaddr_in::sin_zero not initialised
  103.  */
  104. }
  105. /*
  106.  * CSocket::Exception
  107.  */
  108. CSocket::Exception::Exception( const _tstring &where, DWORD error )
  109. : CWin32Exception(where, error)
  110. {
  111. }
  112. } // End of namespace OnlineGameLib
  113. } // End of namespace Win32