| 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 "webrtc/base/asyncpacketsocket.h" | 14 #include "webrtc/base/asyncpacketsocket.h" |
| 15 #include "webrtc/base/buffer.h" |
| 15 #include "webrtc/base/scoped_ptr.h" | 16 #include "webrtc/base/scoped_ptr.h" |
| 16 #include "webrtc/base/socketfactory.h" | 17 #include "webrtc/base/socketfactory.h" |
| 17 | 18 |
| 18 namespace rtc { | 19 namespace rtc { |
| 19 | 20 |
| 20 // Simulates UDP semantics over TCP. Send and Recv packet sizes | 21 // Simulates UDP semantics over TCP. Send and Recv packet sizes |
| 21 // are preserved, and drops packets silently on Send, rather than | 22 // are preserved, and drops packets silently on Send, rather than |
| 22 // buffer them in user space. | 23 // buffer them in user space. |
| 23 class AsyncTCPSocketBase : public AsyncPacketSocket { | 24 class AsyncTCPSocketBase : public AsyncPacketSocket { |
| 24 public: | 25 public: |
| (...skipping 27 matching lines...) Expand all Loading... |
| 52 // connect() fail (|socket| is destroyed in that case). | 53 // connect() fail (|socket| is destroyed in that case). |
| 53 static AsyncSocket* ConnectSocket(AsyncSocket* socket, | 54 static AsyncSocket* ConnectSocket(AsyncSocket* socket, |
| 54 const SocketAddress& bind_address, | 55 const SocketAddress& bind_address, |
| 55 const SocketAddress& remote_address); | 56 const SocketAddress& remote_address); |
| 56 virtual int SendRaw(const void* pv, size_t cb); | 57 virtual int SendRaw(const void* pv, size_t cb); |
| 57 int FlushOutBuffer(); | 58 int FlushOutBuffer(); |
| 58 // Add data to |outbuf_|. | 59 // Add data to |outbuf_|. |
| 59 void AppendToOutBuffer(const void* pv, size_t cb); | 60 void AppendToOutBuffer(const void* pv, size_t cb); |
| 60 | 61 |
| 61 // Helper methods for |outpos_|. | 62 // Helper methods for |outpos_|. |
| 62 bool IsOutBufferEmpty() const { return outpos_ == 0; } | 63 bool IsOutBufferEmpty() const { return outbuf_.size() == 0; } |
| 63 void ClearOutBuffer() { outpos_ = 0; } | 64 void ClearOutBuffer() { outbuf_.Clear(); } |
| 64 | 65 |
| 65 private: | 66 private: |
| 66 // Called by the underlying socket | 67 // Called by the underlying socket |
| 67 void OnConnectEvent(AsyncSocket* socket); | 68 void OnConnectEvent(AsyncSocket* socket); |
| 68 void OnReadEvent(AsyncSocket* socket); | 69 void OnReadEvent(AsyncSocket* socket); |
| 69 void OnWriteEvent(AsyncSocket* socket); | 70 void OnWriteEvent(AsyncSocket* socket); |
| 70 void OnCloseEvent(AsyncSocket* socket, int error); | 71 void OnCloseEvent(AsyncSocket* socket, int error); |
| 71 | 72 |
| 72 scoped_ptr<AsyncSocket> socket_; | 73 scoped_ptr<AsyncSocket> socket_; |
| 73 bool listen_; | 74 bool listen_; |
| 74 scoped_ptr<char[]> inbuf_; | 75 scoped_ptr<char[]> inbuf_; |
| 75 scoped_ptr<char[]> outbuf_; | 76 Buffer outbuf_; |
| 76 size_t insize_, inpos_, outsize_, outpos_; | 77 size_t insize_, inpos_; |
| 78 size_t max_outsize_; |
| 77 | 79 |
| 78 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocketBase); | 80 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocketBase); |
| 79 }; | 81 }; |
| 80 | 82 |
| 81 class AsyncTCPSocket : public AsyncTCPSocketBase { | 83 class AsyncTCPSocket : public AsyncTCPSocketBase { |
| 82 public: | 84 public: |
| 83 // Binds and connects |socket| and creates AsyncTCPSocket for | 85 // Binds and connects |socket| and creates AsyncTCPSocket for |
| 84 // it. Takes ownership of |socket|. Returns NULL if bind() or | 86 // it. Takes ownership of |socket|. Returns NULL if bind() or |
| 85 // connect() fail (|socket| is destroyed in that case). | 87 // connect() fail (|socket| is destroyed in that case). |
| 86 static AsyncTCPSocket* Create(AsyncSocket* socket, | 88 static AsyncTCPSocket* Create(AsyncSocket* socket, |
| 87 const SocketAddress& bind_address, | 89 const SocketAddress& bind_address, |
| 88 const SocketAddress& remote_address); | 90 const SocketAddress& remote_address); |
| 89 AsyncTCPSocket(AsyncSocket* socket, bool listen); | 91 AsyncTCPSocket(AsyncSocket* socket, bool listen); |
| 90 ~AsyncTCPSocket() override {} | 92 ~AsyncTCPSocket() override {} |
| 91 | 93 |
| 92 int Send(const void* pv, | 94 int Send(const void* pv, |
| 93 size_t cb, | 95 size_t cb, |
| 94 const rtc::PacketOptions& options) override; | 96 const rtc::PacketOptions& options) override; |
| 95 void ProcessInput(char* data, size_t* len) override; | 97 void ProcessInput(char* data, size_t* len) override; |
| 96 void HandleIncomingConnection(AsyncSocket* socket) override; | 98 void HandleIncomingConnection(AsyncSocket* socket) override; |
| 97 | 99 |
| 98 private: | 100 private: |
| 99 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocket); | 101 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncTCPSocket); |
| 100 }; | 102 }; |
| 101 | 103 |
| 102 } // namespace rtc | 104 } // namespace rtc |
| 103 | 105 |
| 104 #endif // WEBRTC_BASE_ASYNCTCPSOCKET_H_ | 106 #endif // WEBRTC_BASE_ASYNCTCPSOCKET_H_ |
| OLD | NEW |