move params to init()

This commit is contained in:
holmesyxli
2021-11-17 22:02:30 +08:00
parent 33b4afa4d8
commit a5e4237d48
3 changed files with 22 additions and 27 deletions
+16 -18
View File
@@ -32,20 +32,30 @@ WFWebSocketTask *WebSocketClient::create_websocket_task(websocket_callback_t cb)
}
int WebSocketClient::init(const std::string& url)
{
struct WFWebSocketParams params = WEBSOCKET_PARAMS_DEFAULT;
params.url = url.c_str();
return this->init(&params);
}
int WebSocketClient::init(const struct WFWebSocketParams *params)
{
ParsedURI uri;
if (URIParser::parse(url, uri) < 0)
if (URIParser::parse(params->url, uri) < 0)
return -1;
this->channel = new ComplexWebSocketChannel(NULL,
WFGlobal::get_scheduler(),
this->params.random_masking_key,
params->random_masking_key,
std::move(process));
this->channel->set_uri(uri);
this->channel->set_idle_timeout(this->params.idle_timeout);
this->channel->set_size_limit(this->params.size_limit);
this->channel->set_sec_protocol(this->sec_protocol);
this->channel->set_sec_version(this->sec_version);
this->channel->set_idle_timeout(params->idle_timeout);
this->channel->set_size_limit(params->size_limit);
if (params->sec_protocol)
this->channel->set_sec_protocol(params->sec_protocol);
if (params->sec_version)
this->channel->set_sec_version(params->sec_version);
this->channel->set_callback([this](WFChannel<protocol::WebSocketFrame> *channel)
{
@@ -103,15 +113,3 @@ WFWebSocketTask *WebSocketClient::create_close_task(websocket_callback_t cb)
return close_task;
}
WebSocketClient::WebSocketClient(const struct WFWebSocketParams *params,
websocket_process_t process) :
process(std::move(process))
{
this->params = *params;
if (params->sec_protocol)
this->sec_protocol = params->sec_protocol;
if (params->sec_version)
this->sec_version = params->sec_version;
}
+4 -7
View File
@@ -28,6 +28,7 @@
struct WFWebSocketParams
{
const char *url;
int idle_timeout;
int ping_interval;
size_t size_limit;
@@ -38,6 +39,7 @@ struct WFWebSocketParams
static constexpr struct WFWebSocketParams WEBSOCKET_PARAMS_DEFAULT =
{
.url = NULL,
.idle_timeout = WS_HANDSHAKE_TIMEOUT,
.ping_interval = -1,
.size_limit = (size_t)-1,
@@ -50,6 +52,7 @@ class WebSocketClient
{
public:
int init(const std::string& url);
int init(const struct WFWebSocketParams *params);
void deinit();
WFWebSocketTask *create_websocket_task(websocket_callback_t cb);
@@ -60,15 +63,9 @@ private:
ComplexWebSocketChannel *channel;
websocket_process_t process;
struct WFWebSocketParams params;
std::string sec_protocol;
std::string sec_version;
public:
WebSocketClient(const struct WFWebSocketParams *params,
websocket_process_t process);
WebSocketClient(websocket_process_t process) :
WebSocketClient(&WEBSOCKET_PARAMS_DEFAULT, std::move(process))
process(std::move(process))
{ }
};
+2 -2
View File
@@ -453,8 +453,8 @@ public:
void set_idle_timeout(int timeout) { this->idle_timeout = timeout; }
void set_size_limit(size_t size_limit) { this->size_limit = size_limit; }
void set_sec_protocol(std::string& protocol) { this->sec_protocol = protocol; }
void set_sec_version(std::string& version) { this->sec_version = version; }
void set_sec_protocol(const char *protocol) { this->sec_protocol = protocol; }
void set_sec_version(const char *version) { this->sec_version = version; }
const char *get_sec_protocol() const { return this->sec_protocol.c_str(); }
const char *get_sec_version() const { return this->sec_version.c_str(); }