a8/a8/websocketclient.cc
aozhiwei 0a6ae55785 1
2023-05-28 14:53:20 +08:00

122 lines
3.8 KiB
C++

#include <a8/a8.h>
#include <a8/websocketclient.h>
#ifdef USE_BOOST
static const unsigned char FIN = 0x80;
static const unsigned char RSV1 = 0x40;
static const unsigned char RSV2 = 0x20;
static const unsigned char RSV3 = 0x10;
static const unsigned char RSV_MASK = RSV1 | RSV2 | RSV3;
static const unsigned char OPCODE_MASK = 0x0F;
static const unsigned char TEXT_MODE = 0x01;
static const unsigned char BINARY_MODE = 0x02;
static const unsigned char WEBSOCKET_OPCODE = 0x0F;
static const unsigned char WEBSOCKET_FRAME_CONTINUE = 0x0;
static const unsigned char WEBSOCKET_FRAME_TEXT = 0x1;
static const unsigned char WEBSOCKET_FRAME_BINARY = 0x2;
static const unsigned char WEBSOCKET_FRAME_CLOSE = 0x8;
static const unsigned char WEBSOCKET_FRAME_PING = 0x9;
static const unsigned char WEBSOCKET_FRAME_PONG = 0xA;
static const unsigned char WEBSOCKET_MASK = 0x80;
static const unsigned char WEBSOCKET_PAYLOAD_LEN = 0x7F;
static const unsigned char WEBSOCKET_PAYLOAD_LEN_UINT16 = 126;
static const unsigned char WEBSOCKET_PAYLOAD_LEN_UINT64 = 127;
static const char* WEB_SOCKET_KEY = "Sec-WebSocket-Key: ";
static const char* WEB_SOCKET_KEY2 = "Sec-Websocket-Key: ";
namespace a8
{
WebSocketClient::WebSocketClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port)
{
tcp_client_ = std::make_shared<AsioTcpClient>(io_context, remote_ip, remote_port);
decoded_buff_ = (char *)malloc(1024 * 64 + 1);
decoded_bufflen_ = 0;
tcp_client_->on_error =
[] (a8::AsioTcpClient* socket, int err)
{
};
tcp_client_->on_connect =
[] (a8::AsioTcpClient* socket)
{
std::string data = a8::Format("GET ws://%s:%d/\r\n",
{socket->GetRemoteAddress(),
socket->GetRemotePort()});
data += "Upgrade: websocket\r\n";
data += "Connection: Upgrade\r\n";
data += "Sec-WebSocket-Version: 13\r\n";
data += "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==*\r\n";
data += "\r\n";
socket->SendBuff(data.data(), data.size());
};
tcp_client_->on_disconnect =
[] (a8::AsioTcpClient* socket)
{
};
tcp_client_->on_socketread =
[] (a8::AsioTcpClient* socket, char* buf, unsigned int buf_len)
{
int i = 0;
};
}
WebSocketClient::~WebSocketClient()
{
free(decoded_buff_);
decoded_buff_ = nullptr;
decoded_bufflen_ = 0;
}
void WebSocketClient::Open()
{
tcp_client_->Open();
}
void WebSocketClient::Close()
{
tcp_client_->Close();
}
bool WebSocketClient::IsActive()
{
return tcp_client_->IsActive();
}
bool WebSocketClient::Connected()
{
return tcp_client_->Connected();
}
void WebSocketClient::SendBuff(const char* buff, unsigned int bufflen)
{
if (!handshook_) {
abort();
}
unsigned char szbuff [1024 * 65];
szbuff[0] = FIN | BINARY_MODE | 0;
if (bufflen < 126) {
szbuff[1] = bufflen;
memmove(szbuff + 2, buff, bufflen);
tcp_client_->SendBuff((char*)szbuff, bufflen + 2);
} else if (bufflen <= 0xFFFF) {
szbuff[1] = 126;
szbuff[2] = (bufflen >> 8) & 0xFF;
szbuff[3] = bufflen & 0xFF;
memmove(szbuff + 4, buff, bufflen);
tcp_client_->SendBuff((char*)szbuff, bufflen + 4);
} else {
abort();
}
}
}
#endif