Skip to content

Commit

Permalink
Support UDP server.
Browse files Browse the repository at this point in the history
  • Loading branch information
Barenboim committed Mar 1, 2024
1 parent 6ef12e6 commit 5e34178
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/server/WFDnsServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ using WFDnsServer = WFServer<protocol::DnsRequest,

static constexpr struct WFServerParams DNS_SERVER_PARAMS_DEFAULT =
{
.transport_type = TT_UDP,
.max_connections = 2000,
.peer_response_timeout = 10 * 1000,
.receive_timeout = -1,
Expand Down
1 change: 1 addition & 0 deletions src/server/WFHttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ using WFHttpServer = WFServer<protocol::HttpRequest,

static constexpr struct WFServerParams HTTP_SERVER_PARAMS_DEFAULT =
{
.transport_type = TT_TCP,
.max_connections = 2000,
.peer_response_timeout = 10 * 1000,
.receive_timeout = -1,
Expand Down
1 change: 1 addition & 0 deletions src/server/WFMySQLServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class MySQLServer;

static constexpr struct WFServerParams MYSQL_SERVER_PARAMS_DEFAULT =
{
.transport_type = TT_TCP,
.max_connections = 2000,
.peer_response_timeout = 10 * 1000,
.receive_timeout = -1,
Expand Down
1 change: 1 addition & 0 deletions src/server/WFRedisServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ using WFRedisServer = WFServer<protocol::RedisRequest,

static constexpr struct WFServerParams REDIS_SERVER_PARAMS_DEFAULT =
{
.transport_type = TT_TCP,
.max_connections = 2000,
.peer_response_timeout = 10 * 1000,
.receive_timeout = -1,
Expand Down
27 changes: 26 additions & 1 deletion src/server/WFServer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <condition_variable>
#include <openssl/ssl.h>
#include "CommScheduler.h"
#include "EndpointParams.h"
#include "WFConnection.h"
#include "WFGlobal.h"
#include "WFServer.h"
Expand Down Expand Up @@ -121,10 +122,34 @@ int WFServerBase::create_listen_fd()
{
const struct sockaddr *bind_addr;
socklen_t addrlen;
int type, protocol;
int reuse = 1;

switch (this->params.transport_type)
{
case TT_TCP:
case TT_TCP_SSL:
type = SOCK_STREAM;
protocol = 0;
break;
case TT_UDP:
type = SOCK_DGRAM;
protocol = 0;
break;
#ifdef IPPROTO_SCTP
case TT_SCTP:
case TT_SCTP_SSL:
type = SOCK_STREAM;
protocol = IPPROTO_SCTP;
break;
#endif
default:
errno = EPROTONOSUPPORT;
return -1;
}

this->get_addr(&bind_addr, &addrlen);
this->listen_fd = socket(bind_addr->sa_family, SOCK_STREAM, 0);
this->listen_fd = socket(bind_addr->sa_family, type, protocol);
if (this->listen_fd >= 0)
{
setsockopt(this->listen_fd, SOL_SOCKET, SO_REUSEADDR,
Expand Down
3 changes: 3 additions & 0 deletions src/server/WFServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
#include <mutex>
#include <condition_variable>
#include <openssl/ssl.h>
#include "EndpointParams.h"
#include "WFTaskFactory.h"

struct WFServerParams
{
enum TransportType transport_type;
size_t max_connections;
int peer_response_timeout; /* timeout of each read or write operation */
int receive_timeout; /* timeout of receiving the whole message */
Expand All @@ -42,6 +44,7 @@ struct WFServerParams

static constexpr struct WFServerParams SERVER_PARAMS_DEFAULT =
{
.transport_type = TT_TCP,
.max_connections = 2000,
.peer_response_timeout = 10 * 1000,
.receive_timeout = -1,
Expand Down

0 comments on commit 5e34178

Please sign in to comment.