PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/src/SFML/Network/Win32/SocketImpl.cpp

http://github.com/LaurentGomila/SFML
C++ | 112 lines | 58 code | 18 blank | 36 comment | 1 complexity | 3c9eee751f7269424290cd8346ffcb37 MD5 | raw file
  1. ////////////////////////////////////////////////////////////
  2. //
  3. // SFML - Simple and Fast Multimedia Library
  4. // Copyright (C) 2007-2019 Laurent Gomila (laurent@sfml-dev.org)
  5. //
  6. // This software is provided 'as-is', without any express or implied warranty.
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it freely,
  11. // subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented;
  14. // you must not claim that you wrote the original software.
  15. // If you use this software in a product, an acknowledgment
  16. // in the product documentation would be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such,
  19. // and must not be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source distribution.
  22. //
  23. ////////////////////////////////////////////////////////////
  24. ////////////////////////////////////////////////////////////
  25. // Headers
  26. ////////////////////////////////////////////////////////////
  27. #include <SFML/Network/Win32/SocketImpl.hpp>
  28. #include <cstring>
  29. namespace sf
  30. {
  31. namespace priv
  32. {
  33. ////////////////////////////////////////////////////////////
  34. sockaddr_in SocketImpl::createAddress(Uint32 address, unsigned short port)
  35. {
  36. sockaddr_in addr;
  37. std::memset(&addr, 0, sizeof(addr));
  38. addr.sin_addr.s_addr = htonl(address);
  39. addr.sin_family = AF_INET;
  40. addr.sin_port = htons(port);
  41. return addr;
  42. }
  43. ////////////////////////////////////////////////////////////
  44. SocketHandle SocketImpl::invalidSocket()
  45. {
  46. return INVALID_SOCKET;
  47. }
  48. ////////////////////////////////////////////////////////////
  49. void SocketImpl::close(SocketHandle sock)
  50. {
  51. closesocket(sock);
  52. }
  53. ////////////////////////////////////////////////////////////
  54. void SocketImpl::setBlocking(SocketHandle sock, bool block)
  55. {
  56. u_long blocking = block ? 0 : 1;
  57. ioctlsocket(sock, FIONBIO, &blocking);
  58. }
  59. ////////////////////////////////////////////////////////////
  60. Socket::Status SocketImpl::getErrorStatus()
  61. {
  62. switch (WSAGetLastError())
  63. {
  64. case WSAEWOULDBLOCK: return Socket::NotReady;
  65. case WSAEALREADY: return Socket::NotReady;
  66. case WSAECONNABORTED: return Socket::Disconnected;
  67. case WSAECONNRESET: return Socket::Disconnected;
  68. case WSAETIMEDOUT: return Socket::Disconnected;
  69. case WSAENETRESET: return Socket::Disconnected;
  70. case WSAENOTCONN: return Socket::Disconnected;
  71. case WSAEISCONN: return Socket::Done; // when connecting a non-blocking socket
  72. default: return Socket::Error;
  73. }
  74. }
  75. ////////////////////////////////////////////////////////////
  76. // Windows needs some initialization and cleanup to get
  77. // sockets working properly... so let's create a class that will
  78. // do it automatically
  79. ////////////////////////////////////////////////////////////
  80. struct SocketInitializer
  81. {
  82. SocketInitializer()
  83. {
  84. WSADATA init;
  85. WSAStartup(MAKEWORD(2, 2), &init);
  86. }
  87. ~SocketInitializer()
  88. {
  89. WSACleanup();
  90. }
  91. };
  92. SocketInitializer globalInitializer;
  93. } // namespace priv
  94. } // namespace sf