cpp-mirai-client  v2.6.1
cpp client for mirai-api-http
MiraiClientEvents.hpp
浏览该文件的文档.
1// Copyright (C) 2022 Numendacil and contributors
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU Affero General Public License as
5// published by the Free Software Foundation, either version 3 of the
6// License, or (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU Affero General Public License for more details.
12//
13// You should have received a copy of the GNU Affero General Public License
14// along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16#ifndef MIRAI_MIRAI_CLIENT_EVENTS_HPP_
17#define MIRAI_MIRAI_CLIENT_EVENTS_HPP_
18
19#include <exception>
20#include <map>
21#include <string>
22
26
27namespace Mirai
28{
29
30/**
31 * @brief 连接建立事件
32 *
33 * 会在连接成功建立后, `MiraiClient::Connect()` 返回之前广播。
34 * `MiraiClient` 的mirai相关api应在此事件之后才能被正常使用。
35 */
36struct ClientConnectionEstablishedEvent final : IEvent<ClientConnectionEstablishedEvent>
37{
38 /// 连接地址
39 std::string uri;
40 /// 接收的文件头信息
41 std::map<std::string, std::string> headers;
42 /// 协议信息
43 std::string protocol;
44 /// 本次连接的session key
45 std::string SessionKey;
46 /// 获取到的的Bot资料
48
49private:
52};
53
54/**
55 * @brief 连接错误事件
56 *
57 * 通常出现于网络问题导致的无法连接目标服务器。
58 * 连接建立后出现的错误将导致 `ClientConnectionClosedEvent`
59 */
60struct ClientConnectionErrorEvent final: public IEvent<ClientConnectionErrorEvent>
61{
62 /// 当前重试次数
63 size_t RetryCount = 0;
64 /// 下一次重试前的等待时间(ms)
65 double WaitTime = 0;
66 /// Http连接状态
67 int HttpStatus = 0;
68 /// 错误原因
69 std::string reason;
70 /// 是否为解压缩编码错误
71 bool DecompressionError = false;
72
73private:
75 static constexpr EventTypes TYPE_ = EventTypes::ClientConnectionError;
76};
77
78/**
79 * @brief 连接丢失事件
80 *
81 * 只可能在 `ClientConnectionEstablishedEvent` 之后发生,表明已建立的连接因为某种原因关闭了。
82 * 若是 `MiraiClient::Disconnect()` 导致的连接关闭,该事件将会在函数返回之前被广播。
83 * 连接关闭后不再应该进行任何mirai api的使用,直到下次连接建立接收到 `ClientConnectionEstablishedEvent` 为止。
84 * 若开启了自动重连选项,`MiraiClient` 会在非客户端主动关闭导致的连接丢失后尝试自动重连。重连过程中的错误将会触发 `ClientConnectionErrorEvent`
85 */
86struct ClientConnectionClosedEvent final: public IEvent<ClientConnectionClosedEvent>
87{
88 /// 信息码
89 uint16_t code;
90 /// 连接关闭原因
91 std::string reason;
92 /// 是否由远程服务器关闭的连接
93 bool remote;
94
95private:
97 static constexpr EventTypes TYPE_ = EventTypes::ClientConnectionClosed;
98};
99
100/**
101 * @brief 消息解析错误事件
102 *
103 * 从mirai-api-http接收到了格式错误的信息,可能是版本不同或bug导致
104 */
105struct ClientParseErrorEvent final: public IEvent<ClientParseErrorEvent>
106{
107 /// 错误消息
109 /// 接收到的原消息
110 std::string message;
111
112private:
114 static constexpr EventTypes TYPE_ = EventTypes::ClientParseError;
115};
116
117
118
119#define DECLARE_TYPE_ENUM(event_type) \
120 template<> struct GetEventType<event_type::GetType()> \
121 { \
122 using type = event_type; \
123 }
124
129
130#undef DECLARE_TYPE_ENUM
131
132} // namespace Mirai
133
134#endif
Common interface for all event types
Definition: IEvent.hpp:34
消息解析错误
Definition: Exceptions.hpp:100
所有mirai相关的对象的命名空间
DECLARE_TYPE_ENUM(BotOfflineEventActive)
std::string reason
连接关闭原因
bool remote
是否由远程服务器关闭的连接
bool DecompressionError
是否为解压缩编码错误
size_t RetryCount
当前重试次数
double WaitTime
下一次重试前的等待时间(ms)
std::string SessionKey
本次连接的session key
std::map< std::string, std::string > headers
接收的文件头信息
User BotProfile
获取到的的Bot资料
消息解析错误事件
ParseError error
错误消息
std::string message
接收到的原消息
QQ用户
Definition: BasicTypes.hpp:305