mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-12-02 11:57:43 +08:00
Merge https://github.com/acl-dev/acl into HEAD
This commit is contained in:
commit
7ad4471ab3
BIN
doc/net-architecture.pptx
Normal file
BIN
doc/net-architecture.pptx
Normal file
Binary file not shown.
@ -1,5 +1,13 @@
|
||||
修改历史列表:
|
||||
|
||||
590) 2019.9.17
|
||||
590.1) bugfix: 在客户端模式下,需要在 http_aclient::ws_handshake() 中需要调用
|
||||
set_frame_masking_key() 设置客户端发送数据的掩码
|
||||
|
||||
589) 2019.9.16
|
||||
589.1) bugfix: aio_stream 类中 get_peer() / get_local() 返回有误
|
||||
589.2) bugfix: redis_pubsub::get_message() 添加读超时时间
|
||||
|
||||
588) 2019.8.22
|
||||
588.1) bugfix: socket_stream.cpp 中 get_ip() 方法在使用 std::string 时应该对
|
||||
源串判空,否则会引起崩溃
|
||||
|
@ -99,12 +99,10 @@ public:
|
||||
|
||||
/**
|
||||
* 当连接成功时的回调方法,子类可以实现本方法
|
||||
* @param checker {check_client&}
|
||||
* @param cost {double} 从发起连接请求到超时的时间间隔(秒)
|
||||
*/
|
||||
virtual void on_connected(const check_client& checker, double cost)
|
||||
virtual void on_connected(const check_client&, double cost)
|
||||
{
|
||||
(void) checker;
|
||||
(void) cost;
|
||||
}
|
||||
|
||||
|
@ -124,7 +124,7 @@ public:
|
||||
websocket& set_frame_payload_len(unsigned long long len);
|
||||
|
||||
/**
|
||||
* 设置数据帧数据的掩码值
|
||||
* 设置数据帧数据的掩码值,客户端模式下必须设置此项
|
||||
* @param mask {unsigned int}
|
||||
* @return {websocket&}
|
||||
*/
|
||||
|
@ -125,11 +125,13 @@ public:
|
||||
* store the message posted to the channel
|
||||
* @param message_type {string*} will store messsage or pmessage
|
||||
* @param pattern {string*} will store pattern set by psubscribe
|
||||
* @return {bool} 是否成功,如果返回 false 则表示出错
|
||||
* true on success, false on error
|
||||
* @param timeout {int} 当该值 >= 0 时,表示等待消息超时时间(秒)
|
||||
* when timeout >= 0, which was used as the waiting time for reading(second)
|
||||
* @return {bool} 是否成功,如果返回 false 则表示出错或超时
|
||||
* true on success, false on error or waiting timeout
|
||||
*/
|
||||
bool get_message(string& channel, string& msg,
|
||||
string* message_type = NULL, string* pattern = NULL);
|
||||
bool get_message(string& channel, string& msg, string* message_type = NULL,
|
||||
string* pattern = NULL, int timeout = -1);
|
||||
|
||||
/**
|
||||
* 列出当前的活跃频道:活跃频道指的是那些至少有一个订阅者的频道, 订阅模式的
|
||||
|
@ -216,9 +216,9 @@ public:
|
||||
aio_handle& get_handle(void) const;
|
||||
|
||||
/**
|
||||
* 注册读写流对象,内部会自动调用 hook->open 过程,如果成功,则返回之前注册的对象
|
||||
* (可能为NULL),若失败则返回与输入参数相同的指针,应用可以通过判断返回值与输入值
|
||||
* 是否相同来判断注册流对象是否成功
|
||||
* 注册读写流对象,内部自动调用 hook->open 过程,如果成功,则返回之前注册
|
||||
* 的对象(可能为NULL),若失败则返回与输入参数相同的指针,应用可以通过判断
|
||||
* 返回值与输入值是否相同来判断注册流对象是否成功
|
||||
* xxx: 在调用此方法前必须保证流连接已经创建
|
||||
* @param hook {stream_hook*} 非空对象指针
|
||||
* @return {stream_hook*} 返回值与输入值不同则表示成功
|
||||
@ -275,7 +275,8 @@ private:
|
||||
static int timeout_callback(ACL_ASTREAM*, void*);
|
||||
|
||||
private:
|
||||
std::string ipbuf_;
|
||||
std::string ip_peer_;
|
||||
std::string ip_local_;
|
||||
|
||||
const char* get_ip(const char* addr, std::string& out);
|
||||
|
||||
|
@ -9,15 +9,13 @@ static bool handshake(acl::socket_stream& conn)
|
||||
.set_upgrade("websocket")
|
||||
.set_keep_alive(true);
|
||||
|
||||
if (req.request(NULL, 0) == false)
|
||||
{
|
||||
if (!req.request(NULL, 0)) {
|
||||
printf("request error\r\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
int status = req.http_status();
|
||||
if (status != 101)
|
||||
{
|
||||
if (status != 101) {
|
||||
printf("invalid http status: %d\r\n", status);
|
||||
return false;
|
||||
}
|
||||
@ -28,15 +26,13 @@ static bool handshake(acl::socket_stream& conn)
|
||||
static bool send_file(acl::websocket& ws, const char* filepath)
|
||||
{
|
||||
acl::ifstream in;
|
||||
if (in.open_read(filepath) == false)
|
||||
{
|
||||
if (!in.open_read(filepath)) {
|
||||
printf("open %s error %s\r\n", filepath, acl::last_serror());
|
||||
return false;
|
||||
}
|
||||
|
||||
long long size = in.fsize();
|
||||
if (size <= 0)
|
||||
{
|
||||
if (size <= 0) {
|
||||
printf("filename: %s, invalid size: %lld\r\n", filepath, size);
|
||||
return false;
|
||||
}
|
||||
@ -44,12 +40,13 @@ static bool send_file(acl::websocket& ws, const char* filepath)
|
||||
acl::string buf;
|
||||
buf.basename(filepath);
|
||||
|
||||
unsigned mask = ~0;
|
||||
ws.set_frame_fin(true)
|
||||
.set_frame_opcode(acl::FRAME_TEXT)
|
||||
.set_frame_payload_len(buf.size());
|
||||
.set_frame_payload_len(buf.size())
|
||||
.set_frame_masking_key(mask);
|
||||
|
||||
if (ws.send_frame_data(buf, buf.size()) == false)
|
||||
{
|
||||
if (!ws.send_frame_data(buf, buf.size())) {
|
||||
printf("send filenam error %s\r\n", acl::last_serror());
|
||||
return false;
|
||||
}
|
||||
@ -58,32 +55,31 @@ static bool send_file(acl::websocket& ws, const char* filepath)
|
||||
ws.reset().set_frame_fin(true)
|
||||
.set_frame_opcode(acl::FRAME_TEXT)
|
||||
.set_frame_payload_len(buf.size());
|
||||
if (ws.send_frame_data(buf, buf.size()) == false)
|
||||
{
|
||||
if (!ws.send_frame_data(buf, buf.size())) {
|
||||
printf("send file size error %s\r\n", acl::last_serror());
|
||||
return false;
|
||||
}
|
||||
|
||||
long long total = 0;
|
||||
char cbuf[128000];
|
||||
while (!in.eof())
|
||||
{
|
||||
while (!in.eof()) {
|
||||
int ret = in.read(cbuf, sizeof(cbuf), false);
|
||||
if (ret == -1)
|
||||
if (ret == -1) {
|
||||
break;
|
||||
}
|
||||
|
||||
printf(">>send %d\r\n", ret);
|
||||
ws.reset().set_frame_fin(true)
|
||||
.set_frame_opcode(acl::FRAME_BINARY)
|
||||
.set_frame_payload_len(ret);
|
||||
if (ws.send_frame_data(cbuf, ret) == false)
|
||||
{
|
||||
if (!ws.send_frame_data(cbuf, ret)) {
|
||||
printf("send data error %s\r\n", acl::last_serror());
|
||||
return false;
|
||||
}
|
||||
total += ret;
|
||||
if (total % 10240000 == 0)
|
||||
if (total % 10240000 == 0) {
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
printf(">>total send: %lld\r\n", total);
|
||||
@ -92,16 +88,14 @@ static bool send_file(acl::websocket& ws, const char* filepath)
|
||||
|
||||
static bool read_reply(acl::websocket& ws)
|
||||
{
|
||||
if (ws.read_frame_head() == false)
|
||||
{
|
||||
if (!ws.read_frame_head()) {
|
||||
printf("read_frame_head error %s\r\n", acl::last_serror());
|
||||
return false;
|
||||
}
|
||||
|
||||
char cbuf[1024];
|
||||
unsigned char opcode = ws.get_frame_opcode();
|
||||
switch (opcode)
|
||||
{
|
||||
switch (opcode) {
|
||||
case acl::FRAME_TEXT:
|
||||
case acl::FRAME_BINARY:
|
||||
break;
|
||||
@ -111,8 +105,7 @@ static bool read_reply(acl::websocket& ws)
|
||||
}
|
||||
|
||||
int ret = ws.read_frame_data(cbuf, sizeof(cbuf) - 1);
|
||||
if (ret <= 0)
|
||||
{
|
||||
if (ret <= 0) {
|
||||
printf("read_frame_data error\r\n");
|
||||
return false;
|
||||
}
|
||||
@ -125,22 +118,24 @@ static bool read_reply(acl::websocket& ws)
|
||||
static bool upload(const char* addr, const char* filepath)
|
||||
{
|
||||
acl::socket_stream conn;
|
||||
if (conn.open(addr, 30, 30) == false)
|
||||
{
|
||||
if (!conn.open(addr, 30, 30)) {
|
||||
printf("connect %s error %s\r\n", addr, acl::last_serror());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (handshake(conn) == false)
|
||||
if (!handshake(conn)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
acl::websocket ws(conn);
|
||||
|
||||
if (send_file(ws, filepath) == false)
|
||||
if (!send_file(ws, filepath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (read_reply(ws) == false)
|
||||
if (!read_reply(ws)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -158,10 +153,8 @@ int main(int argc, char* argv[])
|
||||
|
||||
acl::string addr, filename;
|
||||
|
||||
while ((ch = getopt(argc, argv, "hf:s:")) > 0)
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
while ((ch = getopt(argc, argv, "hf:s:")) > 0) {
|
||||
switch (ch) {
|
||||
case 'h':
|
||||
usage(argv[0]);
|
||||
return 0;
|
||||
@ -176,13 +169,11 @@ int main(int argc, char* argv[])
|
||||
}
|
||||
}
|
||||
|
||||
if (addr.empty() || filename.empty())
|
||||
{
|
||||
if (addr.empty() || filename.empty()) {
|
||||
usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
upload(addr, filename);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -662,6 +662,9 @@ void http_aclient::ws_handshake(const void* key, size_t len)
|
||||
ws_in_ = NEW websocket(*stream_);
|
||||
ws_out_ = NEW websocket(*stream_);
|
||||
|
||||
unsigned mask = ~0;
|
||||
ws_out_->set_frame_masking_key(mask);
|
||||
|
||||
status_ = HTTP_ACLIENT_STATUS_WS_HANDSHAKE;
|
||||
send_request(NULL, 0);
|
||||
}
|
||||
|
@ -50,9 +50,9 @@ websocket& websocket::reset(void)
|
||||
header_.rsv2 = false;
|
||||
header_.rsv3 = false;
|
||||
header_.opcode = FRAME_TEXT;
|
||||
header_.mask = false;
|
||||
// header_.mask = false;
|
||||
header_.payload_len = 0;
|
||||
header_.masking_key = 0;
|
||||
// header_.masking_key = 0;
|
||||
|
||||
payload_nread_ = 0;
|
||||
payload_nsent_ = 0;
|
||||
@ -105,7 +105,7 @@ websocket& websocket::set_frame_payload_len(unsigned long long len)
|
||||
websocket& websocket::set_frame_masking_key(unsigned int mask)
|
||||
{
|
||||
header_.masking_key = mask;
|
||||
header_.mask = true;
|
||||
header_.mask = mask != 0 ? true : false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -303,11 +303,12 @@ int redis_pubsub::check_channel(const redis_result* obj, const char* cmd,
|
||||
}
|
||||
|
||||
bool redis_pubsub::get_message(string& channel, string& msg,
|
||||
string* message_type /* = NULL */, string* pattern /* = NULL */)
|
||||
string* message_type /* = NULL */, string* pattern /* = NULL */,
|
||||
int timeout /* = -1 */)
|
||||
{
|
||||
clear_request();
|
||||
int rw_timeout = -1;
|
||||
const redis_result* result = run(0, &rw_timeout);
|
||||
const redis_result* result = run(0, timeout >= 0 ? &timeout : &rw_timeout);
|
||||
if (result == NULL)
|
||||
return false;
|
||||
if (result->get_type() != REDIS_RESULT_ARRAY)
|
||||
|
@ -87,7 +87,7 @@ const char* aio_stream::get_peer(bool full /* = false */) const
|
||||
}
|
||||
|
||||
return const_cast<aio_stream*>
|
||||
(this)->get_ip(ptr, const_cast<aio_stream*>(this)->ipbuf_);
|
||||
(this)->get_ip(ptr, const_cast<aio_stream*>(this)->ip_peer_);
|
||||
}
|
||||
|
||||
const char* aio_stream::get_local(bool full /* = false */) const
|
||||
@ -113,7 +113,7 @@ const char* aio_stream::get_local(bool full /* = false */) const
|
||||
}
|
||||
|
||||
return const_cast<aio_stream*>
|
||||
(this)->get_ip(ptr, const_cast<aio_stream*>(this)->ipbuf_);
|
||||
(this)->get_ip(ptr, const_cast<aio_stream*>(this)->ip_local_);
|
||||
}
|
||||
|
||||
const char* aio_stream::get_ip(const char* addr, std::string& out)
|
||||
@ -124,7 +124,7 @@ const char* aio_stream::get_ip(const char* addr, std::string& out)
|
||||
if (ptr) {
|
||||
*ptr = 0;
|
||||
}
|
||||
out = ptr;
|
||||
out = buf;
|
||||
return out.c_str();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user