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

Side by Side Diff: chrome/browser/extensions/api/socket/socket.h

Issue 10310170: Improve socket.write() to make sure all data will be sent out. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fix compile error on Mac Created 8 years, 7 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
« no previous file with comments | « no previous file | chrome/browser/extensions/api/socket/socket.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ 6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_
7 #pragma once 7 #pragma once
8 8
9 #include <queue>
9 #include <string> 10 #include <string>
11 #include <utility>
10 12
11 #include "base/callback.h" 13 #include "base/callback.h"
12 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
13 #include "chrome/browser/extensions/api/api_resource.h" 15 #include "chrome/browser/extensions/api/api_resource.h"
16 #include "net/base/completion_callback.h"
14 #include "net/base/io_buffer.h" 17 #include "net/base/io_buffer.h"
15 18
16 namespace net { 19 namespace net {
17 class AddressList; 20 class AddressList;
18 class IPEndPoint; 21 class IPEndPoint;
19 class Socket; 22 class Socket;
20 } 23 }
21 24
22 namespace extensions { 25 namespace extensions {
23 26
24 typedef base::Callback<void(int)> CompletionCallback; 27 typedef base::Callback<void(int)> CompletionCallback;
25 typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> 28 typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)>
26 ReadCompletionCallback; 29 ReadCompletionCallback;
27 typedef base::Callback< 30 typedef base::Callback<
28 void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)> 31 void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)>
29 RecvFromCompletionCallback; 32 RecvFromCompletionCallback;
30 33
31 // A Socket wraps a low-level socket and includes housekeeping information that 34 // A Socket wraps a low-level socket and includes housekeeping information that
32 // we need to manage it in the context of an extension. 35 // we need to manage it in the context of an extension.
33 class Socket : public APIResource { 36 class Socket : public APIResource {
34 public: 37 public:
35 virtual ~Socket(); 38 virtual ~Socket();
36
37 // Returns net::OK if successful, or an error code otherwise.
38 virtual void Connect(const std::string& address, 39 virtual void Connect(const std::string& address,
39 int port, 40 int port,
40 const CompletionCallback& callback) = 0; 41 const CompletionCallback& callback) = 0;
41 virtual void Disconnect() = 0; 42 virtual void Disconnect() = 0;
42
43 virtual int Bind(const std::string& address, int port) = 0; 43 virtual int Bind(const std::string& address, int port) = 0;
44 44
45 // Returns the number of bytes read into the buffer, or a negative number if 45 // The |callback| will be called with the number of bytes read into the
46 // an error occurred. 46 // buffer, or a negative number if an error occurred.
47 virtual void Read(int count, 47 virtual void Read(int count,
48 const ReadCompletionCallback& callback) = 0; 48 const ReadCompletionCallback& callback) = 0;
49 49
50 // Returns the number of bytes successfully written, or a negative error 50 // The |callback| will be called with |byte_count| or a negative number if an
51 // code. Note that ERR_IO_PENDING means that the operation blocked, in which 51 // error occurred.
52 // case |event_notifier| (supplied at socket creation) will eventually be 52 void Write(scoped_refptr<net::IOBuffer> io_buffer,
53 // called with the final result (again, either a nonnegative number of bytes 53 int byte_count,
54 // written, or a negative error). 54 const CompletionCallback& callback);
55 virtual void Write(scoped_refptr<net::IOBuffer> io_buffer,
56 int byte_count,
57 const CompletionCallback& callback) = 0;
58 55
59 virtual void RecvFrom(int count, 56 virtual void RecvFrom(int count,
60 const RecvFromCompletionCallback& callback) = 0; 57 const RecvFromCompletionCallback& callback) = 0;
61
62 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer, 58 virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer,
63 int byte_count, 59 int byte_count,
64 const std::string& address, 60 const std::string& address,
65 int port, 61 int port,
66 const CompletionCallback& callback) = 0; 62 const CompletionCallback& callback) = 0;
67
68 static bool StringAndPortToAddressList(const std::string& ip_address_str, 63 static bool StringAndPortToAddressList(const std::string& ip_address_str,
69 int port, 64 int port,
70 net::AddressList* address_list); 65 net::AddressList* address_list);
71 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str, 66 static bool StringAndPortToIPEndPoint(const std::string& ip_address_str,
72 int port, 67 int port,
73 net::IPEndPoint* ip_end_point); 68 net::IPEndPoint* ip_end_point);
74 static void IPEndPointToStringAndPort(const net::IPEndPoint& address, 69 static void IPEndPointToStringAndPort(const net::IPEndPoint& address,
75 std::string* ip_address_str, 70 std::string* ip_address_str,
76 int* port); 71 int* port);
77 72
78 protected: 73 protected:
79 explicit Socket(APIResourceEventNotifier* event_notifier); 74 explicit Socket(APIResourceEventNotifier* event_notifier);
75
76 void WriteData();
77 virtual int WriteImpl(net::IOBuffer* io_buffer,
78 int io_buffer_size,
79 const net::CompletionCallback& callback) = 0;
80 virtual void OnWriteComplete(int result);
81
80 const std::string address_; 82 const std::string address_;
81 int port_; 83 int port_;
82 bool is_connected_; 84 bool is_connected_;
85
86 private:
87 struct WriteRequest {
88 WriteRequest(scoped_refptr<net::IOBuffer> io_buffer,
89 int byte_count,
90 const CompletionCallback& callback)
91 : io_buffer(io_buffer),
92 byte_count(byte_count),
93 callback(callback),
94 bytes_written(0) { }
95 ~WriteRequest() { }
96 scoped_refptr<net::IOBuffer> io_buffer;
97 int byte_count;
98 CompletionCallback callback;
99 int bytes_written;
100 };
101 std::queue<WriteRequest> write_queue_;
102 scoped_refptr<net::IOBuffer> io_buffer_write_;
83 }; 103 };
84 104
85 } // namespace extensions 105 } // namespace extensions
86 106
87 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_ 107 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/api/socket/socket.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698