blob: 3c8de6909026d3ae66677ecc7f4f7caf56cd8bc7 [file] [log] [blame]
sergeyuf1005f62016-02-03 21:11:301// 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_PROTOCOL_MESSAGE_PIPE_H_
6#define REMOTING_PROTOCOL_MESSAGE_PIPE_H_
7
dcheng0765c492016-04-06 22:41:538#include <memory>
9
sergeyuf1005f62016-02-03 21:11:3010#include "base/callback_forward.h"
sergeyuf1005f62016-02-03 21:11:3011
12namespace google {
13namespace protobuf {
14class MessageLite;
15} // namespace protobuf
16} // namespace google
17
18namespace remoting {
19
20class CompoundBuffer;
21
22namespace protocol {
23
24// Represents a bi-directional pipe that allows to send and receive messages.
25class MessagePipe {
26 public:
sergeyud059c462016-07-20 19:34:1027 class EventHandler {
28 public:
sergeyucc40af912016-07-22 03:49:1929 // Called when the channel is open.
30 virtual void OnMessagePipeOpen() = 0;
31
sergeyud059c462016-07-20 19:34:1032 // Called when a message is received.
33 virtual void OnMessageReceived(std::unique_ptr<CompoundBuffer> message) = 0;
34
35 // Called when the channel is closed.
36 virtual void OnMessagePipeClosed() = 0;
37
38 protected:
39 virtual ~EventHandler() {}
40 };
sergeyuf1005f62016-02-03 21:11:3041
42 virtual ~MessagePipe() {}
43
sergeyucc40af912016-07-22 03:49:1944 // Starts the channel. Must be called immediately after MessagePipe is
45 // created. |event_handler| will be notified when state of the pipe changes or
46 // when a message is received.
sergeyud059c462016-07-20 19:34:1047 virtual void Start(EventHandler* event_handler) = 0;
sergeyuf1005f62016-02-03 21:11:3048
49 // Sends a message. |done| is called when the message has been sent to the
50 // client, but it doesn't mean that the client has received it. |done| is
51 // never called if the message is never sent (e.g. if the pipe is destroyed
52 // before the message is sent).
Zijie He55f07c02017-08-31 17:25:4953 // |message| is guaranteed to be used synchronously. It won't be referred
54 // after this function returns.
55 // TODO(zijiehe): the |message| should be const-ref.
sergeyuf1005f62016-02-03 21:11:3056 virtual void Send(google::protobuf::MessageLite* message,
57 const base::Closure& done) = 0;
58};
59
60} // namespace protocol
61} // namespace remoting
62
63#endif // REMOTING_PROTOCOL_MESSAGE_PIPE_H_