add open flag for aio_listen_stream

This commit is contained in:
zsxxsz 2018-09-22 09:58:21 +08:00
parent cb218497b6
commit 893facc629
4 changed files with 14 additions and 7 deletions

View File

@ -56,9 +56,10 @@ public:
*
* TCP连接IP:PORT127.0.0.1:9001
* {path}/tmp/my.sock
* @param flag {unsigned} server_socket.hpp
* @return {bool}
*/
bool open(const char* addr);
bool open(const char* addr, unsigned flag = 0);
/**
*

View File

@ -9,12 +9,12 @@ namespace acl {
class socket_stream;
typedef enum {
enum {
OPEN_FLAG_NONE = 0,
OPEN_FLAG_NONBLOCK = 1, // 非阻塞模式
OPEN_FLAG_REUSEPORT = 1 << 1, // 端口复用,要求 Linux3.0 以上
OPEN_FLAG_EXCLUSIVE = 1 << 2, // 是否禁止复用地址
} open_flag_t;
};
/**
*
@ -34,7 +34,7 @@ public:
* @param flag {unsigned} OPEN_FLAG_XXX
* @param backlog {int}
*/
explicit server_socket(open_flag_t flag, int backlog = 128);
explicit server_socket(unsigned flag, int backlog = 128);
/**
* open

View File

@ -1,6 +1,7 @@
#include "acl_stdafx.hpp"
#ifndef ACL_PREPARE_COMPILE
#include "acl_cpp/stdlib/snprintf.hpp"
#include "acl_cpp/stream/socket_stream.hpp"
#include "acl_cpp/stream/aio_handle.hpp"
#include "acl_cpp/stream/aio_socket_stream.hpp"
#include "acl_cpp/stream/aio_listen_stream.hpp"
@ -38,9 +39,14 @@ void aio_listen_stream::add_accept_callback(aio_accept_callback* callback)
accept_callbacks_.push_back(callback);
}
bool aio_listen_stream::open(const char* addr)
bool aio_listen_stream::open(const char* addr, unsigned flag /* = 0 */)
{
ACL_VSTREAM *sstream = acl_vstream_listen(addr, 128);
unsigned oflag = 0;
if (flag & OPEN_FLAG_REUSEPORT)
oflag |= ACL_INET_FLAG_REUSEPORT;
if (flag & OPEN_FLAG_EXCLUSIVE)
oflag |= ACL_INET_FLAG_EXCLUSIVE;
ACL_VSTREAM *sstream = acl_vstream_listen_ex(addr, 128, oflag, 0, 0);
if (sstream == NULL)
return false;

View File

@ -20,7 +20,7 @@ server_socket::server_socket(int backlog /* = 128 */, bool block /* = true */)
open_flag_ = block ? ACL_BLOCKING : ACL_NON_BLOCKING;
}
server_socket::server_socket(open_flag_t flag, int backlog /* = 128 */)
server_socket::server_socket(unsigned flag, int backlog /* = 128 */)
: backlog_(backlog)
, unix_sock_(false)
, fd_(ACL_SOCKET_INVALID)