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
11#ifndef WEBRTC_BASE_ASYNCSOCKET_H_
12#define WEBRTC_BASE_ASYNCSOCKET_H_
13
14#include "webrtc/base/common.h"
15#include "webrtc/base/sigslot.h"
16#include "webrtc/base/socket.h"
17
18namespace rtc {
19
20// TODO: Remove Socket and rename AsyncSocket to Socket.
21
22// Provides the ability to perform socket I/O asynchronously.
23class AsyncSocket : public Socket {
24 public:
25  AsyncSocket();
26  virtual ~AsyncSocket();
27
28  virtual AsyncSocket* Accept(SocketAddress* paddr) = 0;
29
30  // SignalReadEvent and SignalWriteEvent use multi_threaded_local to allow
31  // access concurrently from different thread.
32  // For example SignalReadEvent::connect will be called in AsyncUDPSocket ctor
33  // but at the same time the SocketDispatcher maybe signaling the read event.
34  // ready to read
35  sigslot::signal1<AsyncSocket*,
36                   sigslot::multi_threaded_local> SignalReadEvent;
37  // ready to write
38  sigslot::signal1<AsyncSocket*,
39                   sigslot::multi_threaded_local> SignalWriteEvent;
40  sigslot::signal1<AsyncSocket*> SignalConnectEvent;     // connected
41  sigslot::signal2<AsyncSocket*, int> SignalCloseEvent;  // closed
42};
43
44class AsyncSocketAdapter : public AsyncSocket, public sigslot::has_slots<> {
45 public:
46  // The adapted socket may explicitly be NULL, and later assigned using Attach.
47  // However, subclasses which support detached mode must override any methods
48  // that will be called during the detached period (usually GetState()), to
49  // avoid dereferencing a null pointer.
50  explicit AsyncSocketAdapter(AsyncSocket* socket);
51  virtual ~AsyncSocketAdapter();
52  void Attach(AsyncSocket* socket);
53  virtual SocketAddress GetLocalAddress() const {
54    return socket_->GetLocalAddress();
55  }
56  virtual SocketAddress GetRemoteAddress() const {
57    return socket_->GetRemoteAddress();
58  }
59  virtual int Bind(const SocketAddress& addr) {
60    return socket_->Bind(addr);
61  }
62  virtual int Connect(const SocketAddress& addr) {
63    return socket_->Connect(addr);
64  }
65  virtual int Send(const void* pv, size_t cb) {
66    return socket_->Send(pv, cb);
67  }
68  virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) {
69    return socket_->SendTo(pv, cb, addr);
70  }
71  virtual int Recv(void* pv, size_t cb) {
72    return socket_->Recv(pv, cb);
73  }
74  virtual int RecvFrom(void* pv, size_t cb, SocketAddress* paddr) {
75    return socket_->RecvFrom(pv, cb, paddr);
76  }
77  virtual int Listen(int backlog) {
78    return socket_->Listen(backlog);
79  }
80  virtual AsyncSocket* Accept(SocketAddress* paddr) {
81    return socket_->Accept(paddr);
82  }
83  virtual int Close() {
84    return socket_->Close();
85  }
86  virtual int GetError() const {
87    return socket_->GetError();
88  }
89  virtual void SetError(int error) {
90    return socket_->SetError(error);
91  }
92  virtual ConnState GetState() const {
93    return socket_->GetState();
94  }
95  virtual int EstimateMTU(uint16* mtu) {
96    return socket_->EstimateMTU(mtu);
97  }
98  virtual int GetOption(Option opt, int* value) {
99    return socket_->GetOption(opt, value);
100  }
101  virtual int SetOption(Option opt, int value) {
102    return socket_->SetOption(opt, value);
103  }
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
119  AsyncSocket* socket_;
120};
121
122}  // namespace rtc
123
124#endif  // WEBRTC_BASE_ASYNCSOCKET_H_
125