- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / p2p / socket_host_udp.h
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_UDP_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_UDP_H_
7
8 #include <deque>
9 #include <set>
10 #include <vector>
11
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "content/browser/renderer_host/p2p/socket_host.h"
17 #include "content/common/content_export.h"
18 #include "content/common/p2p_sockets.h"
19 #include "net/base/ip_endpoint.h"
20 #include "net/udp/udp_server_socket.h"
21
22 namespace content {
23
24 class P2PMessageThrottler;
25
26 class CONTENT_EXPORT P2PSocketHostUdp : public P2PSocketHost {
27  public:
28   P2PSocketHostUdp(IPC::Sender* message_sender, int id,
29                    P2PMessageThrottler* throttler);
30   virtual ~P2PSocketHostUdp();
31
32   // P2PSocketHost overrides.
33   virtual bool Init(const net::IPEndPoint& local_address,
34                     const net::IPEndPoint& remote_address) OVERRIDE;
35   virtual void Send(const net::IPEndPoint& to,
36                     const std::vector<char>& data,
37                     net::DiffServCodePoint dscp,
38                     uint64 packet_id) OVERRIDE;
39   virtual P2PSocketHost* AcceptIncomingTcpConnection(
40       const net::IPEndPoint& remote_address, int id) OVERRIDE;
41
42  private:
43   friend class P2PSocketHostUdpTest;
44
45   typedef std::set<net::IPEndPoint> ConnectedPeerSet;
46
47   struct PendingPacket {
48     PendingPacket(const net::IPEndPoint& to,
49                   const std::vector<char>& content,
50                   net::DiffServCodePoint dscp,
51                   uint64 id);
52     ~PendingPacket();
53     net::IPEndPoint to;
54     scoped_refptr<net::IOBuffer> data;
55     int size;
56     net::DiffServCodePoint dscp;
57     uint64 id;
58   };
59
60   void OnError();
61
62   void DoRead();
63   void OnRecv(int result);
64   void HandleReadResult(int result);
65
66   void DoSend(const PendingPacket& packet);
67   void OnSend(uint64 packet_id, int result);
68   void HandleSendResult(uint64 packet_id, int result);
69
70   scoped_ptr<net::DatagramServerSocket> socket_;
71   scoped_refptr<net::IOBuffer> recv_buffer_;
72   net::IPEndPoint recv_address_;
73
74   std::deque<PendingPacket> send_queue_;
75   bool send_pending_;
76   net::DiffServCodePoint last_dscp_;
77
78   // Set of peer for which we have received STUN binding request or
79   // response or relay allocation request or response.
80   ConnectedPeerSet connected_peers_;
81   P2PMessageThrottler* throttler_;
82
83   DISALLOW_COPY_AND_ASSIGN(P2PSocketHostUdp);
84 };
85
86 }  // namespace content
87
88 #endif  // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_UDP_H_