OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef REMOTING_HOST_SECURITY_KEY_GNUBBY_SOCKET_H_ | |
6 #define REMOTING_HOST_SECURITY_KEY_GNUBBY_SOCKET_H_ | |
7 | |
8 #include <stddef.h> | |
9 | |
10 #include <memory> | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/callback.h" | |
15 #include "base/macros.h" | |
16 #include "base/memory/ref_counted.h" | |
17 #include "base/threading/thread_checker.h" | |
18 | |
19 namespace base { | |
20 class Timer; | |
21 } // namespace base | |
22 | |
23 namespace net { | |
24 class DrainableIOBuffer; | |
25 class IOBufferWithSize; | |
26 class StreamSocket; | |
27 } // namespace net | |
28 | |
29 namespace remoting { | |
30 | |
31 // Class that manages reading requests and sending responses. The socket can | |
32 // only handle receiving one request at a time. It expects to receive no extra | |
33 // bytes over the wire, which is checked by IsRequestTooLarge method. | |
34 class GnubbySocket { | |
35 public: | |
36 GnubbySocket(std::unique_ptr<net::StreamSocket> socket, | |
37 base::TimeDelta timeout, | |
38 const base::Closure& timeout_callback); | |
39 ~GnubbySocket(); | |
40 | |
41 // Returns false if the request has not yet completed, or is too large to be | |
42 // processed. Otherwise, the cached request data is copied into |data_out| and | |
43 // the internal buffer resets and is ready for the next request. | |
44 bool GetAndClearRequestData(std::string* data_out); | |
45 | |
46 // Sends response data to the socket. | |
47 void SendResponse(const std::string& data); | |
48 | |
49 // Sends an SSH error code to the socket. | |
50 void SendSshError(); | |
51 | |
52 // |request_received_callback| is used to notify the caller that request data | |
53 // has been fully read, and caller is to use GetAndClearRequestData method to | |
54 // get the request data. | |
55 void StartReadingRequest(const base::Closure& request_received_callback); | |
56 | |
57 private: | |
58 // Called when bytes are written to |socket_|. | |
59 void OnDataWritten(int result); | |
60 | |
61 // Continues writing to |socket_| if needed. | |
62 void DoWrite(); | |
63 | |
64 // Called when bytes are read from |socket_|. | |
65 void OnDataRead(int bytes_read); | |
66 | |
67 // Continues to read. | |
68 void DoRead(); | |
69 | |
70 // Returns true if the current request is complete. | |
71 bool IsRequestComplete() const; | |
72 | |
73 // Returns true if the stated request size is larger than the allowed maximum. | |
74 bool IsRequestTooLarge() const; | |
75 | |
76 // Returns the stated request length. | |
77 size_t GetRequestLength() const; | |
78 | |
79 // Returns the response length bytes. | |
80 std::string GetResponseLengthAsBytes(const std::string& response) const; | |
81 | |
82 // Resets the socket activity timer. | |
83 void ResetTimer(); | |
84 | |
85 // Ensures GnubbySocket methods are called on the same thread. | |
86 base::ThreadChecker thread_checker_; | |
87 | |
88 // The socket. | |
89 std::unique_ptr<net::StreamSocket> socket_; | |
90 | |
91 // Invoked when request data has been read. | |
92 base::Closure request_received_callback_; | |
93 | |
94 // Indicates whether read has completed and |request_received_callback_| is | |
95 // about to be run. | |
96 bool read_completed_; | |
97 | |
98 // Request data. | |
99 std::vector<char> request_data_; | |
100 | |
101 scoped_refptr<net::DrainableIOBuffer> write_buffer_; | |
102 | |
103 scoped_refptr<net::IOBufferWithSize> read_buffer_; | |
104 | |
105 // The activity timer. | |
106 std::unique_ptr<base::Timer> timer_; | |
107 | |
108 DISALLOW_COPY_AND_ASSIGN(GnubbySocket); | |
109 }; | |
110 | |
111 } // namespace remoting | |
112 | |
113 #endif // REMOTING_HOST_SECURITY_KEY_GNUBBY_SOCKET_H_ | |
OLD | NEW |