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

Side by Side Diff: google_apis/gcm/engine/connection_handler_impl.h

Issue 54743007: [GCM] Add connection factory for creating MCS connections (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Self review Created 7 years, 1 month 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 GOOGLE_APIS_GCM_ENGINE_CONNECTION_HANDLER_H_ 5 #ifndef GOOGLE_APIS_GCM_ENGINE_CONNECTION_HANDLER_IMPL_H_
6 #define GOOGLE_APIS_GCM_ENGINE_CONNECTION_HANDLER_H_ 6 #define GOOGLE_APIS_GCM_ENGINE_CONNECTION_HANDLER_IMPL_H_
7 7
8 #include "base/memory/weak_ptr.h" 8 #include "base/memory/weak_ptr.h"
9 #include "base/time/time.h"
9 #include "base/timer/timer.h" 10 #include "base/timer/timer.h"
10 #include "google_apis/gcm/base/gcm_export.h" 11 #include "google_apis/gcm/engine/connection_handler.h"
11 #include "google_apis/gcm/protocol/mcs.pb.h" 12 #include "google_apis/gcm/protocol/gcm.pb.h"
12
13 namespace net{
14 class StreamSocket;
15 }
16 13
17 namespace gcm { 14 namespace gcm {
18 15
19 class SocketInputStream; 16 class GCM_EXPORT ConnectionHandlerImpl : public ConnectionHandler {
20 class SocketOutputStream;
21
22 // Handles performing the protocol handshake and sending/receiving protobuf
23 // messages. Note that no retrying or queueing is enforced at this layer.
24 // Once a connection error is encountered, the ConnectionHandler will disconnect
25 // the socket and must be reinitialized with a new StreamSocket before
26 // messages can be sent/received again.
27 class GCM_EXPORT ConnectionHandler {
28 public: 17 public:
29 typedef base::Callback<void(scoped_ptr<google::protobuf::MessageLite>)>
30 ProtoReceivedCallback;
31 typedef base::Closure ProtoSentCallback;
32 typedef base::Callback<void(int)> ConnectionChangedCallback;
33
34 explicit ConnectionHandler(base::TimeDelta read_timeout);
35 ~ConnectionHandler();
36
37 // Starts a new MCS connection handshake (using |login_request|) and, upon
38 // success, begins listening for incoming/outgoing messages. A successful
39 // handshake is when a mcs_proto::LoginResponse is received, and is signaled
40 // via the |read_callback|.
41 // Outputs:
42 // |read_callback| will be invoked with the contents of any received protobuf 18 // |read_callback| will be invoked with the contents of any received protobuf
43 // message. 19 // message.
44 // |write_callback| will be invoked anytime a message has been successfully 20 // |write_callback| will be invoked anytime a message has been successfully
45 // sent. Note: this just means the data was sent to the wire, not that the 21 // sent. Note: this just means the data was sent to the wire, not that the
46 // other end received it. 22 // other end received it.
47 // |connection_callback| will be invoked with any fatal read/write errors 23 // |connection_callback| will be invoked with any fatal read/write errors
48 // encountered. 24 // encountered.
49 // 25 ConnectionHandlerImpl(
50 // Note: It is correct and expected to call Init more than once, as connection 26 base::TimeDelta read_timeout,
51 // issues are encountered and new connections must be made. 27 const ProtoReceivedCallback& read_callback,
52 void Init(scoped_ptr<net::StreamSocket> socket, 28 const ProtoSentCallback& write_callback,
53 const google::protobuf::MessageLite& login_request, 29 const ConnectionChangedCallback& connection_callback);
54 const ProtoReceivedCallback& read_callback, 30 virtual ~ConnectionHandlerImpl();
55 const ProtoSentCallback& write_callback,
56 const ConnectionChangedCallback& connection_callback);
57 31
58 // Checks that a handshake has been completed and a message is not already 32 // ConnectionHandler implementation.
59 // in flight. 33 virtual void Init(const mcs_proto::LoginRequest& login_request,
60 bool CanSendMessage() const; 34 scoped_ptr<net::StreamSocket> socket) OVERRIDE;
61 35 virtual bool CanSendMessage() const OVERRIDE;
62 // Send an MCS protobuf message. CanSendMessage() must be true. 36 virtual void SendMessage(const google::protobuf::MessageLite& message)
63 void SendMessage(const google::protobuf::MessageLite& message); 37 OVERRIDE;
64 38
65 private: 39 private:
66 // State machine for handling incoming data. See WaitForData(..) for usage. 40 // State machine for handling incoming data. See WaitForData(..) for usage.
67 enum ProcessingState { 41 enum ProcessingState {
68 // Processing the version, tag, and size packets (assuming minimum length 42 // Processing the version, tag, and size packets (assuming minimum length
69 // size packet). Only used during the login handshake. 43 // size packet). Only used during the login handshake.
70 MCS_VERSION_TAG_AND_SIZE = 0, 44 MCS_VERSION_TAG_AND_SIZE = 0,
71 // Processing the tag and size packets (assuming minimum length size 45 // Processing the tag and size packets (assuming minimum length size
72 // packet). Used for normal messages. 46 // packet). Used for normal messages.
73 MCS_TAG_AND_SIZE, 47 MCS_TAG_AND_SIZE,
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 // Closes the current connection. 83 // Closes the current connection.
110 void CloseConnection(); 84 void CloseConnection();
111 85
112 // Timeout policy: the timeout is only enforced while waiting on the 86 // Timeout policy: the timeout is only enforced while waiting on the
113 // handshake (version and/or LoginResponse) or once at least a tag packet has 87 // handshake (version and/or LoginResponse) or once at least a tag packet has
114 // been received. It is reset every time new data is received, and is 88 // been received. It is reset every time new data is received, and is
115 // only stopped when a full message is processed. 89 // only stopped when a full message is processed.
116 // TODO(zea): consider enforcing a separate timeout when waiting for 90 // TODO(zea): consider enforcing a separate timeout when waiting for
117 // a message to send. 91 // a message to send.
118 const base::TimeDelta read_timeout_; 92 const base::TimeDelta read_timeout_;
119 base::OneShotTimer<ConnectionHandler> read_timeout_timer_; 93 base::OneShotTimer<ConnectionHandlerImpl> read_timeout_timer_;
120 94
121 // This connection's socket and the input/output streams attached to it. 95 // This connection's socket and the input/output streams attached to it.
122 scoped_ptr<net::StreamSocket> socket_; 96 scoped_ptr<net::StreamSocket> socket_;
123 scoped_ptr<SocketInputStream> input_stream_; 97 scoped_ptr<SocketInputStream> input_stream_;
124 scoped_ptr<SocketOutputStream> output_stream_; 98 scoped_ptr<SocketOutputStream> output_stream_;
125 99
126 // Whether the MCS login handshake has successfully completed. See Init(..) 100 // Whether the MCS login handshake has successfully completed. See Init(..)
127 // description for more info on what the handshake involves. 101 // description for more info on what the handshake involves.
128 bool handshake_complete_; 102 bool handshake_complete_;
129 103
130 // State for the message currently being processed, if there is one. 104 // State for the message currently being processed, if there is one.
131 uint8 message_tag_; 105 uint8 message_tag_;
132 uint32 message_size_; 106 uint32 message_size_;
133 107
134 ProtoReceivedCallback read_callback_; 108 ProtoReceivedCallback read_callback_;
135 ProtoSentCallback write_callback_; 109 ProtoSentCallback write_callback_;
136 ConnectionChangedCallback connection_callback_; 110 ConnectionChangedCallback connection_callback_;
137 111
138 base::WeakPtrFactory<ConnectionHandler> weak_ptr_factory_; 112 base::WeakPtrFactory<ConnectionHandlerImpl> weak_ptr_factory_;
139 113
140 DISALLOW_COPY_AND_ASSIGN(ConnectionHandler); 114 DISALLOW_COPY_AND_ASSIGN(ConnectionHandlerImpl);
141 }; 115 };
142 116
143 } // namespace gcm 117 } // namespace gcm
144 118
145 #endif // GOOGLE_APIS_GCM_ENGINE_CONNECTION_HANDLER_H_ 119 #endif // GOOGLE_APIS_GCM_ENGINE_CONNECTION_HANDLER_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698