socket_host_tcp_server.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_SERVER_H_
6#define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_SERVER_H_
7
8#include <map>
9#include <vector>
10
11#include "base/compiler_specific.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/message_loop/message_loop.h"
14#include "content/browser/renderer_host/p2p/socket_host.h"
15#include "content/common/content_export.h"
16#include "content/public/common/p2p_socket_type.h"
17#include "ipc/ipc_sender.h"
18#include "net/base/completion_callback.h"
19#include "net/socket/tcp_server_socket.h"
20
21namespace net {
22class StreamSocket;
23}  // namespace net
24
25namespace content {
26
27class CONTENT_EXPORT P2PSocketHostTcpServer : public P2PSocketHost {
28 public:
29  typedef std::map<net::IPEndPoint, net::StreamSocket*> AcceptedSocketsMap;
30
31  P2PSocketHostTcpServer(IPC::Sender* message_sender, int id,
32                         P2PSocketType client_type);
33  virtual ~P2PSocketHostTcpServer();
34
35  // P2PSocketHost overrides.
36  virtual bool Init(const net::IPEndPoint& local_address,
37                    const net::IPEndPoint& remote_address) OVERRIDE;
38  virtual void Send(const net::IPEndPoint& to,
39                    const std::vector<char>& data,
40                    net::DiffServCodePoint dscp,
41                    uint64 packet_id) OVERRIDE;
42  virtual P2PSocketHost* AcceptIncomingTcpConnection(
43      const net::IPEndPoint& remote_address, int id) OVERRIDE;
44
45 private:
46  friend class P2PSocketHostTcpServerTest;
47
48  void OnError();
49
50  void DoAccept();
51  void HandleAcceptResult(int result);
52
53  // Callback for Accept().
54  void OnAccepted(int result);
55
56  const P2PSocketType client_type_;
57  scoped_ptr<net::ServerSocket> socket_;
58  net::IPEndPoint local_address_;
59
60  scoped_ptr<net::StreamSocket> accept_socket_;
61  AcceptedSocketsMap accepted_sockets_;
62
63  net::CompletionCallback accept_callback_;
64
65  DISALLOW_COPY_AND_ASSIGN(P2PSocketHostTcpServer);
66};
67
68}  // namespace content
69
70#endif  // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_TCP_SERVER_H_
71