PageRenderTime 45ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/ClassMaster2014/barahon/Transcend_0.3_UnixSource/minorGems/network/win32/SocketServerWin32.cpp

https://gitlab.com/garheade/linux_camp
C++ | 174 lines | 81 code | 54 blank | 39 comment | 19 complexity | 681d53bc7dc8a708e8e08c0be82742aa MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * Modification History
  3. *
  4. * 2001-January-28 Jason Rohrer
  5. * Created.
  6. *
  7. * 2001-January-29 Jason Rohrer
  8. * Fixed an endian bug that was messing up the port numbers.
  9. *
  10. * 2002-April-6 Jason Rohrer
  11. * Changed to implement the new timeout-on-accept interface,
  12. * though did no actually implement timeouts.
  13. *
  14. * 2002-September-8 Jason Rohrer
  15. * Added a check for a potentially NULL argument.
  16. *
  17. * 2004-February-23 Jason Rohrer
  18. * Added timeout code for accepting connections.
  19. * Removed use of shutdown on listening socket.
  20. */
  21. #include "minorGems/network/SocketServer.h"
  22. #include <Winsock.h>
  23. #include <stdio.h>
  24. union sock {
  25. struct sockaddr s;
  26. struct sockaddr_in i;
  27. } sock;
  28. SocketServer::SocketServer( int inPort, int inMaxQueuedConnections ) {
  29. int error = 0;
  30. if( !Socket::isFrameworkInitialized() ) {
  31. // try to init the framework
  32. int error = Socket::initSocketFramework();
  33. if( error == -1 ) {
  34. printf( "initializing network socket framework failed\n" );
  35. exit( 1 );
  36. }
  37. }
  38. // create the socket
  39. int sockID = socket( AF_INET, SOCK_STREAM, 0 );
  40. if( sockID == INVALID_SOCKET ) {
  41. printf( "Creating socket failed.\n" );
  42. exit( 1 );
  43. }
  44. // store socket id in native object pointer
  45. int *idStorage = new int[1];
  46. idStorage[0] = sockID;
  47. mNativeObjectPointer = (void *)idStorage;
  48. // bind socket to the port
  49. union sock sockAddress;
  50. sockAddress.i.sin_family = AF_INET;
  51. sockAddress.i.sin_port = htons( inPort );
  52. sockAddress.i.sin_addr.s_addr = INADDR_ANY;
  53. error = bind( sockID, &(sockAddress.s), sizeof( struct sockaddr ) );
  54. if( error == -1 ) {
  55. printf( "Network socket bind to port %d failed\n", inPort );
  56. exit( 1 );
  57. }
  58. // start listening for connections
  59. error = listen( sockID, inMaxQueuedConnections );
  60. if( error == -1 ) {
  61. printf( "Listening for network socket connections failed.\n" );
  62. exit(1);
  63. }
  64. }
  65. SocketServer::~SocketServer() {
  66. int *socketIDptr = (int *)( mNativeObjectPointer );
  67. int socketID = socketIDptr[0];
  68. closesocket( socketID );
  69. delete [] socketIDptr;
  70. }
  71. Socket *SocketServer::acceptConnection( long inTimeoutInMilliseconds,
  72. char *outTimedOut ) {
  73. if( outTimedOut != NULL ) {
  74. *outTimedOut = false;
  75. }
  76. // printf( "Waiting for a connection.\n" );
  77. // extract socket id from native object pointer
  78. int *socketIDptr = (int *)( mNativeObjectPointer );
  79. int socketID = socketIDptr[0];
  80. // same timeout code as in linux version
  81. if( inTimeoutInMilliseconds != -1 ) {
  82. // if we have a timeout specified, select before accepting
  83. // this found in the Linux man page for select,
  84. // but idea (which originally used poll) was found
  85. // in the Unix Socket FAQ
  86. fd_set rfds;
  87. struct timeval tv;
  88. int retval;
  89. // insert our socket descriptor into this set
  90. FD_ZERO( &rfds );
  91. FD_SET( socketID, &rfds );
  92. // convert our timeout into the structure's format
  93. tv.tv_sec = inTimeoutInMilliseconds / 1000;
  94. tv.tv_usec = ( inTimeoutInMilliseconds % 1000 ) * 1000 ;
  95. retval = select( socketID + 1, &rfds, NULL, NULL, &tv );
  96. if( retval == 0 ) {
  97. // timeout
  98. if( outTimedOut != NULL ) {
  99. *outTimedOut = true;
  100. }
  101. return NULL;
  102. }
  103. }
  104. //int addressLength;
  105. //union sock acceptedAddress;
  106. struct sockaddr acceptedAddress;
  107. //int addressLength = sizeof( struct sockaddr );
  108. int acceptedID = accept( socketID,
  109. &( acceptedAddress ), NULL );
  110. if( acceptedID == -1 ) {
  111. printf( "Failed to accept a network connection.\n" );
  112. return NULL;
  113. }
  114. Socket *acceptedSocket = new Socket();
  115. int *idStorage = new int[1];
  116. idStorage[0] = acceptedID;
  117. acceptedSocket->mNativeObjectPointer = (void *)idStorage;
  118. //printf( "Connection received.\n" );
  119. return acceptedSocket;
  120. }