1 // Copyright 2014 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.
5 #ifndef REMOTING_HOST_GNUBBY_SOCKET_H_
6 #define REMOTING_HOST_GNUBBY_SOCKET_H_
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/non_thread_safe.h"
21 class DrainableIOBuffer
;
22 class IOBufferWithSize
;
28 // Class that manages reading requests and sending responses. The socket can
29 // only handle receiving one request at a time. It expects to receive no extra
30 // bytes over the wire, which is checked by IsRequestTooLarge method.
31 class GnubbySocket
: public base::NonThreadSafe
{
33 GnubbySocket(scoped_ptr
<net::StreamSocket
> socket
,
34 const base::TimeDelta
& timeout
,
35 const base::Closure
& timeout_callback
);
38 // Returns false if the request has not yet completed, or is too large to be
39 // processed. Otherwise, the cached request data is copied into |data_out| and
40 // the internal buffer resets and is ready for the next request.
41 bool GetAndClearRequestData(std::string
* data_out
);
43 // Sends response data to the socket.
44 void SendResponse(const std::string
& data
);
46 // Sends an SSH error code to the socket.
49 // |request_received_callback| is used to notify the caller that request data
50 // has been fully read, and caller is to use GetAndClearRequestData method to
51 // get the request data.
52 void StartReadingRequest(const base::Closure
& request_received_callback
);
55 // Called when bytes are written to |socket_|.
56 void OnDataWritten(int result
);
58 // Continues writing to |socket_| if needed.
61 // Called when bytes are read from |socket_|.
62 void OnDataRead(int bytes_read
);
67 // Returns true if the current request is complete.
68 bool IsRequestComplete() const;
70 // Returns true if the stated request size is larger than the allowed maximum.
71 bool IsRequestTooLarge() const;
73 // Returns the stated request length.
74 size_t GetRequestLength() const;
76 // Returns the response length bytes.
77 std::string
GetResponseLengthAsBytes(const std::string
& response
) const;
79 // Resets the socket activity timer.
83 scoped_ptr
<net::StreamSocket
> socket_
;
85 // Invoked when request data has been read.
86 base::Closure request_received_callback_
;
88 // Indicates whether read has completed and |request_received_callback_| is
93 std::vector
<char> request_data_
;
95 scoped_refptr
<net::DrainableIOBuffer
> write_buffer_
;
97 scoped_refptr
<net::IOBufferWithSize
> read_buffer_
;
99 // The activity timer.
100 scoped_ptr
<base::Timer
> timer_
;
102 DISALLOW_COPY_AND_ASSIGN(GnubbySocket
);
105 } // namespace remoting
107 #endif // REMOTING_HOST_GNUBBY_SOCKET_H_