void tcp_stream::async_connect(const char* ip, port_t port, async_connect_callback callback) noexcept { if (socket.is_open()) { this->disconnect(); } std::string _ip_str{ ip }; auto on_connect = [this, callback, port, ip_str = std::move(_ip_str)](const ::boost::system::error_code& error) { async_connect_result result; result.endpoint.set_value(ip_str.c_str(), port); result.callback = std::move(callback); char buffer[256] = { 0, }; _itoa_s(port, buffer, 10); if (error) { result.is_success = false; result.message = std::string{ result.endpoint.ip } + ":" + buffer + " connection failed, detail:" + error.message(); //this->remote_endpoint.clear(); } else { result.is_success = true; result.message = std::string{ result.endpoint.ip } +":" + buffer + " connection established"; this->remote_endpoint = result.endpoint; } //callback->do_call(result); callback(result); }; tcp_endpoint ep{ string_to_ipaddr(ip), port }; socket.async_connect(ep, on_connect); }
void tls_cmd_init_socket_cfg(void) { int timeout = 0, host_len; struct tls_param_socket remote_socket_cfg; tls_param_get(TLS_PARAM_ID_DEFSOCKET, &remote_socket_cfg, FALSE); /* read default socket params */ socket_cfg.proto = remote_socket_cfg.protocol; socket_cfg.client = remote_socket_cfg.client_or_server ? 0 : 1; socket_cfg.port = remote_socket_cfg.port_num; TLS_DBGPRT_INFO("socket_cfg.proto = %d, socket_cfg.client = %d, socket_cfg.port = %d\n", socket_cfg.proto, socket_cfg.client, socket_cfg.port); host_len = strlen((char *)remote_socket_cfg.host); if (socket_cfg.client) { /* host name */ if (host_len){ MEMCPY(socket_cfg.host, remote_socket_cfg.host, host_len); string_to_ipaddr((char *)remote_socket_cfg.host, &socket_cfg.ip_addr[0]); } } else if (!socket_cfg.client && socket_cfg.proto == 0) { if (strtodec(&timeout, (char *)remote_socket_cfg.host)<0){ timeout = 0; } socket_cfg.timeout = timeout; } else ; }
void tcp_stream::connect(const char* ip, port_t port) { if (socket.is_open()) { this->disconnect(); } ::boost::system::error_code err; tcp_endpoint ep{ string_to_ipaddr(ip), port }; this->socket.connect(ep, err); if (err) { throw connection_failed_exception{ err.message().c_str() }; } this->remote_endpoint.set_value(ip, port); }