1
This commit is contained in:
parent
b6e0b07313
commit
126d511bfb
@ -63,11 +63,35 @@ namespace a8
|
|||||||
tcp_client_->on_socketread =
|
tcp_client_->on_socketread =
|
||||||
[this] (a8::AsioTcpClient* socket, char* buf, unsigned int buflen)
|
[this] (a8::AsioTcpClient* socket, char* buf, unsigned int buflen)
|
||||||
{
|
{
|
||||||
if (!handshook_) {
|
unsigned int already_read_bytes = 0;
|
||||||
buf[buflen] = '\0';
|
do {
|
||||||
} else {
|
if (already_read_bytes < buflen) {
|
||||||
|
int read_bytes = std::min(buflen - already_read_bytes,
|
||||||
|
(unsigned int)max_packet_len_ - recv_bufflen_);
|
||||||
|
if (read_bytes > 0) {
|
||||||
|
memmove(&recv_buff_[recv_bufflen_], buf + already_read_bytes, read_bytes);
|
||||||
|
recv_bufflen_ += read_bytes;
|
||||||
|
already_read_bytes += read_bytes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
int offset = 0;
|
||||||
|
int prev_offset = 0;
|
||||||
|
do {
|
||||||
|
prev_offset = offset;
|
||||||
|
DecodePacket(recv_buff_, offset, recv_bufflen_);
|
||||||
|
} while (prev_offset < offset && offset < recv_bufflen_);
|
||||||
|
|
||||||
|
if (offset > 0 && offset < recv_bufflen_){
|
||||||
|
memmove(recv_buff_, recv_buff_ + offset, recv_bufflen_ - offset);
|
||||||
|
}
|
||||||
|
recv_bufflen_ -= offset;
|
||||||
|
if (recv_bufflen_ >= max_packet_len_) {
|
||||||
|
//收到超长包
|
||||||
|
Close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} while (already_read_bytes < buflen);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,7 +153,7 @@ namespace a8
|
|||||||
if (strstr(buf + offset, WEB_SOCKET_KEY) || strstr(buf + offset, WEB_SOCKET_KEY2)) {
|
if (strstr(buf + offset, WEB_SOCKET_KEY) || strstr(buf + offset, WEB_SOCKET_KEY2)) {
|
||||||
ProcessWsHandShake(buf, offset, buflen);
|
ProcessWsHandShake(buf, offset, buflen);
|
||||||
} else {
|
} else {
|
||||||
//ProcessHttpHandShake(buf, offset, buflen);
|
abort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,20 +207,18 @@ namespace a8
|
|||||||
int prev_offset = 0;
|
int prev_offset = 0;
|
||||||
do {
|
do {
|
||||||
prev_offset = offset;
|
prev_offset = offset;
|
||||||
//DecodeUserPacket(decoded_buff_, offset, decoded_bufflen_);
|
DecodeUserPacket(decoded_buff_, offset, decoded_bufflen_);
|
||||||
} while (prev_offset < offset && offset < decoded_bufflen_);
|
} while (prev_offset < offset && offset < decoded_bufflen_);
|
||||||
|
|
||||||
if (offset > 0 && offset < decoded_bufflen_){
|
if (offset > 0 && offset < decoded_bufflen_){
|
||||||
memmove(decoded_buff_, decoded_buff_ + offset, decoded_bufflen_ - offset);
|
memmove(decoded_buff_, decoded_buff_ + offset, decoded_bufflen_ - offset);
|
||||||
}
|
}
|
||||||
decoded_bufflen_ -= offset;
|
decoded_bufflen_ -= offset;
|
||||||
#if 0
|
|
||||||
if (decoded_bufflen_ >= max_packet_len_) {
|
if (decoded_bufflen_ >= max_packet_len_) {
|
||||||
//收到超长包
|
//收到超长包
|
||||||
Close();
|
Close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebSocketClient::DecodeFrame(char* buf, int& offset, unsigned int buflen)
|
void WebSocketClient::DecodeFrame(char* buf, int& offset, unsigned int buflen)
|
||||||
@ -276,6 +298,21 @@ namespace a8
|
|||||||
offset += real_pkg_len;
|
offset += real_pkg_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WebSocketClient::DecodePacket(char* buf, int& offset, unsigned int buflen)
|
||||||
|
{
|
||||||
|
if (!handshook_) {
|
||||||
|
buf[buflen] = '\0';
|
||||||
|
ProcessHandShake(buf, offset, buflen);
|
||||||
|
} else {
|
||||||
|
DecodeFrame(buf, offset, buflen);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebSocketClient::DecodeUserPacket(char* buf, int& offset, unsigned int buflen)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -29,16 +29,21 @@ namespace a8
|
|||||||
void SendBuff(const char* buff, unsigned int bufflen);
|
void SendBuff(const char* buff, unsigned int bufflen);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void DecodePacket(char* buf, int& offset, unsigned int buflen);
|
||||||
|
void DecodeUserPacket(char* buf, int& offset, unsigned int buflen);
|
||||||
|
|
||||||
void ProcessHandShake(char* buf, int& offset, unsigned int buflen);
|
void ProcessHandShake(char* buf, int& offset, unsigned int buflen);
|
||||||
void ProcessWsHandShake(char* buf, int& offset, unsigned int buflen);
|
void ProcessWsHandShake(char* buf, int& offset, unsigned int buflen);
|
||||||
void ProcessUserPacket();
|
void ProcessUserPacket();
|
||||||
void DecodeFrame(char* buf, int& offset, unsigned int buflen);
|
void DecodeFrame(char* buf, int& offset, unsigned int buflen);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<AsioTcpClient> tcp_client_;
|
std::shared_ptr<AsioTcpClient> tcp_client_;
|
||||||
char *decoded_buff_ = nullptr;
|
char *decoded_buff_ = nullptr;
|
||||||
int decoded_bufflen_ = 0;
|
int decoded_bufflen_ = 0;
|
||||||
bool handshook_ = false;
|
bool handshook_ = false;
|
||||||
|
char *recv_buff_ = nullptr;
|
||||||
|
int recv_bufflen_ = 0;
|
||||||
|
int max_packet_len_ = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user