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. 4 5 #ifndef GOOGLE_APIS_GCM_ENGINE_CONNECTION_HANDLER_H_ 6 #define GOOGLE_APIS_GCM_ENGINE_CONNECTION_HANDLER_H_ 7 8 #include "base/callback.h" 9 #include "google_apis/gcm/base/gcm_export.h" 10 11 namespace net{ 12 class StreamSocket; 13 } // namespace net 14 15 namespace google { 16 namespace protobuf { 17 class MessageLite; 18 } // namespace protobuf 19 } // namepsace google 20 21 namespace mcs_proto { 22 class LoginRequest; 23 } 24 25 namespace gcm { 26 27 class SocketInputStream; 28 class SocketOutputStream; 29 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 { 36 public: 37 typedef base::Callback<void(scoped_ptr<google::protobuf::MessageLite>)> 38 ProtoReceivedCallback; 39 typedef base::Closure ProtoSentCallback; 40 typedef base::Callback<void(int)> ConnectionChangedCallback; 41 42 ConnectionHandler(); 43 virtual ~ConnectionHandler(); 44 45 // Starts a new MCS connection handshake (using |login_request|) and, upon 46 // success, begins listening for incoming/outgoing messages. 47 // 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; 52 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; 56 57 // Checks that a handshake has been completed and a message is not already 58 // in flight. 59 virtual bool CanSendMessage() const = 0; 60 61 // Send an MCS protobuf message. CanSendMessage() must be true. 62 virtual void SendMessage(const google::protobuf::MessageLite& message) = 0; 63 }; 64 65 } // namespace gcm 66 67 #endif // GOOGLE_APIS_GCM_ENGINE_CONNECTION_HANDLER_H_ 68