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
3 // found in the LICENSE file.
5 #ifndef GOOGLE_APIS_GCM_ENGINE_CONNECTION_HANDLER_H_
6 #define GOOGLE_APIS_GCM_ENGINE_CONNECTION_HANDLER_H_
8 #include "base/callback.h"
9 #include "google_apis/gcm/base/gcm_export.h"
18 } // namespace protobuf
27 class SocketInputStream
;
28 class SocketOutputStream
;
30 // Handles performing the protocol handshake and sending/receiving protobuf
31 // messages. Note that no retrying or queueing is enforced at this layer.
32 // Once a connection error is encountered, the ConnectionHandler will disconnect
33 // the socket and must be reinitialized with a new StreamSocket before
34 // messages can be sent/received again.
35 class GCM_EXPORT ConnectionHandler
{
37 typedef base::Callback
<void(scoped_ptr
<google::protobuf::MessageLite
>)>
38 ProtoReceivedCallback
;
39 typedef base::Closure ProtoSentCallback
;
40 typedef base::Callback
<void(int)> ConnectionChangedCallback
;
43 virtual ~ConnectionHandler();
45 // Starts a new MCS connection handshake (using |login_request|) and, upon
46 // success, begins listening for incoming/outgoing messages.
48 // Note: It is correct and expected to call Init more than once, as connection
49 // issues are encountered and new connections must be made.
50 virtual void Init(const mcs_proto::LoginRequest
& login_request
,
51 net::StreamSocket
* socket
) = 0;
53 // Resets the handler and any internal state. Should be called any time
54 // a connection reset happens externally to the handler.
55 virtual void Reset() = 0;
57 // Checks that a handshake has been completed and a message is not already
59 virtual bool CanSendMessage() const = 0;
61 // Send an MCS protobuf message. CanSendMessage() must be true.
62 virtual void SendMessage(const google::protobuf::MessageLite
& message
) = 0;
67 #endif // GOOGLE_APIS_GCM_ENGINE_CONNECTION_HANDLER_H_