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.
5 #ifndef JINGLE_GLUE_FAKE_SOCKET_FACTORY_H_
6 #define JINGLE_GLUE_FAKE_SOCKET_FACTORY_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "net/base/ip_endpoint.h"
16 #include "third_party/libjingle/source/talk/base/asyncpacketsocket.h"
17 #include "third_party/libjingle/source/talk/p2p/base/packetsocketfactory.h"
23 namespace jingle_glue
{
25 class FakeSocketManager
;
27 class FakeUDPPacketSocket
: public talk_base::AsyncPacketSocket
,
28 public base::NonThreadSafe
{
30 FakeUDPPacketSocket(FakeSocketManager
* fake_socket_manager
,
31 const net::IPEndPoint
& address
);
32 virtual ~FakeUDPPacketSocket();
34 const net::IPEndPoint
& endpoint() const { return endpoint_
; }
35 void DeliverPacket(const net::IPEndPoint
& from
,
36 const std::vector
<char>& data
);
38 // talk_base::AsyncPacketSocket implementation.
39 virtual talk_base::SocketAddress
GetLocalAddress() const OVERRIDE
;
40 virtual talk_base::SocketAddress
GetRemoteAddress() const OVERRIDE
;
41 virtual int Send(const void *pv
, size_t cb
,
42 const talk_base::PacketOptions
& options
) OVERRIDE
;
43 virtual int SendTo(const void *pv
, size_t cb
,
44 const talk_base::SocketAddress
& addr
,
45 const talk_base::PacketOptions
& options
) OVERRIDE
;
46 virtual int Close() OVERRIDE
;
47 virtual State
GetState() const OVERRIDE
;
48 virtual int GetOption(talk_base::Socket::Option opt
, int* value
) OVERRIDE
;
49 virtual int SetOption(talk_base::Socket::Option opt
, int value
) OVERRIDE
;
50 virtual int GetError() const OVERRIDE
;
51 virtual void SetError(int error
) OVERRIDE
;
59 scoped_refptr
<FakeSocketManager
> fake_socket_manager_
;
60 net::IPEndPoint endpoint_
;
61 talk_base::SocketAddress local_address_
;
62 talk_base::SocketAddress remote_address_
;
66 DISALLOW_COPY_AND_ASSIGN(FakeUDPPacketSocket
);
69 class FakeSocketManager
: public base::RefCountedThreadSafe
<FakeSocketManager
> {
73 void SendPacket(const net::IPEndPoint
& from
,
74 const net::IPEndPoint
& to
,
75 const std::vector
<char>& data
);
77 void AddSocket(FakeUDPPacketSocket
* socket_factory
);
78 void RemoveSocket(FakeUDPPacketSocket
* socket_factory
);
81 friend class base::RefCountedThreadSafe
<FakeSocketManager
>;
85 void DeliverPacket(const net::IPEndPoint
& from
,
86 const net::IPEndPoint
& to
,
87 const std::vector
<char>& data
);
89 base::MessageLoop
* message_loop_
;
90 std::map
<net::IPEndPoint
, FakeUDPPacketSocket
*> endpoints_
;
92 DISALLOW_COPY_AND_ASSIGN(FakeSocketManager
);
95 class FakeSocketFactory
: public talk_base::PacketSocketFactory
{
97 FakeSocketFactory(FakeSocketManager
* socket_manager
,
98 const net::IPAddressNumber
& address
);
99 virtual ~FakeSocketFactory();
101 // talk_base::PacketSocketFactory implementation.
102 virtual talk_base::AsyncPacketSocket
* CreateUdpSocket(
103 const talk_base::SocketAddress
& local_address
,
104 int min_port
, int max_port
) OVERRIDE
;
105 virtual talk_base::AsyncPacketSocket
* CreateServerTcpSocket(
106 const talk_base::SocketAddress
& local_address
, int min_port
, int max_port
,
108 virtual talk_base::AsyncPacketSocket
* CreateClientTcpSocket(
109 const talk_base::SocketAddress
& local_address
,
110 const talk_base::SocketAddress
& remote_address
,
111 const talk_base::ProxyInfo
& proxy_info
,
112 const std::string
& user_agent
,
116 scoped_refptr
<FakeSocketManager
> socket_manager_
;
117 net::IPAddressNumber address_
;
118 int last_allocated_port_
;
120 DISALLOW_COPY_AND_ASSIGN(FakeSocketFactory
);
123 } // namespace jingle_glue
125 #endif // JINGLE_GLUE_FAKE_SOCKET_FACTORY_H_