Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support UDP server. #1498

Merged
merged 7 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/en/tutorial-05-http_proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ In [WFHttpServer.h](/src/server/WFHttpServer.h), the default parameters for an H
~~~cpp
static constexpr struct WFServerParams HTTP_SERVER_PARAMS_DEFAULT =
{
.transport_type = TT_TCP,
.max_connections = 2000,
.peer_response_timeout = 10 * 1000,
.receive_timeout = -1,
Expand All @@ -49,7 +50,7 @@ static constexpr struct WFServerParams HTTP_SERVER_PARAMS_DEFAULT =
.ssl_accept_timeout = 10 * 1000,
};
~~~

**transport\_type**: the transport layer protocol. Besides the default type TT_TCP, you may specify TT_UDP, or TT_SCTP on Linux platform.
**max\_connections**: the maximum number of connections is 2000. When it is exceeded, the least recently used keep-alive connection will be closed. If there is no keep-alive connection, the server will refuse new connections.
**peer\_response\_timeout**: set the maximum duration for reading or sending out a block of data. The default setting is 10 seconds.
**receive\_timeout**: set the maximum duration for receiving a complete request; -1 means unlimited time.
Expand Down
1 change: 0 additions & 1 deletion docs/en/tutorial-10-user_defined_protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ private:
* For the definition of **struct iovec**, please see the system calls **readv** or **writev**.
* Normally the return value of the encode function is between 0 and max, indicating how many vector are used in the message.
* In case of UDP protocol, please note that the total length must not be more than 64k, and no more than 1024 vectors are used (in Linux, writev writes only 1024 vectors at one time).
* UDP protocol can only be used for a client, and UDP server cannot be realized.
* The encode -1 indicates errors. To return -1, you need to set errno. If the return value is > max, you will get an EOVERFLOW error. All errors are obtained in the callback.
* For performance reasons, the content pointed to by the iov\_base pointer in the vector will not be copied. So it generally points to the member of the message class.

Expand Down
2 changes: 2 additions & 0 deletions docs/tutorial-05-http_proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ int main(int argc, char *argv[])
~~~cpp
static constexpr struct WFServerParams HTTP_SERVER_PARAMS_DEFAULT =
{
.transport_type = TT_TCP,
.max_connections = 2000,
.peer_response_timeout = 10 * 1000,
.receive_timeout = -1,
Expand All @@ -47,6 +48,7 @@ static constexpr struct WFServerParams HTTP_SERVER_PARAMS_DEFAULT =
.ssl_accept_timeout = 10 * 1000,
};
~~~
transport_type:传输层协议,默认为TCP。除了TT_TCP外,可选择的还有TT_UDP和Linux下支持的TT_SCTP。
max_connections:最大连接数2000,达到上限之后会关闭最久未使用的keep-alive连接。没找到keep-alive连接,则拒绝新连接。
peer_response_timeout:每读取到一块数据或发送出一块数据的超时时间为10秒。
receive_timeout:接收一条完整的请求超时时间为-1,无限。
Expand Down
1 change: 0 additions & 1 deletion docs/tutorial-10-user_defined_protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ private:
* 结构体struct iovec定义在请参考系统调用readv和writev。
* encode函数正确情况下的返回值在0到max之间,表示消息使用了多少个vector。
* 如果是UDP协议,请注意总长度不超过64k,并且使用不超过1024个vector(Linux一次writev只能1024个vector)。
* UDP协议只能用于client,无法实现UDP server。
* encode返回-1表示错误。返回-1时,需要置errno。如果返回值>max,将得到一个EOVERFLOW错误。错误都在callback里得到。
* 为了性能考虑vector里的iov_base指针指向的内容不会被复制。所以一般指向消息类的成员。

Expand Down
42 changes: 41 additions & 1 deletion src/factory/DnsTaskImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

#include <string>
#include <atomic>
#include "DnsMessage.h"
#include "WFTaskError.h"
#include "WFTaskFactory.h"
#include "DnsMessage.h"
#include "WFServer.h"

using namespace protocol;

Expand Down Expand Up @@ -184,3 +185,42 @@ WFDnsTask *WFTaskFactory::create_dns_task(const ParsedURI& uri,
return task;
}


/**********Server**********/

class WFDnsServerTask : public WFServerTask<DnsRequest, DnsResponse>
{
public:
WFDnsServerTask(CommService *service,
std::function<void (WFDnsTask *)>& proc) :
WFServerTask(service, WFGlobal::get_scheduler(), proc)
{
auto *server = (WFServer<DnsRequest, DnsResponse> *)service;
this->type = server->get_params()->transport_type;
}

protected:
virtual CommMessageIn *message_in()
{
this->get_req()->set_single_packet(this->type == TT_UDP);
return this->WFServerTask::message_in();
}

virtual CommMessageOut *message_out()
{
this->get_resp()->set_single_packet(this->type == TT_UDP);
return this->WFServerTask::message_out();
}

protected:
enum TransportType type;
};

/**********Server Factory**********/

WFDnsTask *WFServerTaskFactory::create_dns_task(CommService *service,
std::function<void (WFDnsTask *)>& proc)
{
return new WFDnsServerTask(service, proc);
}

3 changes: 3 additions & 0 deletions src/factory/WFTaskFactory.inl
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,9 @@ WFNetworkTaskFactory<REQ, RESP>::create_server_task(CommService *service,
class WFServerTaskFactory
{
public:
static WFDnsTask *create_dns_task(CommService *service,
std::function<void (WFDnsTask *)>& proc);

static WFHttpTask *create_http_task(CommService *service,
std::function<void (WFHttpTask *)>& proc)
{
Expand Down
Loading
Loading