1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_H_
#define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_H_
#include <queue>
#include <vector>
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "content/browser/renderer_host/p2p/socket_host.h"
#include "content/common/p2p_socket_type.h"
#include "net/base/completion_callback.h"
#include "net/base/ip_endpoint.h"
namespace net {
class DrainableIOBuffer;
class GrowableIOBuffer;
class StreamSocket;
class URLRequestContextGetter;
} // namespace net
namespace content {
class CONTENT_EXPORT P2PSocketHostTcpBase : public P2PSocketHost {
public:
P2PSocketHostTcpBase(IPC::Sender* message_sender,
int socket_id,
P2PSocketType type,
net::URLRequestContextGetter* url_context);
~P2PSocketHostTcpBase() override;
bool InitAccepted(const net::IPEndPoint& remote_address,
net::StreamSocket* socket);
// P2PSocketHost overrides.
bool Init(const net::IPEndPoint& local_address,
const P2PHostAndIPEndPoint& remote_address) override;
void Send(const net::IPEndPoint& to,
const std::vector<char>& data,
const rtc::PacketOptions& options,
uint64 packet_id) override;
P2PSocketHost* AcceptIncomingTcpConnection(
const net::IPEndPoint& remote_address,
int id) override;
bool SetOption(P2PSocketOption option, int value) override;
protected:
// Derived classes will provide the implementation.
virtual int ProcessInput(char* input, int input_len) = 0;
virtual void DoSend(const net::IPEndPoint& to,
const std::vector<char>& data,
const rtc::PacketOptions& options) = 0;
void WriteOrQueue(scoped_refptr<net::DrainableIOBuffer>& buffer);
void OnPacket(const std::vector<char>& data);
void OnError();
private:
friend class P2PSocketHostTcpTestBase;
friend class P2PSocketHostTcpServerTest;
// SSL/TLS connection functions.
void StartTls();
void ProcessTlsSslConnectDone(int status);
void DidCompleteRead(int result);
void DoRead();
void DoWrite();
void HandleWriteResult(int result);
// Callbacks for Connect(), Read() and Write().
void OnConnected(int result);
void OnRead(int result);
void OnWritten(int result);
// Helper method to send socket create message and start read.
void OnOpen();
bool DoSendSocketCreateMsg();
P2PHostAndIPEndPoint remote_address_;
scoped_ptr<net::StreamSocket> socket_;
scoped_refptr<net::GrowableIOBuffer> read_buffer_;
std::queue<scoped_refptr<net::DrainableIOBuffer> > write_queue_;
scoped_refptr<net::DrainableIOBuffer> write_buffer_;
bool write_pending_;
bool connected_;
P2PSocketType type_;
scoped_refptr<net::URLRequestContextGetter> url_context_;
DISALLOW_COPY_AND_ASSIGN(P2PSocketHostTcpBase);
};
class CONTENT_EXPORT P2PSocketHostTcp : public P2PSocketHostTcpBase {
public:
P2PSocketHostTcp(IPC::Sender* message_sender,
int socket_id,
P2PSocketType type,
net::URLRequestContextGetter* url_context);
~P2PSocketHostTcp() override;
protected:
int ProcessInput(char* input, int input_len) override;
void DoSend(const net::IPEndPoint& to,
const std::vector<char>& data,
const rtc::PacketOptions& options) override;
private:
DISALLOW_COPY_AND_ASSIGN(P2PSocketHostTcp);
};
// P2PSocketHostStunTcp class provides the framing of STUN messages when used
// with TURN. These messages will not have length at front of the packet and
// are padded to multiple of 4 bytes.
// Formatting of messages is defined in RFC5766.
class CONTENT_EXPORT P2PSocketHostStunTcp : public P2PSocketHostTcpBase {
public:
P2PSocketHostStunTcp(IPC::Sender* message_sender,
int socket_id,
P2PSocketType type,
net::URLRequestContextGetter* url_context);
~P2PSocketHostStunTcp() override;
protected:
int ProcessInput(char* input, int input_len) override;
void DoSend(const net::IPEndPoint& to,
const std::vector<char>& data,
const rtc::PacketOptions& options) override;
private:
int GetExpectedPacketSize(const char* data, int len, int* pad_bytes);
DISALLOW_COPY_AND_ASSIGN(P2PSocketHostStunTcp);
};
} // namespace content
#endif // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_H_
|