Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit 1f1f47a

Browse files
authored
Merge pull request #3 from cyanray/new_http_library
使用 httplib 替代 libcurl。
2 parents bc16fc4 + 46cb745 commit 1f1f47a

File tree

9 files changed

+5482
-768
lines changed

9 files changed

+5482
-768
lines changed

CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ project (mirai-cpp VERSION 0.4.4)
44

55
set(CMAKE_CXX_STANDARD 11)
66

7-
if(CMAKE_TOOLCHAIN_FILE)
8-
include(${CMAKE_TOOLCHAIN_FILE})
9-
endif(CMAKE_TOOLCHAIN_FILE)
10-
11-
find_package(CURL CONFIG REQUIRED)
12-
137

148
if(MSVC)
159
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
@@ -18,7 +12,6 @@ if(MSVC)
1812
endif(MSVC)
1913

2014

21-
2215
option(BUILD_SHARED_LIBS "Build ${PROJECT_NAME} as a shared library." OFF)
2316
if(BUILD_SHARED_LIBS)
2417
set(LIBRARY_TYPE SHARED)
@@ -37,7 +30,6 @@ target_include_directories(${PROJECT_NAME} PUBLIC
3730
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third-party>
3831
$<INSTALL_INTERFACE:third-party>
3932
)
40-
target_link_libraries(${PROJECT_NAME} CURL::libcurl)
4133

4234
if(BUILD_SHARED_LIBS)
4335
if(WIN32)

examples/RichMessage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int main()
4747
{
4848
try
4949
{
50-
gm.Reply(MessageChain().FlashImage(img));
50+
gm.Reply(MessageChain().FlashImage(gImg));
5151
}
5252
catch (const std::exception& ex)
5353
{

include/events/member_mute_event.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ namespace Cyan
2424
DurationSeconds = gm.DurationSeconds;
2525
Member = gm.Member;
2626
Operator = gm.Operator;
27+
operator_is_null_ = gm.operator_is_null_;
2728
}
2829
MemberMuteEvent& operator=(const MemberMuteEvent& t)
2930
{
3031
MemberMuteEvent tmp(t);
3132
std::swap(this->DurationSeconds, tmp.DurationSeconds);
3233
std::swap(this->Member, tmp.Member);
3334
std::swap(this->Operator, tmp.Operator);
35+
std::swap(this->operator_is_null_, tmp.operator_is_null_);
3436
return *this;
3537
}
3638

@@ -39,12 +41,21 @@ namespace Cyan
3941
this->bot_ = bot;
4042
}
4143

44+
bool OperatorIsBot() const
45+
{
46+
return operator_is_null_;
47+
}
48+
4249
virtual ~MemberMuteEvent() = default;
4350
virtual bool Set(const json& j) override
4451
{
4552
this->DurationSeconds = j["durationSeconds"].get<int>();
4653
this->Member.Set(j["member"]);
47-
this->Operator.Set(j["operator"]);
54+
if (!j["operator"].is_null())
55+
{
56+
this->Operator.Set(j["operator"]);
57+
this->operator_is_null_ = false;
58+
}
4859
return true;
4960
}
5061
virtual json ToJson() const override
@@ -59,6 +70,7 @@ namespace Cyan
5970

6071
private:
6172
MiraiBot* bot_;
73+
bool operator_is_null_ = true;
6274
};
6375

6476
}

include/events/member_unmute_event.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ namespace Cyan
2222
{
2323
Member = gm.Member;
2424
Operator = gm.Operator;
25+
operator_is_null_ = gm.operator_is_null_;
2526
}
2627
MemberUnmuteEvent& operator=(const MemberUnmuteEvent& t)
2728
{
2829
MemberUnmuteEvent tmp(t);
2930
std::swap(this->Member, tmp.Member);
3031
std::swap(this->Operator, tmp.Operator);
32+
std::swap(this->operator_is_null_, tmp.operator_is_null_);
3133
return *this;
3234
}
3335

@@ -36,11 +38,20 @@ namespace Cyan
3638
this->bot_ = bot;
3739
}
3840

41+
bool OperatorIsBot() const
42+
{
43+
return operator_is_null_;
44+
}
45+
3946
virtual ~MemberUnmuteEvent() = default;
4047
virtual bool Set(const json& j) override
4148
{
4249
this->Member.Set(j["member"]);
43-
this->Operator.Set(j["operator"]);
50+
if (!j["operator"].is_null())
51+
{
52+
this->Operator.Set(j["operator"]);
53+
this->operator_is_null_ = false;
54+
}
4455
return true;
4556
}
4657
virtual json ToJson() const override
@@ -54,6 +65,7 @@ namespace Cyan
5465

5566
private:
5667
MiraiBot* bot_;
68+
bool operator_is_null_ = true;
5769
};
5870

5971
}

include/mirai_bot.hpp

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212

1313
#include "ThreadPool.h"
1414
#include "nlohmann/json.hpp"
15-
#include "CURLWrapper.h"
15+
16+
// fuck windows.h
17+
#ifdef max
18+
#undef max
19+
#endif
20+
#include "httplib.h"
21+
22+
1623
#include "defs/defs.hpp"
1724
#include "events/events.hpp"
1825
#include "exported.h"
@@ -40,14 +47,8 @@ namespace Cyan
4047
class EXPORTED MiraiBot
4148
{
4249
public:
43-
MiraiBot() :pool_(4), qq_(0) {}
44-
MiraiBot(const string& host, int port) : pool_(4), qq_(0)
45-
{
46-
stringstream ss;
47-
ss << "http://" << host << ":" << port;
48-
api_url_prefix_ = ss.str();
49-
}
50-
MiraiBot(const string& url_prefix) :api_url_prefix_(url_prefix), pool_(4), qq_(0) {}
50+
MiraiBot() :qq_(0), pool_(4), http_client_("localhost", 8080) {}
51+
MiraiBot(const string& host, int port) : qq_(0), pool_(4), http_client_(host, port) {}
5152
~MiraiBot()
5253
{
5354
Release();
@@ -56,9 +57,10 @@ namespace Cyan
5657
{
5758
return sessionKey_;
5859
}
59-
string GetApiUrlPrefix() const
60+
61+
httplib::Client* GetHttpClient()
6062
{
61-
return api_url_prefix_;
63+
return &(this->http_client_);
6264
}
6365

6466
bool Auth(const string& authKey, QQ_t qq);
@@ -110,10 +112,9 @@ namespace Cyan
110112
void EventLoop();
111113

112114
private:
113-
bool SessionVerify() const;
114-
bool SessionRelease() const;
115+
bool SessionVerify();
116+
bool SessionRelease();
115117
unsigned int FetchMessagesAndEvents(unsigned int count = 10);
116-
117118
template<typename T>
118119
inline WeakEvent MakeWeakEvent(const json& json_)
119120
{
@@ -122,7 +123,6 @@ namespace Cyan
122123
e->Set(json_);
123124
return std::dynamic_pointer_cast<Serializable>(e);
124125
}
125-
126126
WeakEvent CreateEvent(MiraiEvent mirai_event, const json& json_);
127127
bool Release() noexcept
128128
{
@@ -136,10 +136,24 @@ namespace Cyan
136136
}
137137

138138
}
139+
140+
// 因为 httplib 使用 string 来保存文件内容,这里适配一下
141+
inline string ReadFile(const string& filename)
142+
{
143+
std::ifstream ifs(filename, std::ifstream::binary);
144+
std::filebuf* pbuf = ifs.rdbuf();
145+
std::size_t size = pbuf->pubseekoff(0, ifs.end, ifs.in);
146+
pbuf->pubseekpos(0, ifs.in);
147+
string result(size, '\0');
148+
pbuf->sgetn(&result[0], size);
149+
ifs.close();
150+
return result;
151+
}
152+
139153
string authKey_;
140154
QQ_t qq_;
141155
string sessionKey_;
142-
string api_url_prefix_ = "http://127.0.0.1:8080";
156+
httplib::Client http_client_;
143157
unordered_map<MiraiEvent, function<void(WeakEvent)> > processors_;
144158
ThreadPool pool_;
145159
};

src/event_func.cpp

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "mirai_bot.hpp"
22
#include "events/events.hpp"
3+
#include "httplib.h"
34

45
namespace Cyan
56
{
@@ -41,51 +42,52 @@ namespace Cyan
4142

4243
bool NewFriendRequestEvent::Respose(int operate, const string& message)
4344
{
44-
static const string api_url = bot_->GetApiUrlPrefix() + "/resp/newFriendRequestEvent";
45-
46-
json j;
47-
j["sessionKey"] = bot_->GetSessionKey();
48-
j["eventId"] = this->EventId;
49-
j["fromId"] = (int64_t)this->FromId;
50-
j["groupId"] = (int64_t)this->GroupId;
51-
j["operate"] = operate;
52-
j["message"] = message;
53-
54-
string pData = j.dump();
55-
HTTP http; http.SetContentType("application/json;charset=UTF-8");
56-
auto res = http.Post(api_url, pData);
57-
58-
if (res.Ready)
45+
json data =
46+
{
47+
{ "sessionKey", bot_->GetSessionKey() },
48+
{ "eventId", this->EventId},
49+
{ "fromId", (int64_t)this->FromId},
50+
{ "groupId", (int64_t)this->GroupId},
51+
{ "operate", operate},
52+
{ "message", message},
53+
};
54+
55+
httplib::Client& http_client = *(bot_->GetHttpClient());
56+
auto res = http_client.Post("/resp/newFriendRequestEvent", data.dump(), "application/json;charset=UTF-8");
57+
if (res)
5958
{
59+
if (res->status != 200)
60+
throw std::runtime_error("[mirai-api-http error]: " + res->body);
6061
return true;
6162
}
6263
else
63-
throw runtime_error(res.ErrorMsg);
64+
throw runtime_error("网络错误");
6465

6566
}
6667

6768
bool MemberJoinRequestEvent::Respose(int operate, const string& message)
6869
{
69-
static const string api_url = bot_->GetApiUrlPrefix() + "/resp/memberJoinRequestEvent";
7070

71-
json j;
72-
j["sessionKey"] = bot_->GetSessionKey();
73-
j["eventId"] = this->EventId;
74-
j["fromId"] = (int64_t)this->FromId;
75-
j["groupId"] = (int64_t)this->GroupId;
76-
j["operate"] = operate;
77-
j["message"] = message;
78-
79-
string pData = j.dump();
80-
HTTP http; http.SetContentType("application/json;charset=UTF-8");
81-
auto res = http.Post(api_url, pData);
82-
83-
if (res.Ready)
71+
json data =
72+
{
73+
{ "sessionKey", bot_->GetSessionKey() },
74+
{ "eventId", this->EventId},
75+
{ "fromId", (int64_t)this->FromId},
76+
{ "groupId", (int64_t)this->GroupId},
77+
{ "operate", operate},
78+
{ "message", message},
79+
};
80+
81+
httplib::Client& http_client = *(bot_->GetHttpClient());
82+
auto res = http_client.Post("/resp/memberJoinRequestEvent", data.dump(), "application/json;charset=UTF-8");
83+
if (res)
8484
{
85+
if (res->status != 200)
86+
throw std::runtime_error("[mirai-api-http error]: " + res->body);
8587
return true;
8688
}
8789
else
88-
throw runtime_error(res.ErrorMsg);
90+
throw runtime_error("网络错误");
8991

9092
}
9193

0 commit comments

Comments
 (0)