Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
youngwolf
ascs
提交
7e54a267
A
ascs
项目概览
youngwolf
/
ascs
通知
5
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
ascs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7e54a267
编写于
3月 16, 2019
作者:
Y
youngwolf
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Extract function start_listen from current implementations.
上级
f8ae6611
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
44 addition
and
46 deletion
+44
-46
examples/socket_management/client.h
examples/socket_management/client.h
+2
-3
include/ascs/tcp/server.h
include/ascs/tcp/server.h
+42
-43
未找到文件。
examples/socket_management/client.h
浏览文件 @
7e54a267
...
...
@@ -99,11 +99,10 @@ public:
return
socket_ptr
?
(
socket_ptr
->
force_shutdown
(),
true
)
:
false
;
}
bool
send_msg
(
const
std
::
string
&
name
,
const
std
::
string
&
msg
)
{
return
send_msg
(
name
,
std
::
string
(
msg
));}
bool
send_msg
(
const
std
::
string
&
name
,
std
::
string
&&
msg
)
template
<
typename
T
>
bool
send_msg
(
const
std
::
string
&
name
,
T
&&
msg
)
{
auto
socket_ptr
=
find
(
find_link
(
name
));
return
socket_ptr
?
socket_ptr
->
send_msg
(
std
::
move
(
msg
))
:
false
;
return
socket_ptr
?
send_msg
(
name
,
std
::
forward
<
T
>
(
msg
))
:
false
;
}
protected:
...
...
include/ascs/tcp/server.h
浏览文件 @
7e54a267
...
...
@@ -47,8 +47,48 @@ public:
}
const
asio
::
ip
::
tcp
::
endpoint
&
get_server_addr
()
const
{
return
server_addr
;}
void
stop_listen
()
{
asio
::
error_code
ec
;
acceptor
.
cancel
(
ec
);
acceptor
.
close
(
ec
);}
bool
start_listen
()
{
asio
::
error_code
ec
;
if
(
!
acceptor
.
is_open
())
{
acceptor
.
open
(
server_addr
.
protocol
(),
ec
);
assert
(
!
ec
);}
//user maybe has opened this acceptor (to set options for example)
#ifndef ASCS_NOT_REUSE_ADDRESS
acceptor
.
set_option
(
asio
::
ip
::
tcp
::
acceptor
::
reuse_address
(
true
),
ec
);
assert
(
!
ec
);
#endif
acceptor
.
bind
(
server_addr
,
ec
);
assert
(
!
ec
);
if
(
ec
)
{
unified_out
::
error_out
(
"bind failed."
);
return
false
;}
auto
num
=
async_accept_num
();
assert
(
num
>
0
);
if
(
num
<=
0
)
num
=
16
;
std
::
list
<
typename
Pool
::
object_type
>
sockets
;
unified_out
::
info_out
(
"begin to pre-create %d server socket..."
,
num
);
while
(
--
num
>=
0
)
{
auto
socket_ptr
(
create_object
());
if
(
!
socket_ptr
)
break
;
sockets
.
push_back
(
std
::
move
(
socket_ptr
));
}
if
(
num
>=
0
)
unified_out
::
info_out
(
"finished pre-creating server sockets, but failed %d time(s)."
,
num
+
1
);
else
unified_out
::
info_out
(
"finished pre-creating server sockets."
);
#if ASIO_VERSION >= 101100
acceptor
.
listen
(
asio
::
ip
::
tcp
::
acceptor
::
max_listen_connections
,
ec
);
assert
(
!
ec
);
#else
acceptor
.
listen
(
asio
::
ip
::
tcp
::
acceptor
::
max_connections
,
ec
);
assert
(
!
ec
);
#endif
if
(
ec
)
{
unified_out
::
error_out
(
"listen failed."
);
return
false
;}
ascs
::
do_something_to_all
(
sockets
,
[
this
](
typename
Pool
::
object_ctype
&
item
)
{
this
->
do_async_accept
(
item
);});
return
true
;
}
bool
is_listening
()
const
{
return
acceptor
.
is_open
();}
void
stop_listen
()
{
asio
::
error_code
ec
;
acceptor
.
cancel
(
ec
);
acceptor
.
close
(
ec
);}
asio
::
ip
::
tcp
::
acceptor
&
next_layer
()
{
return
acceptor
;}
const
asio
::
ip
::
tcp
::
acceptor
&
next_layer
()
const
{
return
acceptor
;}
...
...
@@ -113,48 +153,7 @@ public:
protected:
virtual
int
async_accept_num
()
{
return
ASCS_ASYNC_ACCEPT_NUM
;}
virtual
bool
init
()
{
asio
::
error_code
ec
;
if
(
!
acceptor
.
is_open
())
{
acceptor
.
open
(
server_addr
.
protocol
(),
ec
);
assert
(
!
ec
);}
//user maybe has opened this acceptor (to set options for example)
#ifndef ASCS_NOT_REUSE_ADDRESS
acceptor
.
set_option
(
asio
::
ip
::
tcp
::
acceptor
::
reuse_address
(
true
),
ec
);
assert
(
!
ec
);
#endif
acceptor
.
bind
(
server_addr
,
ec
);
assert
(
!
ec
);
if
(
ec
)
{
unified_out
::
error_out
(
"bind failed."
);
return
false
;}
auto
num
=
async_accept_num
();
assert
(
num
>
0
);
if
(
num
<=
0
)
num
=
16
;
std
::
list
<
typename
Pool
::
object_type
>
sockets
;
unified_out
::
info_out
(
"begin to pre-create %d server socket..."
,
num
);
while
(
--
num
>=
0
)
{
auto
socket_ptr
(
create_object
());
if
(
!
socket_ptr
)
break
;
sockets
.
push_back
(
std
::
move
(
socket_ptr
));
}
if
(
num
>=
0
)
unified_out
::
info_out
(
"finished pre-creating server sockets, but failed %d time(s)."
,
num
+
1
);
else
unified_out
::
info_out
(
"finished pre-creating server sockets."
);
#if ASIO_VERSION >= 101100
acceptor
.
listen
(
asio
::
ip
::
tcp
::
acceptor
::
max_listen_connections
,
ec
);
assert
(
!
ec
);
#else
acceptor
.
listen
(
asio
::
ip
::
tcp
::
acceptor
::
max_connections
,
ec
);
assert
(
!
ec
);
#endif
if
(
ec
)
{
unified_out
::
error_out
(
"listen failed."
);
return
false
;}
ascs
::
do_something_to_all
(
sockets
,
[
this
](
typename
Pool
::
object_ctype
&
item
)
{
this
->
do_async_accept
(
item
);});
this
->
start
();
return
true
;
}
virtual
bool
init
()
{
return
start_listen
()
?
(
this
->
start
(),
true
)
:
false
;}
virtual
void
uninit
()
{
this
->
stop
();
stop_listen
();
force_shutdown
();}
//if you wanna graceful shutdown, call graceful_shutdown before service_pump::stop_service invocation.
virtual
bool
on_accept
(
typename
Pool
::
object_ctype
&
socket_ptr
)
{
return
true
;}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录