Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: net/socket/socket_libevent.h

Issue 348803003: Refactor tcp socket and unix domain socket. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments and fixed unittests for chrome os. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #ifndef NET_SOCKET_SOCKET_LIBEVENT_H_
6 #define NET_SOCKET_SOCKET_LIBEVENT_H_
7
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"
18
19 namespace net {
20
21 class IOBuffer;
22 class IPEndPoint;
23
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{
mmenke 2014/06/30 18:26:51 nit: Space before open brace.
byungchul 2014/06/30 19:38:35 Done. Do you think NET_EXPORT is necessary here?
27 public:
28 SocketLibevent();
29 virtual ~SocketLibevent();
30
31 // Opens a socket and returns net::OK if |address_family| is AF_INET, AF_INET6
32 // or AF_UNIX. Otherwise, returns a net error.
mmenke 2014/06/30 18:26:51 We now DCHECK on address family.
byungchul 2014/06/30 19:38:34 DCHECK() is only for debug build. Modified the com
33 int Open(int address_family);
34 // Takes ownership of |socket|.
35 int AdoptConnectedSocket(SocketDescriptor socket,
36 const SockaddrStorage& peer_address);
37
38 int Bind(const SockaddrStorage& address);
39
40 int Listen(int backlog);
41 int Accept(scoped_ptr<SocketLibevent>* socket,
42 const CompletionCallback& callback);
43
44 int Connect(const SockaddrStorage& address,
45 const CompletionCallback& callback);
46 bool IsConnected() const;
47 bool IsConnectedAndIdle() const;
48
49 // Multiple outstanding requests are not supported.
mmenke 2014/06/30 18:26:51 nit: "...requests of the same type..."
byungchul 2014/06/30 19:38:34 Done.
50 // Full duplex mode (reading and writing at the same time) is supported.
51 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
52 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
53
54 // Waits for next write event. This os called by TCPsocketLibevent for TCP
mmenke 2014/06/30 18:26:51 os -> is
byungchul 2014/06/30 19:38:34 Done.
55 // fastopen after sending first data. Returns ERR_IO_PENDING if it starts
56 // waiting for write event successfully. Otherwise, returns net_error.
mmenke 2014/06/30 18:26:51 nit: "Otherwise, return a net error code."
mmenke 2014/06/30 18:26:51 nit: Think it's worth explicitly saying this must
byungchul 2014/06/30 19:38:34 Done.
byungchul 2014/06/30 19:38:34 Done.
57 int WaitForWrite(IOBuffer* buf, int buf_len,
58 const CompletionCallback& callback);
59
60 int GetLocalAddress(SockaddrStorage* address) const;
61 int GetPeerAddress(SockaddrStorage* address) const;
62 void SetPeerAddress(const SockaddrStorage& address);
63 // Returns true if peer address has been set regardless of socket state.
64 bool HasPeerAddress() const;
65
66 void Close();
67
68 SocketDescriptor socket_fd() const { return socket_fd_; }
69
70 private:
71 // base::MessageLoopForIO::Watcher methods.
72 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
73 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
74
75 int DoAccept(scoped_ptr<SocketLibevent>* socket);
76 void AcceptCompleted();
77
78 int DoConnect();
79 void ConnectCompleted();
80
81 int DoRead(IOBuffer* buf, int buf_len);
82 void ReadCompleted();
83
84 int DoWrite(IOBuffer* buf, int buf_len);
85 void WriteCompleted();
86
87 SocketDescriptor socket_fd_;
88
89 base::MessageLoopForIO::FileDescriptorWatcher accept_socket_watcher_;
90 scoped_ptr<SocketLibevent>* accept_socket_;
91 CompletionCallback accept_callback_;
92
93 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_;
94 scoped_refptr<IOBuffer> read_buf_;
95 int read_buf_len_;
96 // External callback; called when read is complete.
97 CompletionCallback read_callback_;
98
99 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_;
100 scoped_refptr<IOBuffer> write_buf_;
101 int write_buf_len_;
102 // External callback; called when write or connect is complete.
103 CompletionCallback write_callback_;
104
105 // A connect operation is pending. In this case, |write_callback_| needs to be
106 // called when connect is complete.
107 bool waiting_connect_;
108
109 scoped_ptr<SockaddrStorage> peer_address_;
110
111 base::ThreadChecker thread_checker_;
112
113 DISALLOW_COPY_AND_ASSIGN(SocketLibevent);
114 };
115
116 } // namespace net
117
118 #endif // NET_SOCKET_SOCKET_LIBEVENT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698