Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef NET_UDP_UDP_SOCKET_WIN_H_ | |
| 6 #define NET_UDP_UDP_SOCKET_WIN_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <winsock2.h> | |
| 10 | |
| 11 #include "base/message_loop.h" | |
| 12 #include "base/ref_counted.h" | |
| 13 #include "base/scoped_ptr.h" | |
| 14 #include "base/threading/non_thread_safe.h" | |
| 15 #include "base/win/object_watcher.h" | |
| 16 #include "net/base/completion_callback.h" | |
| 17 #include "net/base/ip_endpoint.h" | |
| 18 #include "net/base/net_log.h" | |
| 19 #include "net/socket/client_socket.h" | |
|
Mike Belshe
2011/03/10 06:53:38
nit: do you need client_socket.h?
Sergey Ulanov
2011/03/11 00:54:37
Done.
| |
| 20 | |
| 21 namespace net { | |
| 22 | |
| 23 class BoundNetLog; | |
| 24 | |
| 25 class UDPSocketWin : public base::NonThreadSafe { | |
| 26 public: | |
| 27 UDPSocketWin(net::NetLog* net_log, | |
| 28 const net::NetLog::Source& source); | |
|
Mike Belshe
2011/03/10 06:53:38
nit: spacing
Sergey Ulanov
2011/03/11 00:54:37
Done.
| |
| 29 virtual ~UDPSocketWin(); | |
| 30 | |
| 31 // Connect the socket to connect with a certain |address|. | |
| 32 // Returns a net error code. | |
| 33 int Connect(const IPEndPoint& address); | |
| 34 | |
| 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. | |
| 38 int Bind(const IPEndPoint& address); | |
| 39 | |
| 40 // Close the socket. | |
| 41 void Close(); | |
| 42 | |
| 43 // Copy the remote udp address into |address| and return a network error code. | |
| 44 int GetPeerAddress(IPEndPoint* address) const; | |
| 45 | |
| 46 // Copy the local udp address into |address| and return a network error code. | |
| 47 // (similar to getsockname) | |
| 48 int GetLocalAddress(IPEndPoint* address) const; | |
| 49 | |
| 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. | |
| 57 int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); | |
| 58 | |
| 59 // Write to the socket. | |
| 60 // Only usable from the client-side of a UDP socket, after the socket | |
| 61 // has been connected. | |
| 62 int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback); | |
| 63 | |
| 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, | |
| 79 IPEndPoint* address, | |
| 80 CompletionCallback* callback); | |
| 81 | |
| 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, | |
| 93 const IPEndPoint& address, | |
| 94 CompletionCallback* callback); | |
| 95 | |
| 96 // Returns true if the socket is already connected or bound. | |
| 97 bool is_connected() const { return socket_ != kInvalidSocket; } | |
| 98 | |
| 99 private: | |
| 100 static const int kInvalidSocket = -1; | |
|
Mike Belshe
2011/03/10 06:53:38
For windows, I'd get rid of kInvalidSocket, and ju
Sergey Ulanov
2011/03/11 00:54:37
Done.
| |
| 101 | |
| 102 class ReadDelegate : public base::win::ObjectWatcher::Delegate { | |
| 103 public: | |
| 104 explicit ReadDelegate(UDPSocketWin* socket) : socket_(socket) {} | |
| 105 virtual ~ReadDelegate() {} | |
| 106 | |
| 107 // base::ObjectWatcher::Delegate methods: | |
| 108 virtual void OnObjectSignaled(HANDLE object); | |
| 109 | |
| 110 private: | |
| 111 UDPSocketWin* const socket_; | |
| 112 }; | |
| 113 | |
| 114 class WriteDelegate : public base::win::ObjectWatcher::Delegate { | |
| 115 public: | |
| 116 explicit WriteDelegate(UDPSocketWin* socket) : socket_(socket) {} | |
| 117 virtual ~WriteDelegate() {} | |
| 118 | |
| 119 // base::ObjectWatcher::Delegate methods: | |
| 120 virtual void OnObjectSignaled(HANDLE object); | |
| 121 | |
| 122 private: | |
| 123 UDPSocketWin* const socket_; | |
| 124 }; | |
| 125 | |
| 126 void DoReadCallback(int rv); | |
| 127 void DoWriteCallback(int rv); | |
| 128 void DidCompleteRead(); | |
| 129 void DidCompleteWrite(); | |
| 130 bool ProcessSuccessfulRead(int num_bytes); | |
| 131 void ProcessSuccessfulWrite(int num_bytes); | |
| 132 | |
| 133 // Returns the OS error code (or 0 on success). | |
| 134 int CreateSocket(const IPEndPoint& address); | |
| 135 | |
| 136 int InternalRead(); | |
| 137 int InternalWrite(); | |
| 138 | |
| 139 SOCKET socket_; | |
| 140 | |
| 141 // These are mutable since they're just cached copies to make | |
| 142 // GetPeerAddress/GetLocalAddress smarter. | |
| 143 mutable scoped_ptr<IPEndPoint> local_address_; | |
| 144 mutable scoped_ptr<IPEndPoint> remote_address_; | |
| 145 | |
| 146 // The socket's win wrappers | |
| 147 ReadDelegate read_delegate_; | |
| 148 WriteDelegate write_delegate_; | |
| 149 | |
| 150 // Watchers to watch for events from Read() and Write(). | |
| 151 base::win::ObjectWatcher read_watcher_; | |
| 152 base::win::ObjectWatcher write_watcher_; | |
| 153 | |
| 154 // OVERLAPPED for pending read and write operations. | |
| 155 OVERLAPPED read_overlapped_; | |
| 156 OVERLAPPED write_overlapped_; | |
| 157 | |
| 158 // The buffer used by InternalRead() to retry Read requests | |
| 159 WSABUF read_buffer_; | |
| 160 scoped_refptr<IOBuffer> read_iobuffer_; | |
| 161 int read_buf_len_; | |
|
Mike Belshe
2011/03/10 06:53:38
I think we can get rid of read_iobuffer_ and read_
Sergey Ulanov
2011/03/11 00:54:37
We do need read_iobuffer_ to keep the buffer alive
| |
| 162 struct sockaddr_storage recv_addr_storage_; | |
| 163 socklen_t recv_addr_len_; | |
| 164 IPEndPoint* recv_from_address_; | |
| 165 | |
| 166 // The buffer used by InternalWrite() to retry Write requests | |
| 167 WSABUF write_buffer_; | |
| 168 scoped_refptr<IOBuffer> write_iobuffer_; | |
| 169 int write_buf_len_; | |
|
Mike Belshe
2011/03/10 06:53:38
Same comments - nix write_iobuffer_ and write_buf_
Sergey Ulanov
2011/03/11 00:54:37
Done.
| |
| 170 scoped_ptr<IPEndPoint> send_to_address_; | |
| 171 | |
| 172 // External callback; called when read is complete. | |
| 173 CompletionCallback* read_callback_; | |
| 174 | |
| 175 // External callback; called when write is complete. | |
| 176 CompletionCallback* write_callback_; | |
| 177 | |
| 178 BoundNetLog net_log_; | |
| 179 | |
| 180 DISALLOW_COPY_AND_ASSIGN(UDPSocketWin); | |
| 181 }; | |
| 182 | |
| 183 } // namespace net | |
| 184 | |
| 185 #endif // NET_UDP_UDP_SOCKET_WIN_H_ | |
| OLD | NEW |