This commit is contained in:
郑树新 2019-09-18 14:54:26 +08:00
commit 7ad4471ab3
11 changed files with 61 additions and 57 deletions

BIN
doc/net-architecture.pptx Normal file

Binary file not shown.

View File

@ -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 时应该对
源串判空,否则会引起崩溃

View File

@ -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;
}

View File

@ -124,7 +124,7 @@ public:
websocket& set_frame_payload_len(unsigned long long len);
/**
*
*
* @param mask {unsigned int}
* @return {websocket&}
*/

View File

@ -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);
/**
*

View File

@ -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);

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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)

View File

@ -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();
}