blob: abc2dd5c6f6490c0b078fa9636087c262e35101c [file] [log] [blame]
[email protected]a2798d92011-03-02 22:56:181// 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.
4
5#ifndef NET_UDP_UDP_SOCKET_LIBEVENT_H_
6#define NET_UDP_UDP_SOCKET_LIBEVENT_H_
7#pragma once
8
[email protected]3b63f8f42011-03-28 01:54:159#include "base/memory/ref_counted.h"
10#include "base/memory/scoped_ptr.h"
[email protected]a2798d92011-03-02 22:56:1811#include "base/message_loop.h"
[email protected]a2798d92011-03-02 22:56:1812#include "base/threading/non_thread_safe.h"
[email protected]8866f622011-10-18 20:08:1013#include "net/base/address_list_net_log_param.h"
[email protected]a2798d92011-03-02 22:56:1814#include "net/base/completion_callback.h"
[email protected]5370c012011-06-29 03:47:0415#include "net/base/rand_callback.h"
16#include "net/base/io_buffer.h"
[email protected]43d4a0262011-03-09 19:26:0417#include "net/base/ip_endpoint.h"
[email protected]a2798d92011-03-02 22:56:1818#include "net/base/net_log.h"
[email protected]5370c012011-06-29 03:47:0419#include "net/udp/datagram_socket.h"
[email protected]a2798d92011-03-02 22:56:1820
21namespace net {
22
[email protected]a2798d92011-03-02 22:56:1823class UDPSocketLibevent : public base::NonThreadSafe {
24 public:
[email protected]5370c012011-06-29 03:47:0425 UDPSocketLibevent(DatagramSocket::BindType bind_type,
26 const RandIntCallback& rand_int_cb,
27 net::NetLog* net_log,
[email protected]a2798d92011-03-02 22:56:1828 const net::NetLog::Source& source);
29 virtual ~UDPSocketLibevent();
30
31 // Connect the socket to connect with a certain |address|.
32 // Returns a net error code.
[email protected]43d4a0262011-03-09 19:26:0433 int Connect(const IPEndPoint& address);
[email protected]a2798d92011-03-02 22:56:1834
35 // Bind the address/port for this socket to |address|. This is generally
36 // only used on a server.
37 // Returns a net error code.
[email protected]43d4a0262011-03-09 19:26:0438 int Bind(const IPEndPoint& address);
[email protected]a2798d92011-03-02 22:56:1839
40 // Close the socket.
41 void Close();
42
43 // Copy the remote udp address into |address| and return a network error code.
[email protected]43d4a0262011-03-09 19:26:0444 int GetPeerAddress(IPEndPoint* address) const;
[email protected]a2798d92011-03-02 22:56:1845
46 // Copy the local udp address into |address| and return a network error code.
47 // (similar to getsockname)
[email protected]43d4a0262011-03-09 19:26:0448 int GetLocalAddress(IPEndPoint* address) const;
[email protected]a2798d92011-03-02 22:56:1849
50 // IO:
51 // Multiple outstanding read requests are not supported.
52 // Full duplex mode (reading and writing at the same time) is supported
53
54 // Read from the socket.
55 // Only usable from the client-side of a UDP socket, after the socket
56 // has been connected.
[email protected]f1f3f0f82011-10-01 20:38:1057 int Read(IOBuffer* buf, int buf_len, OldCompletionCallback* callback);
[email protected]a2798d92011-03-02 22:56:1858
59 // Write to the socket.
60 // Only usable from the client-side of a UDP socket, after the socket
61 // has been connected.
[email protected]f1f3f0f82011-10-01 20:38:1062 int Write(IOBuffer* buf, int buf_len, OldCompletionCallback* callback);
[email protected]a2798d92011-03-02 22:56:1863
64 // Read from a socket and receive sender address information.
65 // |buf| is the buffer to read data into.
66 // |buf_len| is the maximum amount of data to read.
67 // |address| is a buffer provided by the caller for receiving the sender
68 // address information about the received data. This buffer must be kept
69 // alive by the caller until the callback is placed.
70 // |address_length| is a ptr to the length of the |address| buffer. This
71 // is an input parameter containing the maximum size |address| can hold
72 // and also an output parameter for the size of |address| upon completion.
73 // |callback| the callback on completion of the Recv.
74 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
75 // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|,
76 // and |address_length| alive until the callback is called.
77 int RecvFrom(IOBuffer* buf,
78 int buf_len,
[email protected]43d4a0262011-03-09 19:26:0479 IPEndPoint* address,
[email protected]f1f3f0f82011-10-01 20:38:1080 OldCompletionCallback* callback);
[email protected]a2798d92011-03-02 22:56:1881
82 // Send to a socket with a particular destination.
83 // |buf| is the buffer to send
84 // |buf_len| is the number of bytes to send
85 // |address| is the recipient address.
86 // |address_length| is the size of the recipient address
87 // |callback| is the user callback function to call on complete.
88 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
89 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address|
90 // alive until the callback is called.
91 int SendTo(IOBuffer* buf,
92 int buf_len,
[email protected]43d4a0262011-03-09 19:26:0493 const IPEndPoint& address,
[email protected]f1f3f0f82011-10-01 20:38:1094 OldCompletionCallback* callback);
[email protected]a2798d92011-03-02 22:56:1895
[email protected]df31da42011-10-18 01:44:4096 // Set the receive buffer size (in bytes) for the socket.
97 bool SetReceiveBufferSize(int32 size);
98
99 // Set the send buffer size (in bytes) for the socket.
100 bool SetSendBufferSize(int32 size);
101
[email protected]a2798d92011-03-02 22:56:18102 // Returns true if the socket is already connected or bound.
103 bool is_connected() const { return socket_ != kInvalidSocket; }
104
[email protected]eaf10dc2011-07-18 21:47:35105 const BoundNetLog& NetLog() const { return net_log_; }
106
[email protected]a2798d92011-03-02 22:56:18107 private:
108 static const int kInvalidSocket = -1;
109
110 class ReadWatcher : public MessageLoopForIO::Watcher {
111 public:
112 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {}
113
114 // MessageLoopForIO::Watcher methods
115
116 virtual void OnFileCanReadWithoutBlocking(int /* fd */) {
117 if (socket_->read_callback_)
118 socket_->DidCompleteRead();
119 }
120
121 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) {}
122
123 private:
124 UDPSocketLibevent* const socket_;
125
126 DISALLOW_COPY_AND_ASSIGN(ReadWatcher);
127 };
128
129 class WriteWatcher : public MessageLoopForIO::Watcher {
130 public:
131 explicit WriteWatcher(UDPSocketLibevent* socket) : socket_(socket) {}
132
133 // MessageLoopForIO::Watcher methods
134
135 virtual void OnFileCanReadWithoutBlocking(int /* fd */) {}
136
137 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) {
138 if (socket_->write_callback_)
139 socket_->DidCompleteWrite();
140 }
141
142 private:
143 UDPSocketLibevent* const socket_;
144
145 DISALLOW_COPY_AND_ASSIGN(WriteWatcher);
146 };
147
148 void DoReadCallback(int rv);
149 void DoWriteCallback(int rv);
150 void DidCompleteRead();
151 void DidCompleteWrite();
152
[email protected]8866f622011-10-18 20:08:10153 // Handles stats and logging. |result| is the number of bytes transferred, on
154 // success, or the net error code on failure. On success, LogRead takes in a
155 // sockaddr and its length, which are mandatory, while LogWrite takes in an
156 // optional IPEndPoint.
157 void LogRead(int result, const char* bytes, socklen_t addr_len,
158 const sockaddr* addr) const;
159 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const;
160
[email protected]a2798d92011-03-02 22:56:18161 // Returns the OS error code (or 0 on success).
[email protected]43d4a0262011-03-09 19:26:04162 int CreateSocket(const IPEndPoint& address);
[email protected]a2798d92011-03-02 22:56:18163
[email protected]06043ae2011-03-16 00:00:21164 // Same as SendTo(), except that address is passed by pointer
165 // instead of by reference. It is called from Write() with |address|
166 // set to NULL.
167 int SendToOrWrite(IOBuffer* buf,
168 int buf_len,
169 const IPEndPoint* address,
[email protected]f1f3f0f82011-10-01 20:38:10170 OldCompletionCallback* callback);
[email protected]06043ae2011-03-16 00:00:21171
[email protected]8866f622011-10-18 20:08:10172 int InternalConnect(const IPEndPoint& address);
[email protected]06043ae2011-03-16 00:00:21173 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address);
174 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address);
[email protected]a2798d92011-03-02 22:56:18175
[email protected]5370c012011-06-29 03:47:04176 int DoBind(const IPEndPoint& address);
177 int RandomBind(const IPEndPoint& address);
178
[email protected]a2798d92011-03-02 22:56:18179 int socket_;
180
[email protected]5370c012011-06-29 03:47:04181 // How to do source port binding, used only when UDPSocket is part of
182 // UDPClientSocket, since UDPServerSocket provides Bind.
183 DatagramSocket::BindType bind_type_;
184
185 // PRNG function for generating port numbers.
186 RandIntCallback rand_int_cb_;
187
[email protected]a2798d92011-03-02 22:56:18188 // These are mutable since they're just cached copies to make
189 // GetPeerAddress/GetLocalAddress smarter.
[email protected]43d4a0262011-03-09 19:26:04190 mutable scoped_ptr<IPEndPoint> local_address_;
191 mutable scoped_ptr<IPEndPoint> remote_address_;
[email protected]a2798d92011-03-02 22:56:18192
193 // The socket's libevent wrappers
194 MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_;
195 MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_;
196
197 // The corresponding watchers for reads and writes.
198 ReadWatcher read_watcher_;
199 WriteWatcher write_watcher_;
200
[email protected]43d4a0262011-03-09 19:26:04201 // The buffer used by InternalRead() to retry Read requests
[email protected]a2798d92011-03-02 22:56:18202 scoped_refptr<IOBuffer> read_buf_;
203 int read_buf_len_;
[email protected]43d4a0262011-03-09 19:26:04204 IPEndPoint* recv_from_address_;
[email protected]a2798d92011-03-02 22:56:18205
[email protected]43d4a0262011-03-09 19:26:04206 // The buffer used by InternalWrite() to retry Write requests
[email protected]a2798d92011-03-02 22:56:18207 scoped_refptr<IOBuffer> write_buf_;
208 int write_buf_len_;
[email protected]43d4a0262011-03-09 19:26:04209 scoped_ptr<IPEndPoint> send_to_address_;
[email protected]a2798d92011-03-02 22:56:18210
211 // External callback; called when read is complete.
[email protected]f1f3f0f82011-10-01 20:38:10212 OldCompletionCallback* read_callback_;
[email protected]a2798d92011-03-02 22:56:18213
214 // External callback; called when write is complete.
[email protected]f1f3f0f82011-10-01 20:38:10215 OldCompletionCallback* write_callback_;
[email protected]a2798d92011-03-02 22:56:18216
217 BoundNetLog net_log_;
218
219 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent);
220};
221
222} // namespace net
223
224#endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_