OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_UDP_UDP_SOCKET_LIBEVENT_H_ | 5 #ifndef NET_UDP_UDP_SOCKET_LIBEVENT_H_ |
6 #define NET_UDP_UDP_SOCKET_LIBEVENT_H_ | 6 #define NET_UDP_UDP_SOCKET_LIBEVENT_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 // (similar to getsockname) | 47 // (similar to getsockname) |
48 int GetLocalAddress(IPEndPoint* address) const; | 48 int GetLocalAddress(IPEndPoint* address) const; |
49 | 49 |
50 // IO: | 50 // IO: |
51 // Multiple outstanding read requests are not supported. | 51 // Multiple outstanding read requests are not supported. |
52 // Full duplex mode (reading and writing at the same time) is supported | 52 // Full duplex mode (reading and writing at the same time) is supported |
53 | 53 |
54 // Read from the socket. | 54 // Read from the socket. |
55 // Only usable from the client-side of a UDP socket, after the socket | 55 // Only usable from the client-side of a UDP socket, after the socket |
56 // has been connected. | 56 // has been connected. |
57 int Read(IOBuffer* buf, int buf_len, OldCompletionCallback* callback); | |
58 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 57 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
59 | 58 |
60 // Write to the socket. | 59 // Write to the socket. |
61 // Only usable from the client-side of a UDP socket, after the socket | 60 // Only usable from the client-side of a UDP socket, after the socket |
62 // has been connected. | 61 // has been connected. |
63 int Write(IOBuffer* buf, int buf_len, OldCompletionCallback* callback); | 62 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
64 | 63 |
65 // Read from a socket and receive sender address information. | 64 // Read from a socket and receive sender address information. |
66 // |buf| is the buffer to read data into. | 65 // |buf| is the buffer to read data into. |
67 // |buf_len| is the maximum amount of data to read. | 66 // |buf_len| is the maximum amount of data to read. |
68 // |address| is a buffer provided by the caller for receiving the sender | 67 // |address| is a buffer provided by the caller for receiving the sender |
69 // address information about the received data. This buffer must be kept | 68 // address information about the received data. This buffer must be kept |
70 // alive by the caller until the callback is placed. | 69 // alive by the caller until the callback is placed. |
71 // |address_length| is a ptr to the length of the |address| buffer. This | 70 // |address_length| is a ptr to the length of the |address| buffer. This |
72 // is an input parameter containing the maximum size |address| can hold | 71 // is an input parameter containing the maximum size |address| can hold |
73 // and also an output parameter for the size of |address| upon completion. | 72 // and also an output parameter for the size of |address| upon completion. |
74 // |callback| the callback on completion of the Recv. | 73 // |callback| the callback on completion of the Recv. |
75 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. | 74 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. |
76 // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|, | 75 // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|, |
77 // and |address_length| alive until the callback is called. | 76 // and |address_length| alive until the callback is called. |
78 int RecvFrom(IOBuffer* buf, | 77 int RecvFrom(IOBuffer* buf, |
79 int buf_len, | 78 int buf_len, |
80 IPEndPoint* address, | 79 IPEndPoint* address, |
81 OldCompletionCallback* callback); | |
82 int RecvFrom(IOBuffer* buf, | |
83 int buf_len, | |
84 IPEndPoint* address, | |
85 const CompletionCallback& callback); | 80 const CompletionCallback& callback); |
86 | 81 |
87 // Send to a socket with a particular destination. | 82 // Send to a socket with a particular destination. |
88 // |buf| is the buffer to send | 83 // |buf| is the buffer to send |
89 // |buf_len| is the number of bytes to send | 84 // |buf_len| is the number of bytes to send |
90 // |address| is the recipient address. | 85 // |address| is the recipient address. |
91 // |address_length| is the size of the recipient address | 86 // |address_length| is the size of the recipient address |
92 // |callback| is the user callback function to call on complete. | 87 // |callback| is the user callback function to call on complete. |
93 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. | 88 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. |
94 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address| | 89 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address| |
95 // alive until the callback is called. | 90 // alive until the callback is called. |
96 int SendTo(IOBuffer* buf, | 91 int SendTo(IOBuffer* buf, |
97 int buf_len, | 92 int buf_len, |
98 const IPEndPoint& address, | 93 const IPEndPoint& address, |
99 OldCompletionCallback* callback); | 94 const CompletionCallback& callback); |
100 | 95 |
101 // Set the receive buffer size (in bytes) for the socket. | 96 // Set the receive buffer size (in bytes) for the socket. |
102 bool SetReceiveBufferSize(int32 size); | 97 bool SetReceiveBufferSize(int32 size); |
103 | 98 |
104 // Set the send buffer size (in bytes) for the socket. | 99 // Set the send buffer size (in bytes) for the socket. |
105 bool SetSendBufferSize(int32 size); | 100 bool SetSendBufferSize(int32 size); |
106 | 101 |
107 // Returns true if the socket is already connected or bound. | 102 // Returns true if the socket is already connected or bound. |
108 bool is_connected() const { return socket_ != kInvalidSocket; } | 103 bool is_connected() const { return socket_ != kInvalidSocket; } |
109 | 104 |
110 const BoundNetLog& NetLog() const { return net_log_; } | 105 const BoundNetLog& NetLog() const { return net_log_; } |
111 | 106 |
112 private: | 107 private: |
113 static const int kInvalidSocket = -1; | 108 static const int kInvalidSocket = -1; |
114 | 109 |
115 class ReadWatcher : public MessageLoopForIO::Watcher { | 110 class ReadWatcher : public MessageLoopForIO::Watcher { |
116 public: | 111 public: |
117 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} | 112 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} |
118 | 113 |
119 // MessageLoopForIO::Watcher methods | 114 // MessageLoopForIO::Watcher methods |
120 | 115 |
121 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { | 116 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { |
122 if (socket_->old_read_callback_ || !socket_->read_callback_.is_null()) | 117 if (!socket_->read_callback_.is_null()) |
123 socket_->DidCompleteRead(); | 118 socket_->DidCompleteRead(); |
124 } | 119 } |
125 | 120 |
126 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {} | 121 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE {} |
127 | 122 |
128 private: | 123 private: |
129 UDPSocketLibevent* const socket_; | 124 UDPSocketLibevent* const socket_; |
130 | 125 |
131 DISALLOW_COPY_AND_ASSIGN(ReadWatcher); | 126 DISALLOW_COPY_AND_ASSIGN(ReadWatcher); |
132 }; | 127 }; |
133 | 128 |
134 class WriteWatcher : public MessageLoopForIO::Watcher { | 129 class WriteWatcher : public MessageLoopForIO::Watcher { |
135 public: | 130 public: |
136 explicit WriteWatcher(UDPSocketLibevent* socket) : socket_(socket) {} | 131 explicit WriteWatcher(UDPSocketLibevent* socket) : socket_(socket) {} |
137 | 132 |
138 // MessageLoopForIO::Watcher methods | 133 // MessageLoopForIO::Watcher methods |
139 | 134 |
140 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE {} | 135 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE {} |
141 | 136 |
142 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { | 137 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE { |
143 if (socket_->write_callback_) | 138 if (!socket_->write_callback_.is_null()) |
144 socket_->DidCompleteWrite(); | 139 socket_->DidCompleteWrite(); |
145 } | 140 } |
146 | 141 |
147 private: | 142 private: |
148 UDPSocketLibevent* const socket_; | 143 UDPSocketLibevent* const socket_; |
149 | 144 |
150 DISALLOW_COPY_AND_ASSIGN(WriteWatcher); | 145 DISALLOW_COPY_AND_ASSIGN(WriteWatcher); |
151 }; | 146 }; |
152 | 147 |
153 void DoReadCallback(int rv); | 148 void DoReadCallback(int rv); |
(...skipping 11 matching lines...) Expand all Loading... |
165 | 160 |
166 // Returns the OS error code (or 0 on success). | 161 // Returns the OS error code (or 0 on success). |
167 int CreateSocket(const IPEndPoint& address); | 162 int CreateSocket(const IPEndPoint& address); |
168 | 163 |
169 // Same as SendTo(), except that address is passed by pointer | 164 // Same as SendTo(), except that address is passed by pointer |
170 // instead of by reference. It is called from Write() with |address| | 165 // instead of by reference. It is called from Write() with |address| |
171 // set to NULL. | 166 // set to NULL. |
172 int SendToOrWrite(IOBuffer* buf, | 167 int SendToOrWrite(IOBuffer* buf, |
173 int buf_len, | 168 int buf_len, |
174 const IPEndPoint* address, | 169 const IPEndPoint* address, |
175 OldCompletionCallback* callback); | 170 const CompletionCallback& callback); |
176 | 171 |
177 int InternalConnect(const IPEndPoint& address); | 172 int InternalConnect(const IPEndPoint& address); |
178 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); | 173 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); |
179 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); | 174 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); |
180 | 175 |
181 int DoBind(const IPEndPoint& address); | 176 int DoBind(const IPEndPoint& address); |
182 int RandomBind(const IPEndPoint& address); | 177 int RandomBind(const IPEndPoint& address); |
183 | 178 |
184 int socket_; | 179 int socket_; |
185 | 180 |
(...skipping 21 matching lines...) Expand all Loading... |
207 scoped_refptr<IOBuffer> read_buf_; | 202 scoped_refptr<IOBuffer> read_buf_; |
208 int read_buf_len_; | 203 int read_buf_len_; |
209 IPEndPoint* recv_from_address_; | 204 IPEndPoint* recv_from_address_; |
210 | 205 |
211 // The buffer used by InternalWrite() to retry Write requests | 206 // The buffer used by InternalWrite() to retry Write requests |
212 scoped_refptr<IOBuffer> write_buf_; | 207 scoped_refptr<IOBuffer> write_buf_; |
213 int write_buf_len_; | 208 int write_buf_len_; |
214 scoped_ptr<IPEndPoint> send_to_address_; | 209 scoped_ptr<IPEndPoint> send_to_address_; |
215 | 210 |
216 // External callback; called when read is complete. | 211 // External callback; called when read is complete. |
217 OldCompletionCallback* old_read_callback_; | |
218 CompletionCallback read_callback_; | 212 CompletionCallback read_callback_; |
219 | 213 |
220 // External callback; called when write is complete. | 214 // External callback; called when write is complete. |
221 OldCompletionCallback* write_callback_; | 215 CompletionCallback write_callback_; |
222 | 216 |
223 BoundNetLog net_log_; | 217 BoundNetLog net_log_; |
224 | 218 |
225 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); | 219 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); |
226 }; | 220 }; |
227 | 221 |
228 } // namespace net | 222 } // namespace net |
229 | 223 |
230 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ | 224 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ |
OLD | NEW |