1 // Copyright 2014 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 #include "mojo/services/network/tcp_server_socket_impl.h"
7 #include "mojo/services/network/net_adapters.h"
8 #include "mojo/services/network/net_address_type_converters.h"
9 #include "mojo/services/network/tcp_connected_socket_impl.h"
10 #include "net/base/net_errors.h"
14 TCPServerSocketImpl::TCPServerSocketImpl(
15 scoped_ptr
<net::TCPSocket
> socket
,
16 scoped_ptr
<mojo::AppRefCount
> app_refcount
,
17 InterfaceRequest
<TCPServerSocket
> request
)
18 : socket_(socket
.Pass()), app_refcount_(app_refcount
.Pass()),
19 binding_(this, request
.Pass()) {
22 TCPServerSocketImpl::~TCPServerSocketImpl() {
25 void TCPServerSocketImpl::Accept(
26 ScopedDataPipeConsumerHandle send_stream
,
27 ScopedDataPipeProducerHandle receive_stream
,
28 InterfaceRequest
<TCPConnectedSocket
> client_socket
,
29 const AcceptCallback
& callback
) {
30 // One possible future enhancement would be to enqueue multiple Accept calls
31 // on this object. This would allow the client to accept some number of
32 // incoming connections rapidly without doing an IPC round-trip.
33 if (!pending_callback_
.is_null()) {
34 // Already have a pending accept on this socket.
35 callback
.Run(MakeNetworkError(net::ERR_UNEXPECTED
), NetAddressPtr());
39 int result
= socket_
->Accept(
40 &accepted_socket_
, &accepted_address_
,
41 base::Bind(&TCPServerSocketImpl::OnAcceptCompleted
,
42 base::Unretained(this)));
43 if (result
== net::OK
|| result
== net::ERR_IO_PENDING
) {
44 pending_callback_
= callback
;
45 pending_send_stream_
= send_stream
.Pass();
46 pending_receive_stream_
= receive_stream
.Pass();
47 pending_client_socket_
= client_socket
.Pass();
48 if (result
== net::OK
)
49 OnAcceptCompleted(net::OK
);
51 callback
.Run(MakeNetworkError(result
), NetAddressPtr());
55 void TCPServerSocketImpl::OnAcceptCompleted(int result
) {
56 if (result
!= net::OK
) {
57 pending_callback_
.Run(MakeNetworkError(result
), NetAddressPtr());
58 pending_send_stream_
.reset();
59 pending_receive_stream_
.reset();
60 pending_client_socket_
= InterfaceRequest
<TCPConnectedSocket
>();
62 new TCPConnectedSocketImpl(
63 accepted_socket_
.Pass(), pending_send_stream_
.Pass(),
64 pending_receive_stream_
.Pass(), pending_client_socket_
.Pass(),
65 app_refcount_
->Clone());
66 pending_callback_
.Run(MakeNetworkError(net::OK
),
67 NetAddress::From(accepted_address_
));
70 pending_callback_
= AcceptCallback();