OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 BLIMP_NET_BLIMP_MESSAGE_PUMP_H_ | |
6 #define BLIMP_NET_BLIMP_MESSAGE_PUMP_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "base/macros.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "blimp/net/blimp_net_export.h" | |
14 #include "net/base/completion_callback.h" | |
15 | |
16 namespace net { | |
17 class GrowableIOBuffer; | |
18 } | |
19 | |
20 namespace blimp { | |
21 | |
22 class BlimpMessageProcessor; | |
23 class ConnectionErrorObserver; | |
24 class PacketReader; | |
25 | |
26 // Reads and deserializes incoming packets from |reader_|, and forwards parsed | |
27 // BlimpMessages to |processor_|. When |processor_| is ready to take the next | |
28 // message, the BlimpMessagePump reads the next packet. | |
29 class BLIMP_NET_EXPORT BlimpMessagePump { | |
30 public: | |
31 // Caller ensures that |reader| outlives this object. | |
32 explicit BlimpMessagePump(PacketReader* reader); | |
33 | |
34 ~BlimpMessagePump(); | |
35 | |
36 // Sets the processor which will take BlimpMessages. | |
37 // Caller retains the ownership of |processor|. | |
38 // The processor can be unset by passing a null pointer when no reads are | |
39 // inflight, ie. after |processor_|->ProcessMessage() is called, but before | |
40 // ProcessMessage() invokes its completion callback. | |
41 void SetMessageProcessor(BlimpMessageProcessor* processor); | |
42 | |
43 void set_error_observer(ConnectionErrorObserver* observer) { | |
44 error_observer_ = observer; | |
45 } | |
46 | |
47 private: | |
48 // Read next packet from |reader_|. | |
49 void ReadNextPacket(); | |
50 | |
51 // Callback when next packet is ready in |buffer_|. | |
52 void OnReadPacketComplete(int result); | |
53 | |
54 // Callback when |processor_| finishes processing a BlimpMessage. | |
55 // Any values other than net::OK indicate that |processor_| has encountered an | |
56 // error that should be handled. Currently all errors will cause the | |
57 // connection to be dropped; in the future we will need to add more | |
58 // sophisticated error handling logic here. | |
59 // TODO(kmarshall): Improve error handling. | |
60 void OnProcessMessageComplete(int result); | |
61 | |
62 PacketReader* reader_; | |
63 ConnectionErrorObserver* error_observer_ = nullptr; | |
64 BlimpMessageProcessor* processor_ = nullptr; | |
65 scoped_refptr<net::GrowableIOBuffer> buffer_; | |
66 bool read_inflight_ = false; | |
67 | |
68 // Used to abandon ProcessMessage completion callbacks if |this| is deleted. | |
69 base::WeakPtrFactory<BlimpMessagePump> weak_factory_; | |
70 | |
71 DISALLOW_COPY_AND_ASSIGN(BlimpMessagePump); | |
72 }; | |
73 | |
74 } // namespace blimp | |
75 | |
76 #endif // BLIMP_NET_BLIMP_MESSAGE_PUMP_H_ | |
OLD | NEW |