Skip to content

Commit 7bcfaf6

Browse files
committed
DTXMessageParserTest ParseIncomingBytes_0
1 parent fefe174 commit 7bcfaf6

22 files changed

+575
-64
lines changed

CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ set(HEADERS
4444
include/idevice/dtxchannel.h
4545
include/idevice/dtxtransport.h
4646
include/idevice/dtxprimitivearray.h
47+
include/idevice/macro_def.h
48+
include/idevice/macro_undef.h
4749
)
4850
set(SOURCES
4951
src/instrument.cpp
@@ -74,6 +76,7 @@ add_executable(
7476
test/idevice_test.cpp
7577
test/instrument_test.cpp
7678
test/dtxprimitivearray_test.cpp
79+
test/dtxmessageparser_test.cpp
7780
)
7881
target_link_libraries(
7982
${PROJECT_NAME}_test
@@ -84,6 +87,16 @@ add_definitions(-DENABLE_NSKEYEDARCHIVE_TEST)
8487
include(GoogleTest)
8588
gtest_discover_tests(${PROJECT_NAME}_test)
8689

90+
# tools
91+
add_executable(
92+
${PROJECT_NAME}_decoder
93+
tools/decoder.cpp
94+
)
95+
target_link_libraries(
96+
${PROJECT_NAME}_decoder
97+
${PROJECT_NAME}
98+
)
99+
87100
# asan
88101
#target_compile_options(${PROJECT_NAME} PUBLIC -fsanitize=address -fno-omit-frame-pointer)
89102
#set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS -fsanitize=address)

include/idevice/dtxchannel.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@
88

99
namespace idevice {
1010

11-
class DTXChannel : public DTXMessenger {
11+
class DTXChannel /*: public DTXMessenger */ {
1212
public:
1313
DTXChannel(DTXMessenger* connection, const std::string& label, uint32_t channel_identifier) : connection_(connection), label_(label), channel_identifier_(channel_identifier) {}
14+
DTXChannel() : DTXChannel(nullptr, "", 0) {}
1415
virtual ~DTXChannel() {}
1516

1617
// virtual bool SendMessageSync(std::shared_ptr<DTXMessage> msg, ReplyHandler callback) override;
17-
virtual void SendMessageAsync(std::shared_ptr<DTXMessage> msg, ReplyHandler callback) override;
18+
void SendMessageAsync(std::shared_ptr<DTXMessage> msg, DTXMessenger::ReplyHandler callback);
1819

1920
const std::string& Label() const { return label_; }
2021
uint32_t ChannelIdentifier() const { return channel_identifier_; }
2122

2223
private:
23-
std::string label_;
24-
uint32_t channel_identifier_;
25-
DTXMessenger* connection_;
24+
std::string label_ = "";
25+
uint32_t channel_identifier_ = 0;
26+
DTXMessenger* connection_ = nullptr;
2627

2728
}; // class DTXChannel
2829

include/idevice/dtxconnection.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <thread>
55
#include <memory> // std::unique_ptr, std::shared_ptr
66
#include <atomic>
7+
#include <utility> // std::pair
78
#include <unordered_map>
89

910
#include "idevice/blockingqueue.h"
@@ -19,6 +20,8 @@ namespace idevice {
1920
class DTXConnection : public DTXMessenger {
2021
public:
2122
using ChannelIdentifier = uint32_t;
23+
using MessageIdentifier = uint32_t;
24+
using DTXMessageWithRoutingInfo = std::pair<std::shared_ptr<DTXMessage>, DTXMessageRoutingInfo>;
2225

2326
DTXConnection(DTXTransport* transport) : transport_(transport) {}
2427
virtual ~DTXConnection() {}
@@ -31,7 +34,7 @@ class DTXConnection : public DTXMessenger {
3134
bool CannelChannel(std::shared_ptr<DTXChannel> channel);
3235

3336
// virtual bool SendMessageSync(std::shared_ptr<DTXMessage> msg, ReplyHandler callback) override;
34-
virtual void SendMessageAsync(std::shared_ptr<DTXMessage> msg, ReplyHandler callback) override;
37+
virtual void SendMessageAsync(std::shared_ptr<DTXMessage> msg, const DTXChannel& channel, ReplyHandler callback) override;
3538

3639
private:
3740
struct Packet {
@@ -60,15 +63,19 @@ class DTXConnection : public DTXMessenger {
6063
std::atomic_bool parsing_thread_running_ = ATOMIC_VAR_INIT(false);
6164
std::unique_ptr<std::thread> parsing_thread_ = nullptr; ///< consumer of incoming packets
6265

63-
BlockingQueue<std::shared_ptr<DTXMessage>> send_queue_;
66+
BlockingQueue<DTXMessageWithRoutingInfo> send_queue_;
6467
BlockingQueue<std::unique_ptr<Packet>> receive_queue_;
6568

66-
std::atomic<ChannelIdentifier> next_channel_code_ = ATOMIC_VAR_INIT(0);
69+
std::atomic<ChannelIdentifier> next_channel_code_ = ATOMIC_VAR_INIT(1);
6770
std::unordered_map<ChannelIdentifier, std::shared_ptr<DTXChannel>> channels_by_code_;
6871

72+
std::atomic<MessageIdentifier> next_msg_identifier = ATOMIC_VAR_INIT(1);
73+
6974
DTXTransport* transport_;
7075
DTXMessageParser incoming_parser_;
7176
DTXMessageTransmitter outgoing_transmitter_;
77+
78+
DTXChannel default_channel_;
7279

7380
}; // class DTXConnection
7481

include/idevice/dtxmessage.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ constexpr uint32_t kDTXMessageHeaderSize = sizeof(DTXMessageHeader);
2828
struct DTXMessageRoutingInfo {
2929
uint32_t identifier;
3030
uint32_t conversation_index;
31+
uint32_t channel_code;
3132
};
3233

3334
class DTXMessage {
@@ -78,6 +79,14 @@ class DTXMessage {
7879
void SetChannelCode(uint32_t channel_code) { channel_code_ = channel_code; }
7980
uint32_t ChannelCode() const { return channel_code_; }
8081

82+
size_t PayloadSize() const { return payload_size_; }
83+
char* PayloadBuffer() const { return payload_buffer_; }
84+
85+
void SetDeserialized(bool deserialized) { deserialized_ = deserialized; }
86+
bool Deserialized() const { return deserialized_; }
87+
88+
void Dump(bool dumphex = true) const;
89+
8190
private:
8291
void MaybeSerializeAuxiliaryObjects();
8392
void MaybeSerializePayloadObject();
@@ -86,13 +95,16 @@ class DTXMessage {
8695
char* payload_buffer_ = nullptr;
8796
size_t payload_size_ = 0;
8897
uint32_t message_type_ = 0;
89-
std::unique_ptr<DTXPrimitiveArray> auxiliary_;
98+
std::unique_ptr<DTXPrimitiveArray> auxiliary_ = nullptr;
9099
std::unordered_map<size_t, nskeyedarchiver::KAValue> auxiliary_objects_;
91100

101+
// DTXMessageRoutingInfo
92102
uint32_t identifier_ = 0;
93103
uint32_t conversation_index_ = 0;
94104
uint32_t channel_code_ = 0;
95105

106+
bool deserialized_ = false;
107+
96108
}; // class DTXMessage
97109

98110
} // namespace idevice

include/idevice/dtxmessageparser.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#ifndef IDEVICE_DTXMESSAGE_PARSER_H
22
#define IDEVICE_DTXMESSAGE_PARSER_H
33

4+
#include <queue>
5+
#include <vector>
6+
47
#include "idevice/idevice.h"
58
#include "idevice/dtxmessage.h"
69
#include "idevice/bytebuffer.h"
@@ -16,12 +19,15 @@ class DTXMessageParser {
1619

1720
bool ParseIncomingBytes(const char* buffer, size_t size);
1821

22+
std::vector<std::shared_ptr<DTXMessage>> PopAllParsedMessages();
23+
1924
private:
2025
//const char* Read(ByteReader& reader, size_t size, size_t* actual_size);
2126
bool ParseMessageWithHeader(const DTXMessageHeader& header, const char* data, size_t size);
2227

2328
bool eof_ = false;
2429
BufferMemory parsing_buffer_;
30+
std::queue<std::shared_ptr<DTXMessage>> parsed_message_queue_;
2531
}; // class DTXMessageParser
2632

2733
} // namespace idevice

include/idevice/dtxmessenger.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace idevice {
77

88
class DTXMessage;
9+
class DTXChannel;
910

1011
// Interface
1112
class DTXMessenger {
@@ -15,7 +16,7 @@ class DTXMessenger {
1516
virtual ~DTXMessenger() {}
1617

1718
// virtual bool SendMessageSync(std::shared_ptr<DTXMessage> msg, ReplyHandler callback) = 0;
18-
virtual void SendMessageAsync(std::shared_ptr<DTXMessage> msg, ReplyHandler callback) = 0;
19+
virtual void SendMessageAsync(std::shared_ptr<DTXMessage> msg, const DTXChannel& channel, ReplyHandler callback) = 0;
1920
};
2021

2122
} // namespace idevice

include/idevice/dtxprimitivearray.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
#include <cstdint> // int32_t, int64_t, uint64_t
55
#include <cstdlib> // malloc, free
66
#include <cstring> // memcpy
7+
#include <string>
78
#include <vector>
89
#include <memory> // std::unique_ptr
910

11+
#include "idevice/idevice.h" // hexdump
12+
#include "nskeyedarchiver/nskeyedunarchiver.hpp"
13+
1014
namespace idevice {
1115

1216
class DTXPrimitiveValue {
@@ -131,6 +135,49 @@ class DTXPrimitiveValue {
131135
Type GetType() const { return t_; }
132136
void SetType(Type t) { t_ = t; }
133137

138+
void Dump(bool dumphex = true) const {
139+
switch (t_) {
140+
case kNull:
141+
printf("[type=kNull, size=0, value=]\n");
142+
case kEmptyKey:
143+
printf("[type=kEmptyKey, size=0, value=]\n");
144+
break;
145+
case kString: {
146+
size_t str_len = Size();
147+
char* str = static_cast<char*>(malloc(str_len + 1));
148+
strncpy(str, d_.b, str_len);
149+
str[str_len] = '\0';
150+
printf("[type=kString, size=%zu, value=%s]\n", Size(), str);
151+
free(str);
152+
break;
153+
}
154+
case kBuffer: {
155+
if (dumphex) {
156+
hexdump(d_.b, Size(), 0);
157+
}
158+
nskeyedarchiver::KAValue value = nskeyedarchiver::NSKeyedUnarchiver::UnarchiveTopLevelObjectWithData(d_.b, Size());
159+
printf("[type=kBuffer, size=%zu, value=%s]\n", Size(), value.ToJson().c_str());
160+
break;
161+
}
162+
case kSignedInt32:
163+
printf("[type=kSignedInt32, size=%zu, value=%d]\n", Size(), d_.i32);
164+
break;
165+
case kSignedInt64:
166+
printf("[type=kSignedInt64, size=%zu, value=%lld]\n", Size(), d_.i64);
167+
break;
168+
case kFloat32:
169+
printf("[type=kFloat32, size=%zu, value=%f]\n", Size(), d_.f);
170+
break;
171+
case kFloat64:
172+
printf("[type=kFloat64, size=%zu, value=%f]\n", Size(), d_.d);
173+
break;
174+
case kInteger:
175+
printf("[type=kSignedInt64, size=%zu, value=%llu]\n", Size(), d_.u);
176+
default:
177+
break;
178+
}
179+
}
180+
134181
private:
135182
union {
136183
char* b; // kString or kBuffer
@@ -175,6 +222,8 @@ class DTXPrimitiveArray {
175222

176223
size_t Size() const { return items_.size(); }
177224

225+
void Dump(bool dumphex = true) const;
226+
178227
private:
179228
std::vector<DTXPrimitiveValue> items_;
180229
bool as_dict_ = false;

include/idevice/dtxtransport.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <cstring> // memcpy
77
#include <algorithm> // std::min
88

9+
#include "idevice/macro_def.h"
10+
#include "idevice/idevice.h" // hexdump
911
#include "idevice/instrument.h"
1012

1113
namespace idevice {
@@ -58,6 +60,10 @@ class BufferedDTXTransport {
5860
if (!Flush()) {
5961
return false;
6062
}
63+
#if IDEVICE_DEBUG
64+
IDEVICE_LOG_D("Send\n");
65+
hexdump((void*)(data), size, 0);
66+
#endif
6167
uint32_t actual_size = 0;
6268
return transport_->Send(data, size, &actual_size);
6369
}
@@ -82,6 +88,10 @@ class BufferedDTXTransport {
8288
return; // nothing to do
8389
}
8490
uint32_t actual_size = 0;
91+
#if IDEVICE_DEBUG
92+
IDEVICE_LOG_D("Flush\n");
93+
hexdump(buffer_, buffer_used_, 0);
94+
#endif
8595
bool ret = transport_->Send(buffer_, buffer_used_, &actual_size);
8696
buffer_used_ = 0;
8797
return ret;

include/idevice/macro_def.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef IDEVICE_MACRO_DEF_H
2+
#define IDEVICE_MACRO_DEF_H
3+
4+
// DEBUG
5+
#ifdef NDEBUG
6+
#define IDEVICE_DEBUG 0
7+
#else
8+
#define IDEVICE_DEBUG 1
9+
#endif
10+
11+
// LOG
12+
#define IDEVICE_LOG_LEVEL 2
13+
#if IDEVICE_LOG_LEVEL >= 0
14+
#define IDEVICE_LOG_E(fmt, ...) \
15+
fprintf(stderr, "[ERROR] %s:%d:%s(): " fmt, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
16+
#else
17+
#define IDEVICE_LOG_E(fmt, ...)
18+
#endif
19+
#if IDEVICE_LOG_LEVEL >= 1
20+
#define IDEVICE_LOG_I(fmt, ...) \
21+
printf("[INFO] %s:%d:%s(): " fmt, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
22+
#else
23+
#define IDEVICE_LOG_I(fmt, ...)
24+
#endif
25+
#if IDEVICE_LOG_LEVEL >= 2
26+
#define IDEVICE_LOG_D(fmt, ...) \
27+
printf("[DEBUG] %s:%d:%s(): " fmt, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
28+
#else
29+
#define IDEVICE_LOG_D(fmt, ...)
30+
#endif
31+
#if IDEVICE_LOG_LEVEL >= 3
32+
#define IDEVICE_LOG_V(fmt, ...) \
33+
printf("[VERBOSE] %s:%d:%s(): " fmt, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
34+
#else
35+
#define IDEVICE_LOG_V(fmt, ...)
36+
#endif
37+
38+
// ASSERT
39+
#define IDEVICE_ASSERT(exp, fmt, ...) if (!(exp)) { LOG_ERROR(fmt, ##__VA_ARGS__); } assert(exp)
40+
41+
42+
#endif // IDEVICE_MACRO_SCOPE_H

include/idevice/macro_undef.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef IDEVICE_MACRO_UNDEF_H
2+
#define IDEVICE_MACRO_UNDEF_H
3+
4+
// DEBUG
5+
#undef IDEVICE_DEBUG
6+
7+
// LOG
8+
#undef IDEVICE_LOG_LEVEL
9+
#undef IDEVICE_LOG_E
10+
#undef IDEVICE_LOG_I
11+
#undef IDEVICE_LOG_D
12+
#undef IDEVICE_LOG_V
13+
14+
// ASSERT
15+
#undef IDEVICE_ASSERT
16+
17+
#endif // IDEVICE_MACRO_UNDEF_H

0 commit comments

Comments
 (0)