You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_ASYNCPACKETSOCKET_H_
  28. #define TALK_BASE_ASYNCPACKETSOCKET_H_
  29. #include "talk/base/sigslot.h"
  30. #include "talk/base/socket.h"
  31. namespace talk_base {
  32. // Provides the ability to receive packets asynchronously. Sends are not
  33. // buffered since it is acceptable to drop packets under high load.
  34. class AsyncPacketSocket : public sigslot::has_slots<> {
  35. public:
  36. enum State {
  37. STATE_CLOSED,
  38. STATE_BINDING,
  39. STATE_BOUND,
  40. STATE_CONNECTING,
  41. STATE_CONNECTED
  42. };
  43. AsyncPacketSocket() { }
  44. virtual ~AsyncPacketSocket() { }
  45. // Returns current local address. Address may be set to NULL if the
  46. // socket is not bound yet (GetState() returns STATE_BINDING).
  47. virtual SocketAddress GetLocalAddress() const = 0;
  48. // Returns remote address. Returns zeroes if this is not a client TCP socket.
  49. virtual SocketAddress GetRemoteAddress() const = 0;
  50. // Send a packet.
  51. virtual int Send(const void *pv, size_t cb) = 0;
  52. virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr) = 0;
  53. // Close the socket.
  54. virtual int Close() = 0;
  55. // Returns current state of the socket.
  56. virtual State GetState() const = 0;
  57. // Get/set options.
  58. virtual int GetOption(Socket::Option opt, int* value) = 0;
  59. virtual int SetOption(Socket::Option opt, int value) = 0;
  60. // Get/Set current error.
  61. // TODO: Remove SetError().
  62. virtual int GetError() const = 0;
  63. virtual void SetError(int error) = 0;
  64. // Emitted each time a packet is read. Used only for UDP and
  65. // connected TCP sockets.
  66. sigslot::signal4<AsyncPacketSocket*, const char*, size_t,
  67. const SocketAddress&> SignalReadPacket;
  68. // Emitted after address for the socket is allocated, i.e. binding
  69. // is finished. State of the socket is changed from BINDING to BOUND
  70. // (for UDP and server TCP sockets) or CONNECTING (for client TCP
  71. // sockets).
  72. sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
  73. // Emitted for client TCP sockets when state is changed from
  74. // CONNECTING to CONNECTED.
  75. sigslot::signal1<AsyncPacketSocket*> SignalConnect;
  76. // Emitted for client TCP sockets when state is changed from
  77. // CONNECTED to CLOSED.
  78. sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
  79. // Used only for listening TCP sockets.
  80. sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection;
  81. private:
  82. DISALLOW_EVIL_CONSTRUCTORS(AsyncPacketSocket);
  83. };
  84. } // namespace talk_base
  85. #endif // TALK_BASE_ASYNCPACKETSOCKET_H_