OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client 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 #ifndef DEBUGGER_RSP_RSP_PACKETIZER_H_ | |
5 #define DEBUGGER_RSP_RSP_PACKETIZER_H_ | |
6 | |
7 #include <deque> | |
8 #include <string> | |
9 #include "debugger/base/debug_blob.h" | |
10 | |
11 namespace rsp { | |
12 class PacketConsumerInterface; | |
13 | |
14 /// This class provides ability to convert byte stream into RSP messages. | |
15 /// | |
16 /// Byte stream contain messages in RSP wire format, see | |
17 /// http://sources.redhat.com/gdb/current/onlinedocs/gdb.html#Remote-Protocol | |
18 /// Packetizer removes RSP 'envelope', i.e. start character, stop character, | |
19 /// checksum, converts escaped characters, expands run-encoded data. | |
20 /// | |
21 /// Example: | |
22 /// | |
23 /// class MyPacketConsumer : public rsp::PacketConsumerInterface { | |
24 /// ... | |
25 /// MyPacketConsumer consumer; | |
26 /// rsp::Packetizer packetizer; | |
27 /// packetizer.SetPacketConsumer(&consumer); | |
28 /// ... | |
29 /// char buff[100]; | |
30 /// size_t rd_bytes = 0; | |
31 /// while (ReadConnection(buff, &rd_bytes)) | |
32 /// packetizer.OnData(buff, &rd_bytes); | |
33 /// | |
34 class Packetizer { | |
35 public: | |
36 Packetizer(); | |
37 virtual ~Packetizer(); | |
38 | |
39 /// Associate |consumer| with packetizer. | |
40 /// Packetizer will call consumer->OnPacket when it receives complete | |
41 /// RSP packet. | |
42 /// @param[in] consumer externally created and maintained packet consumer. | |
43 /// Note: |consumer| is not owned by Packetizer, it's a weak reference. | |
44 /// |consumer| shall exist for the duration of Packetizer object. | |
45 virtual void SetPacketConsumer(PacketConsumerInterface* consumer); | |
46 | |
47 /// Consumes incoming RSP messages in GDB RSP wire format. | |
48 /// @param[in] data buffer with incoming bytes | |
49 /// @param[in] data_length size of |data| | |
50 /// Note: |OnData| will directly call one of these methods: | |
51 /// consumer_->OnPacket | |
52 /// consumer_->OnUnexpectedByte | |
53 /// consumer_->OnBreak | |
54 virtual void OnData(const void* data, size_t data_length); | |
55 | |
56 /// Same as previous member. | |
57 /// @param[in] data buffer with incoming bytes | |
58 virtual void OnData(const debug::Blob& data); | |
59 | |
60 /// Resets state, dropping all received data. | |
61 virtual void Reset(); | |
62 | |
63 private: | |
64 enum State { | |
65 IDLE, // in between packets | |
66 BODY, // packet start symbol is received ('$'), normal RSP packet body | |
67 END, // packet end symbol is received ('#'), expect checksum next | |
68 CHECKSUM, // first byte of checksum is received, expect one more | |
69 ESCAPE, // received an escape symbol | |
70 RUNLEN // received a run-length encoding symbol, expect count byte next | |
71 }; | |
72 | |
73 virtual void OnByte(uint8_t c); | |
74 virtual void AddByteToChecksum(uint8_t c); | |
75 virtual void AddByteToBody(uint8_t c); | |
76 | |
77 /// Adds |n| copies of last byte in |body_|. | |
78 virtual void AddRepeatedBytes(size_t n); | |
79 | |
80 State state_; | |
81 PacketConsumerInterface* consumer_; | |
82 debug::Blob body_; | |
83 unsigned int calculated_checksum_; | |
84 unsigned int recv_checksum_; | |
85 }; | |
86 | |
87 /// This class represents interface to RSP packet consumer. | |
88 class PacketConsumerInterface { | |
89 public: | |
90 PacketConsumerInterface() {} | |
91 virtual ~PacketConsumerInterface() {} | |
92 | |
93 /// Handler for received RSP packet. | |
94 /// @param[in] body body of RSP packet, with 'envelope' removed | |
95 /// @param[in] valid_checksum true if checksum for the packet is correct | |
96 virtual void OnPacket(const debug::Blob& body, bool valid_checksum) = 0; | |
97 | |
98 /// This method get called when Packetizer encounter unexpected byte | |
99 virtual void OnUnexpectedByte(uint8_t unexpected_byte) = 0; | |
100 | |
101 /// This method get called when Packetizer encounter Ctl-C code, | |
102 /// meaning a "break" RSP command. | |
103 virtual void OnBreak() = 0; | |
104 }; | |
105 | |
106 } // namespace rsp | |
107 | |
108 #endif // DEBUGGER_RSP_RSP_PACKETIZER_H_ | |
109 | |
OLD | NEW |