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 #ifndef NET_SOCKET_SOCKET_LIBEVENT_H_
6 #define NET_SOCKET_SOCKET_LIBEVENT_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/threading/thread_checker.h"
15 #include "net/base/completion_callback.h"
16 #include "net/base/net_util.h"
17 #include "net/socket/socket_descriptor.h"
24 // Socket class to provide asynchronous read/write operations on top of the
25 // posix socket api. It supports AF_INET, AF_INET6, and AF_UNIX addresses.
26 class SocketLibevent
: public base::MessageLoopForIO::Watcher
{
29 virtual ~SocketLibevent();
31 // Opens a socket and returns net::OK if |address_family| is AF_INET, AF_INET6
32 // or AF_UNIX. Otherwise, it does DCHECK() and returns a net error.
33 int Open(int address_family
);
34 // Takes ownership of |socket|.
35 int AdoptConnectedSocket(SocketDescriptor socket
,
36 const SockaddrStorage
& peer_address
);
38 int Bind(const SockaddrStorage
& address
);
40 int Listen(int backlog
);
41 int Accept(scoped_ptr
<SocketLibevent
>* socket
,
42 const CompletionCallback
& callback
);
44 // Connects socket. On non-ERR_IO_PENDING error, sets errno and returns a net
45 // error code. On ERR_IO_PENDING, |callback| is called with a net error code,
46 // not errno, though errno is set if connect event happens with error.
47 // TODO(byungchul): Need more robust way to pass system errno.
48 int Connect(const SockaddrStorage
& address
,
49 const CompletionCallback
& callback
);
50 bool IsConnected() const;
51 bool IsConnectedAndIdle() const;
53 // Multiple outstanding requests of the same type are not supported.
54 // Full duplex mode (reading and writing at the same time) is supported.
55 // On error which is not ERR_IO_PENDING, sets errno and returns a net error
56 // code. On ERR_IO_PENDING, |callback| is called with a net error code, not
57 // errno, though errno is set if read or write events happen with error.
58 // TODO(byungchul): Need more robust way to pass system errno.
59 int Read(IOBuffer
* buf
, int buf_len
, const CompletionCallback
& callback
);
60 int Write(IOBuffer
* buf
, int buf_len
, const CompletionCallback
& callback
);
62 // Waits for next write event. This is called by TCPsocketLibevent for TCP
63 // fastopen after sending first data. Returns ERR_IO_PENDING if it starts
64 // waiting for write event successfully. Otherwise, returns a net error code.
65 // It must not be called after Write() because Write() calls it internally.
66 int WaitForWrite(IOBuffer
* buf
, int buf_len
,
67 const CompletionCallback
& callback
);
69 int GetLocalAddress(SockaddrStorage
* address
) const;
70 int GetPeerAddress(SockaddrStorage
* address
) const;
71 void SetPeerAddress(const SockaddrStorage
& address
);
72 // Returns true if peer address has been set regardless of socket state.
73 bool HasPeerAddress() const;
77 SocketDescriptor
socket_fd() const { return socket_fd_
; }
80 // base::MessageLoopForIO::Watcher methods.
81 virtual void OnFileCanReadWithoutBlocking(int fd
) OVERRIDE
;
82 virtual void OnFileCanWriteWithoutBlocking(int fd
) OVERRIDE
;
84 int DoAccept(scoped_ptr
<SocketLibevent
>* socket
);
85 void AcceptCompleted();
88 void ConnectCompleted();
90 int DoRead(IOBuffer
* buf
, int buf_len
);
93 int DoWrite(IOBuffer
* buf
, int buf_len
);
94 void WriteCompleted();
96 SocketDescriptor socket_fd_
;
98 base::MessageLoopForIO::FileDescriptorWatcher accept_socket_watcher_
;
99 scoped_ptr
<SocketLibevent
>* accept_socket_
;
100 CompletionCallback accept_callback_
;
102 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_
;
103 scoped_refptr
<IOBuffer
> read_buf_
;
105 // External callback; called when read is complete.
106 CompletionCallback read_callback_
;
108 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_
;
109 scoped_refptr
<IOBuffer
> write_buf_
;
111 // External callback; called when write or connect is complete.
112 CompletionCallback write_callback_
;
114 // A connect operation is pending. In this case, |write_callback_| needs to be
115 // called when connect is complete.
116 bool waiting_connect_
;
118 scoped_ptr
<SockaddrStorage
> peer_address_
;
120 base::ThreadChecker thread_checker_
;
122 DISALLOW_COPY_AND_ASSIGN(SocketLibevent
);
127 #endif // NET_SOCKET_SOCKET_LIBEVENT_H_