socket.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_SOCKET_H_
  11. #define RTC_BASE_SOCKET_H_
  12. #include <errno.h>
  13. #if defined(WEBRTC_POSIX)
  14. #include <arpa/inet.h>
  15. #include <netinet/in.h>
  16. #include <sys/socket.h>
  17. #include <sys/types.h>
  18. #define SOCKET_EACCES EACCES
  19. #endif
  20. #if defined(WEBRTC_WIN)
  21. #include "rtc_base/win32.h"
  22. #endif
  23. #include "rtc_base/constructor_magic.h"
  24. #include "rtc_base/socket_address.h"
  25. // Rather than converting errors into a private namespace,
  26. // Reuse the POSIX socket api errors. Note this depends on
  27. // Win32 compatibility.
  28. #if defined(WEBRTC_WIN)
  29. #undef EWOULDBLOCK // Remove errno.h's definition for each macro below.
  30. #define EWOULDBLOCK WSAEWOULDBLOCK
  31. #undef EINPROGRESS
  32. #define EINPROGRESS WSAEINPROGRESS
  33. #undef EALREADY
  34. #define EALREADY WSAEALREADY
  35. #undef EMSGSIZE
  36. #define EMSGSIZE WSAEMSGSIZE
  37. #undef EADDRINUSE
  38. #define EADDRINUSE WSAEADDRINUSE
  39. #undef EADDRNOTAVAIL
  40. #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
  41. #undef ENETDOWN
  42. #define ENETDOWN WSAENETDOWN
  43. #undef ECONNABORTED
  44. #define ECONNABORTED WSAECONNABORTED
  45. #undef ENOBUFS
  46. #define ENOBUFS WSAENOBUFS
  47. #undef EISCONN
  48. #define EISCONN WSAEISCONN
  49. #undef ENOTCONN
  50. #define ENOTCONN WSAENOTCONN
  51. #undef ECONNREFUSED
  52. #define ECONNREFUSED WSAECONNREFUSED
  53. #undef EHOSTUNREACH
  54. #define EHOSTUNREACH WSAEHOSTUNREACH
  55. #define SOCKET_EACCES WSAEACCES
  56. #endif // WEBRTC_WIN
  57. #if defined(WEBRTC_POSIX)
  58. #define INVALID_SOCKET (-1)
  59. #define SOCKET_ERROR (-1)
  60. #define closesocket(s) close(s)
  61. #endif // WEBRTC_POSIX
  62. namespace rtc {
  63. inline bool IsBlockingError(int e) {
  64. return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS);
  65. }
  66. // General interface for the socket implementations of various networks. The
  67. // methods match those of normal UNIX sockets very closely.
  68. class Socket {
  69. public:
  70. virtual ~Socket() {}
  71. // Returns the address to which the socket is bound. If the socket is not
  72. // bound, then the any-address is returned.
  73. virtual SocketAddress GetLocalAddress() const = 0;
  74. // Returns the address to which the socket is connected. If the socket is
  75. // not connected, then the any-address is returned.
  76. virtual SocketAddress GetRemoteAddress() const = 0;
  77. virtual int Bind(const SocketAddress& addr) = 0;
  78. virtual int Connect(const SocketAddress& addr) = 0;
  79. virtual int Send(const void* pv, size_t cb) = 0;
  80. virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) = 0;
  81. // |timestamp| is in units of microseconds.
  82. virtual int Recv(void* pv, size_t cb, int64_t* timestamp) = 0;
  83. virtual int RecvFrom(void* pv,
  84. size_t cb,
  85. SocketAddress* paddr,
  86. int64_t* timestamp) = 0;
  87. virtual int Listen(int backlog) = 0;
  88. virtual Socket* Accept(SocketAddress* paddr) = 0;
  89. virtual int Close() = 0;
  90. virtual int GetError() const = 0;
  91. virtual void SetError(int error) = 0;
  92. inline bool IsBlocking() const { return IsBlockingError(GetError()); }
  93. enum ConnState { CS_CLOSED, CS_CONNECTING, CS_CONNECTED };
  94. virtual ConnState GetState() const = 0;
  95. enum Option {
  96. OPT_DONTFRAGMENT,
  97. OPT_RCVBUF, // receive buffer size
  98. OPT_SNDBUF, // send buffer size
  99. OPT_NODELAY, // whether Nagle algorithm is enabled
  100. OPT_IPV6_V6ONLY, // Whether the socket is IPv6 only.
  101. OPT_DSCP, // DSCP code
  102. OPT_RTP_SENDTIME_EXTN_ID, // This is a non-traditional socket option param.
  103. // This is specific to libjingle and will be used
  104. // if SendTime option is needed at socket level.
  105. };
  106. virtual int GetOption(Option opt, int* value) = 0;
  107. virtual int SetOption(Option opt, int value) = 0;
  108. protected:
  109. Socket() {}
  110. private:
  111. RTC_DISALLOW_COPY_AND_ASSIGN(Socket);
  112. };
  113. } // namespace rtc
  114. #endif // RTC_BASE_SOCKET_H_