OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_BASE_ASYNCTCPSOCKET_H_ | 11 #ifndef WEBRTC_BASE_ASYNCTCPSOCKET_H_ |
12 #define WEBRTC_BASE_ASYNCTCPSOCKET_H_ | 12 #define WEBRTC_BASE_ASYNCTCPSOCKET_H_ |
13 | 13 |
14 #include <memory> | |
15 | 14 |
16 #include "webrtc/base/asyncpacketsocket.h" | 15 // This header is deprecated and is just left here temporarily during |
17 #include "webrtc/base/buffer.h" | 16 // refactoring. See https://bugs.webrtc.org/7634 for more details. |
18 #include "webrtc/base/constructormagic.h" | 17 #include "webrtc/rtc_base/asynctcpsocket.h" |
19 #include "webrtc/base/socketfactory.h" | |
20 | |
21 namespace rtc { | |
22 | |
23 // Simulates UDP semantics over TCP. Send and Recv packet sizes | |
24 // are preserved, and drops packets silently on Send, rather than | |
25 // buffer them in user space. | |
26 class AsyncTCPSocketBase : public AsyncPacketSocket { | |
27 public: | |
28 AsyncTCPSocketBase(AsyncSocket* socket, bool listen, size_t max_packet_size); | |
29 ~AsyncTCPSocketBase() override; | |
30 | |
31 // Pure virtual methods to send and recv data. | |
32 int Send(const void *pv, size_t cb, | |
33 const rtc::PacketOptions& options) override = 0; | |
34 virtual void ProcessInput(char* data, size_t* len) = 0; | |
35 // Signals incoming connection. | |
36 virtual void HandleIncomingConnection(AsyncSocket* socket) = 0; | |
37 | |
38 SocketAddress GetLocalAddress() const override; | |
39 SocketAddress GetRemoteAddress() const override; | |
40 int SendTo(const void* pv, | |
41 size_t cb, | |
42 const SocketAddress& addr, | |
43 const rtc::PacketOptions& options) override; | |
44 int Close() override; | |
45 | |
46 State GetState() const override; | |
47 int GetOption(Socket::Option opt, int* value) override; | |
48 int SetOption(Socket::Option opt, int value) override; | |
49 int GetError() const override; | |
50 void SetError(int error) override; | |
51 | |
52 protected: | |
53 // Binds and connects |socket| and creates AsyncTCPSocket for | |
54 // it. Takes ownership of |socket|. Returns null if bind() or | |
55 // connect() fail (|socket| is destroyed in that case). | |
56 static AsyncSocket* ConnectSocket(AsyncSocket* socket, | |
57 const SocketAddress& bind_address, | |
58 const SocketAddress& remote_address); | |
59 virtual int SendRaw(const void* pv, size_t cb); | |
60 int FlushOutBuffer(); | |
61 // Add data to |outbuf_|. | |
62 void AppendToOutBuffer(const void* pv, size_t cb); | |
63 | |
64 // Helper methods for |outpos_|. | |
65 bool IsOutBufferEmpty() const { return outbuf_.size() == 0; } | |
66 void ClearOutBuffer() { outbuf_.Clear(); } | |
67 | |
68 private: | |
69 // Called by the underlying socket | |
70 void OnConnectEvent(AsyncSocket* socket); | |
71 void OnReadEvent(AsyncSocket* socket); | |
72 void OnWriteEvent(AsyncSocket* socket); | |
73 void OnCloseEvent(AsyncSocket* socket, int error); | |
74 | |
75 std::unique_ptr<AsyncSocket> socket_; | |
76 bool listen_; | |
77 Buffer inbuf_; | |
78 Buffer outbuf_; | |
79 size_t max_insize_; | |
80 size_t max_outsize_; | |
81 | |
82 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocketBase); | |
83 }; | |
84 | |
85 class AsyncTCPSocket : public AsyncTCPSocketBase { | |
86 public: | |
87 // Binds and connects |socket| and creates AsyncTCPSocket for | |
88 // it. Takes ownership of |socket|. Returns null if bind() or | |
89 // connect() fail (|socket| is destroyed in that case). | |
90 static AsyncTCPSocket* Create(AsyncSocket* socket, | |
91 const SocketAddress& bind_address, | |
92 const SocketAddress& remote_address); | |
93 AsyncTCPSocket(AsyncSocket* socket, bool listen); | |
94 ~AsyncTCPSocket() override {} | |
95 | |
96 int Send(const void* pv, | |
97 size_t cb, | |
98 const rtc::PacketOptions& options) override; | |
99 void ProcessInput(char* data, size_t* len) override; | |
100 void HandleIncomingConnection(AsyncSocket* socket) override; | |
101 | |
102 private: | |
103 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocket); | |
104 }; | |
105 | |
106 } // namespace rtc | |
107 | 18 |
108 #endif // WEBRTC_BASE_ASYNCTCPSOCKET_H_ | 19 #endif // WEBRTC_BASE_ASYNCTCPSOCKET_H_ |
OLD | NEW |