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.

133 lines
4.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2010, 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_ASYNCSOCKET_H_
  28. #define TALK_BASE_ASYNCSOCKET_H_
  29. #include "talk/base/common.h"
  30. #include "talk/base/sigslot.h"
  31. #include "talk/base/socket.h"
  32. namespace talk_base {
  33. // TODO: Remove Socket and rename AsyncSocket to Socket.
  34. // Provides the ability to perform socket I/O asynchronously.
  35. class AsyncSocket : public Socket {
  36. public:
  37. AsyncSocket();
  38. virtual ~AsyncSocket();
  39. virtual AsyncSocket* Accept(SocketAddress* paddr) = 0;
  40. sigslot::signal1<AsyncSocket*> SignalReadEvent; // ready to read
  41. sigslot::signal1<AsyncSocket*> SignalWriteEvent; // ready to write
  42. sigslot::signal1<AsyncSocket*> SignalConnectEvent; // connected
  43. sigslot::signal2<AsyncSocket*, int> SignalCloseEvent; // closed
  44. };
  45. class AsyncSocketAdapter : public AsyncSocket, public sigslot::has_slots<> {
  46. public:
  47. // The adapted socket may explicitly be NULL, and later assigned using Attach.
  48. // However, subclasses which support detached mode must override any methods
  49. // that will be called during the detached period (usually GetState()), to
  50. // avoid dereferencing a null pointer.
  51. explicit AsyncSocketAdapter(AsyncSocket* socket);
  52. virtual ~AsyncSocketAdapter();
  53. void Attach(AsyncSocket* socket);
  54. virtual SocketAddress GetLocalAddress() const {
  55. return socket_->GetLocalAddress();
  56. }
  57. virtual SocketAddress GetRemoteAddress() const {
  58. return socket_->GetRemoteAddress();
  59. }
  60. virtual int Bind(const SocketAddress& addr) {
  61. return socket_->Bind(addr);
  62. }
  63. virtual int Connect(const SocketAddress& addr) {
  64. return socket_->Connect(addr);
  65. }
  66. virtual int Send(const void* pv, size_t cb) {
  67. return socket_->Send(pv, cb);
  68. }
  69. virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) {
  70. return socket_->SendTo(pv, cb, addr);
  71. }
  72. virtual int Recv(void* pv, size_t cb) {
  73. return socket_->Recv(pv, cb);
  74. }
  75. virtual int RecvFrom(void* pv, size_t cb, SocketAddress* paddr) {
  76. return socket_->RecvFrom(pv, cb, paddr);
  77. }
  78. virtual int Listen(int backlog) {
  79. return socket_->Listen(backlog);
  80. }
  81. virtual AsyncSocket* Accept(SocketAddress* paddr) {
  82. return socket_->Accept(paddr);
  83. }
  84. virtual int Close() {
  85. return socket_->Close();
  86. }
  87. virtual int GetError() const {
  88. return socket_->GetError();
  89. }
  90. virtual void SetError(int error) {
  91. return socket_->SetError(error);
  92. }
  93. virtual ConnState GetState() const {
  94. return socket_->GetState();
  95. }
  96. virtual int EstimateMTU(uint16* mtu) {
  97. return socket_->EstimateMTU(mtu);
  98. }
  99. virtual int GetOption(Option opt, int* value) {
  100. return socket_->GetOption(opt, value);
  101. }
  102. virtual int SetOption(Option opt, int value) {
  103. return socket_->SetOption(opt, value);
  104. }
  105. protected:
  106. virtual void OnConnectEvent(AsyncSocket* socket) {
  107. SignalConnectEvent(this);
  108. }
  109. virtual void OnReadEvent(AsyncSocket* socket) {
  110. SignalReadEvent(this);
  111. }
  112. virtual void OnWriteEvent(AsyncSocket* socket) {
  113. SignalWriteEvent(this);
  114. }
  115. virtual void OnCloseEvent(AsyncSocket* socket, int err) {
  116. SignalCloseEvent(this, err);
  117. }
  118. AsyncSocket* socket_;
  119. };
  120. } // namespace talk_base
  121. #endif // TALK_BASE_ASYNCSOCKET_H_