1
This commit is contained in:
parent
0832282bbc
commit
91b5f74b3a
@ -15,17 +15,18 @@ const int MAX_RECV_BUFFERSIZE = 1024 * 64;
|
|||||||
namespace a8
|
namespace a8
|
||||||
{
|
{
|
||||||
|
|
||||||
AsioTcpClient::AsioTcpClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port)
|
AsioTcpClient::AsioTcpClient(std::shared_ptr<asio::io_context> io_context, const std::string& remote_ip, int remote_port)
|
||||||
{
|
{
|
||||||
|
io_context_ = io_context;
|
||||||
remote_address_ = remote_ip;
|
remote_address_ = remote_ip;
|
||||||
remote_port_ = remote_port;
|
remote_port_ = remote_port;
|
||||||
endpoint_ = std::make_shared<asio::ip::tcp::endpoint>
|
endpoint_ = std::make_shared<asio::ip::tcp::endpoint>
|
||||||
(
|
(
|
||||||
asio::ip::make_address(remote_address_),
|
asio::ip::address::from_string(remote_address_),
|
||||||
remote_port_
|
remote_port_
|
||||||
);
|
);
|
||||||
send_buffer_mutex_ = std::make_shared<std::mutex>();
|
send_buffer_mutex_ = std::make_shared<std::mutex>();
|
||||||
socket_ = std::make_shared<asio::ip::tcp::socket>(io_context);
|
socket_ = std::make_shared<asio::ip::tcp::socket>(*io_context);
|
||||||
}
|
}
|
||||||
|
|
||||||
AsioTcpClient::~AsioTcpClient()
|
AsioTcpClient::~AsioTcpClient()
|
||||||
@ -95,10 +96,12 @@ namespace a8
|
|||||||
{
|
{
|
||||||
actived_ = true;
|
actived_ = true;
|
||||||
connected_ = false;
|
connected_ = false;
|
||||||
|
a8::XPrintf("remote_ip:%s remote_port:%d\n", {endpoint_->address().to_string(), endpoint_->port()});
|
||||||
socket_->async_connect
|
socket_->async_connect
|
||||||
(*endpoint_,
|
(*endpoint_,
|
||||||
[this] (const asio::error_code& ec)
|
[this] (const asio::error_code& ec)
|
||||||
{
|
{
|
||||||
|
a8::XPrintf("async econnect %d\n", {ec.value()});
|
||||||
HandleConnect(ec);
|
HandleConnect(ec);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,9 @@ namespace a8
|
|||||||
std::function<void (a8::AsioTcpClient*)> on_connect;
|
std::function<void (a8::AsioTcpClient*)> on_connect;
|
||||||
std::function<void (a8::AsioTcpClient*)> on_disconnect;
|
std::function<void (a8::AsioTcpClient*)> on_disconnect;
|
||||||
std::function<void (a8::AsioTcpClient*, char*, unsigned int)> on_socketread;
|
std::function<void (a8::AsioTcpClient*, char*, unsigned int)> on_socketread;
|
||||||
AsioTcpClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port);
|
AsioTcpClient(std::shared_ptr<asio::io_context> io_context,
|
||||||
|
const std::string& remote_ip,
|
||||||
|
int remote_port);
|
||||||
virtual ~AsioTcpClient();
|
virtual ~AsioTcpClient();
|
||||||
const std::string& GetRemoteAddress() { return remote_address_; }
|
const std::string& GetRemoteAddress() { return remote_address_; }
|
||||||
int GetRemotePort() { return remote_port_; }
|
int GetRemotePort() { return remote_port_; }
|
||||||
@ -33,6 +35,7 @@ namespace a8
|
|||||||
void DoSend();
|
void DoSend();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
std::shared_ptr<asio::io_context> io_context_;
|
||||||
std::string remote_address_;
|
std::string remote_address_;
|
||||||
int remote_port_ = 0;
|
int remote_port_ = 0;
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ static const int DEFAULT_MAX_RECV_BUFFERSIZE = 1024 * 64;
|
|||||||
namespace a8
|
namespace a8
|
||||||
{
|
{
|
||||||
|
|
||||||
WebSocketClient::WebSocketClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port)
|
WebSocketClient::WebSocketClient(std::shared_ptr<asio::io_context> io_context, const std::string& remote_ip, int remote_port)
|
||||||
{
|
{
|
||||||
max_packet_len_ = DEFAULT_MAX_PACKET_LEN;
|
max_packet_len_ = DEFAULT_MAX_PACKET_LEN;
|
||||||
recv_buff_ = (char *)malloc(max_packet_len_ + 1);
|
recv_buff_ = (char *)malloc(max_packet_len_ + 1);
|
||||||
@ -46,12 +46,14 @@ namespace a8
|
|||||||
decoded_buff_ = (char *)malloc(1024 * 64 + 1);
|
decoded_buff_ = (char *)malloc(1024 * 64 + 1);
|
||||||
decoded_bufflen_ = 0;
|
decoded_bufflen_ = 0;
|
||||||
tcp_client_->on_error =
|
tcp_client_->on_error =
|
||||||
[] (a8::AsioTcpClient* socket, int err)
|
[this] (a8::AsioTcpClient* socket, int err)
|
||||||
{
|
{
|
||||||
|
if (on_error) {
|
||||||
|
on_error(this, err);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
tcp_client_->on_connect =
|
tcp_client_->on_connect =
|
||||||
[] (a8::AsioTcpClient* socket)
|
[this] (a8::AsioTcpClient* socket)
|
||||||
{
|
{
|
||||||
std::string data = a8::Format("GET ws://%s:%d/\r\n",
|
std::string data = a8::Format("GET ws://%s:%d/\r\n",
|
||||||
{socket->GetRemoteAddress(),
|
{socket->GetRemoteAddress(),
|
||||||
@ -64,8 +66,11 @@ namespace a8
|
|||||||
socket->SendBuff(data.data(), data.size());
|
socket->SendBuff(data.data(), data.size());
|
||||||
};
|
};
|
||||||
tcp_client_->on_disconnect =
|
tcp_client_->on_disconnect =
|
||||||
[] (a8::AsioTcpClient* socket)
|
[this] (a8::AsioTcpClient* socket)
|
||||||
{
|
{
|
||||||
|
if (on_disconnect) {
|
||||||
|
on_disconnect(this);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
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)
|
||||||
@ -170,8 +175,38 @@ namespace a8
|
|||||||
if (!pend) {
|
if (!pend) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
#if 0
|
||||||
|
std::string server_key;
|
||||||
|
{
|
||||||
|
char* p1 = strstr(buf + offset, WEB_SOCKET_KEY);
|
||||||
|
if (!p1) {
|
||||||
|
p1 = strstr(buf + offset, WEB_SOCKET_KEY2);
|
||||||
|
}
|
||||||
|
if (p1) {
|
||||||
|
p1 += strlen(WEB_SOCKET_KEY);
|
||||||
|
char* p2 = strstr(p1 , "\r\n");
|
||||||
|
if (p2) {
|
||||||
|
server_key.append(p1, p2-p1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
server_key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
||||||
|
std::string sha1_data = a8::openssl::Sha1Encode(server_key);
|
||||||
|
std::string hash_data;
|
||||||
|
a8::openssl::Base64Encode(sha1_data, hash_data);
|
||||||
|
|
||||||
|
std::string response = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
|
||||||
|
"Upgrade: websocket\r\n"
|
||||||
|
"Connection: Upgrade\r\n"
|
||||||
|
"Sec-WebSocket-Accept: " + hash_data + "\r\n" +
|
||||||
|
"\r\n";
|
||||||
|
SendBuff(response.data(), response.size());
|
||||||
handshook_ = true;
|
handshook_ = true;
|
||||||
offset += pend - buf - offset + strlen("\r\n\r\n");
|
offset += pend - buf - offset + strlen("\r\n\r\n");
|
||||||
|
if (on_connect) {
|
||||||
|
on_connect(this);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebSocketClient::ProcessUserPacket()
|
void WebSocketClient::ProcessUserPacket()
|
||||||
|
@ -14,7 +14,7 @@ namespace a8
|
|||||||
class WebSocketClient
|
class WebSocketClient
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
WebSocketClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port);
|
WebSocketClient(std::shared_ptr<asio::io_context> io_context, const std::string& remote_ip, int remote_port);
|
||||||
virtual ~WebSocketClient();
|
virtual ~WebSocketClient();
|
||||||
|
|
||||||
std::function<void (a8::WebSocketClient*, int)> on_error;
|
std::function<void (a8::WebSocketClient*, int)> on_error;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user