OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_SOCKET_SOCKET_BIO_ADAPTER_H_ | 5 #ifndef NET_SOCKET_SOCKET_BIO_ADAPTER_H_ |
6 #define NET_SOCKET_SOCKET_BIO_ADAPTER_H_ | 6 #define NET_SOCKET_SOCKET_BIO_ADAPTER_H_ |
7 | 7 |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "net/base/completion_callback.h" | 10 #include "net/base/completion_callback.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 // | 46 // |
47 // Callers should implement these methods and, when signaled, retry the | 47 // Callers should implement these methods and, when signaled, retry the |
48 // BIO_read or BIO_write. This usually is done by retrying a higher-level | 48 // BIO_read or BIO_write. This usually is done by retrying a higher-level |
49 // operation, such as SSL_read or SSL_write. | 49 // operation, such as SSL_read or SSL_write. |
50 // | 50 // |
51 // Callers may assume that OnReadReady and OnWriteReady will only be called | 51 // Callers may assume that OnReadReady and OnWriteReady will only be called |
52 // from a PostTask or StreamSocket callback. | 52 // from a PostTask or StreamSocket callback. |
53 class Delegate { | 53 class Delegate { |
54 public: | 54 public: |
55 // Called when the BIO is ready to handle BIO_read, after having previously | 55 // Called when the BIO is ready to handle BIO_read, after having previously |
56 // been blocked. | 56 // been blocked. |rv| is an error code if an error occurred or OK. |
57 virtual void OnReadReady() = 0; | 57 virtual void OnReadReady(int rv) = 0; |
58 | 58 |
59 // Called when the BIO is ready to handle BIO_write, after having previously | 59 // Called when the BIO is ready to handle BIO_write, after having previously |
60 // been blocked. | 60 // been blocked. |rv| is an error code if an error occurred or OK. |
61 virtual void OnWriteReady() = 0; | 61 virtual void OnWriteReady(int rv) = 0; |
62 | 62 |
63 protected: | 63 protected: |
64 virtual ~Delegate() {} | 64 virtual ~Delegate() {} |
65 }; | 65 }; |
66 | 66 |
67 // Creates a new SocketBIOAdapter for the specified socket. |socket| and | 67 // Creates a new SocketBIOAdapter for the specified socket. |socket| and |
68 // |delegate| must remain valid for the lifetime of the SocketBIOAdapter. | 68 // |delegate| must remain valid for the lifetime of the SocketBIOAdapter. |
69 SocketBIOAdapter(StreamSocket* socket, | 69 SocketBIOAdapter(StreamSocket* socket, |
70 int read_buffer_capacity, | 70 int read_buffer_capacity, |
71 int write_buffer_capacity, | 71 int write_buffer_capacity, |
72 Delegate* delegate); | 72 Delegate* delegate); |
73 ~SocketBIOAdapter(); | 73 ~SocketBIOAdapter(); |
74 | 74 |
75 BIO* bio() { return bio_.get(); } | 75 BIO* bio() { return bio_.get(); } |
76 | 76 |
77 // Returns true if any data has been read from the underlying StreamSocket, | 77 // Returns true if any data has been read from the underlying StreamSocket, |
78 // but not yet consumed by the BIO. | 78 // but not yet consumed by the BIO. |
79 bool HasPendingReadData(); | 79 bool HasPendingReadData(); |
80 | 80 |
81 // Returns the allocation size estimate in bytes. | 81 // Returns the allocation size estimate in bytes. |
82 size_t GetAllocationSize() const; | 82 size_t GetAllocationSize() const; |
83 | 83 |
84 private: | 84 private: |
85 int BIORead(char* out, int len); | 85 int BIORead(char* out, int len); |
86 void HandleSocketReadResult(int result); | 86 void HandleSocketReadResult(int result); |
87 void OnSocketReadComplete(int result); | 87 void OnSocketReadComplete(int result); |
| 88 void OnSocketReadIfReadyComplete(int result); |
88 | 89 |
89 int BIOWrite(const char* in, int len); | 90 int BIOWrite(const char* in, int len); |
90 void SocketWrite(); | 91 void SocketWrite(); |
91 void HandleSocketWriteResult(int result); | 92 void HandleSocketWriteResult(int result); |
92 void OnSocketWriteComplete(int result); | 93 void OnSocketWriteComplete(int result); |
93 void CallOnReadReady(); | 94 void CallOnReadReady(int rv); |
94 | 95 |
95 static SocketBIOAdapter* GetAdapter(BIO* bio); | 96 static SocketBIOAdapter* GetAdapter(BIO* bio); |
96 static int BIOReadWrapper(BIO* bio, char* out, int len); | 97 static int BIOReadWrapper(BIO* bio, char* out, int len); |
97 static int BIOWriteWrapper(BIO* bio, const char* in, int len); | 98 static int BIOWriteWrapper(BIO* bio, const char* in, int len); |
98 static long BIOCtrlWrapper(BIO* bio, int cmd, long larg, void* parg); | 99 static long BIOCtrlWrapper(BIO* bio, int cmd, long larg, void* parg); |
99 | 100 |
100 static const BIO_METHOD kBIOMethod; | 101 static const BIO_METHOD kBIOMethod; |
101 | 102 |
102 bssl::UniquePtr<BIO> bio_; | 103 bssl::UniquePtr<BIO> bio_; |
103 | 104 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 Delegate* delegate_; | 137 Delegate* delegate_; |
137 | 138 |
138 base::WeakPtrFactory<SocketBIOAdapter> weak_factory_; | 139 base::WeakPtrFactory<SocketBIOAdapter> weak_factory_; |
139 | 140 |
140 DISALLOW_COPY_AND_ASSIGN(SocketBIOAdapter); | 141 DISALLOW_COPY_AND_ASSIGN(SocketBIOAdapter); |
141 }; | 142 }; |
142 | 143 |
143 } // namespace net | 144 } // namespace net |
144 | 145 |
145 #endif // NET_SOCKET_SOCKET_BIO_ADAPTER_H_ | 146 #endif // NET_SOCKET_SOCKET_BIO_ADAPTER_H_ |
OLD | NEW |