-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
111 lines (100 loc) · 3.89 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <QCoreApplication>
#include <QDebug>
#include "bcf.h"
using namespace std;
#define CHANNEL_ID_SERIALPORT 0
bcf::PackMode userPackMode = static_cast<bcf::PackMode>(bcf::PackMode::UNPACK_BY_USER + 1);
class UserProtocolModel : public bcf::AbstractProtocolModel
{
virtual bcf::PackMode protocolType() override
{
return userPackMode;
};
//your extend fields e.g. ByHeadProtocolModel
};
class UserProtocolBuilder : public bcf::IProtocolBuilder
{
protected:
virtual bcf::PackMode getType()const override
{
return userPackMode;
};
virtual std::shared_ptr<bb::ByteBuffer> build(std::shared_ptr<bcf::AbstractProtocolModel> _model)
override
{
//your build... e.g. ByHeadProtocolBuilder
return nullptr;
};
};
class UserProtocolParser : public bcf::IProtocolParser
{
public:
virtual bcf::PackMode getType()const override
{
return userPackMode;
};
virtual void parse(const
std::function<void(ParserState, std::shared_ptr<bcf::AbstractProtocolModel>)>& callback)
override
{
std::shared_ptr<bcf::ByHeadProtocolModel> model = std::make_shared<bcf::ByHeadProtocolModel>();
//your parse... e.g. ByHeadProtocolParser
callback(ParserState::OK, model);
};
};
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
auto requestPtr = bcf::RequestHandlerBuilder()
.withTimeOut(10'000)
.withProtocolBuilders({std::make_shared<ByHeadProtocolBuilder>(),
std::make_shared<UserProtocolBuilder>()})
.withProtocolParsers({std::make_shared<ByHeadProtocolParser>()})
.withAbandonCallback([](std::shared_ptr<bcf::AbstractProtocolModel> model) {})
#ifdef BCF_USE_QT_SERIALPORT
.withChannel(CHANNEL_ID_SERIALPORT, []() {
auto channel = std::make_shared<bcf::SerialChannel>();
// channel->setRawDataCallback([](std::shared_ptr<bb::ByteBuffer> bb) {
// bb->printHex();
// });
channel->setPortName("COM2");
return channel;
})
#endif
.withReceiveData([](std::shared_ptr<bcf::AbstractProtocolModel> model) {
qDebug() << "withReceiveData protocolType:" << model->protocolType();
if (model->protocolType() == bcf::PackMode::UNPACK_BY_LENGTH_FIELD) {
auto bmodel = std::dynamic_pointer_cast<bcf::ByHeadProtocolModel>(model);
if (bmodel) {
qDebug() << "withReceiveData retmodel seq: " << bmodel->seq ;
qDebug() << "withReceiveData retmodel body:" << QString::fromStdString(bmodel->body());
}
}
})
.withFailedCallback([]() {
std::cerr << "withFailedCallback" ;
})
.withConnectionCompletedCallback([](std::shared_ptr<bcf::IChannel> channel) {
qDebug() << "withConnectionCompletedCallback channelID:" << channel->channelID() ;
})
.connect();
std::shared_ptr<bcf::ByHeadProtocolModel> reqmodel = std::make_shared<bcf::ByHeadProtocolModel>();
reqmodel->seq = bcf::util::getNextSeq();
#define CMD_TEST_LOOP 1
reqmodel->cmd = CMD_TEST_LOOP;
reqmodel->setBody(std::string("this is " + std::to_string(reqmodel->seq) + " times request"));
requestPtr->request(reqmodel, [](bcf::ErrorCode code,
std::shared_ptr<bcf::AbstractProtocolModel> retmodel) {
auto bmodel = std::dynamic_pointer_cast<bcf::ByHeadProtocolModel>(retmodel);
if (bmodel) {
qDebug() << "retmodel seq: " << bmodel->seq ;
qDebug() << "retmodel body:" << QString::fromStdString(bmodel->body());
}
});
// std::string filename = "C:\\Users\\bridge\\Desktop\\image.bin";//902 字节
// requestPtr->sendFileWithYModel(filename, [](int progress) {
// qDebug() << "progress: " << progress ;
// }, [](bcf::TransmitStatus status) {
// });
return app.exec();
}