Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
F
freeswitch
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
张华
freeswitch
Commits
924e960d
提交
924e960d
authored
4月 09, 2010
作者:
Mathieu Parent
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
perl-skinny: enhance test
- Use thread for: keepalive, receive and send - Run indefinitevly
上级
adcb11f3
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
199 行增加
和
64 行删除
+199
-64
Skinny.pm
src/mod/endpoints/mod_skinny/Net/Skinny.pm
+19
-17
Client.pm
src/mod/endpoints/mod_skinny/Net/Skinny/Client.pm
+94
-0
Message.pm
src/mod/endpoints/mod_skinny/Net/Skinny/Message.pm
+80
-38
Protocol.pm
src/mod/endpoints/mod_skinny/Net/Skinny/Protocol.pm
+1
-2
test-skinny.pl
src/mod/endpoints/mod_skinny/test-skinny.pl
+5
-7
没有找到文件。
src/mod/endpoints/mod_skinny/Net/Skinny.pm
浏览文件 @
924e960d
...
@@ -6,26 +6,25 @@ package Net::Skinny;
...
@@ -6,26 +6,25 @@ package Net::Skinny;
use
strict
;
use
strict
;
use
warnings
;
use
warnings
;
use
IO::
Socket
;
require
IO::
Socket
;
use
Net::Skinny::
Protocol
qw/:all/
;
use
Net::Skinny::
Protocol
qw/:all/
;
our
(
@ISA
);
our
@ISA
=
qw(IO::Socket::INET)
;
@ISA
=
qw(IO::Socket::INET)
;
sub
new
{
sub
new
{
shift
->
SUPER::
new
(
PeerPort
=>
2000
,
@_
);
shift
->
SUPER::
new
(
PeerPort
=>
2000
,
@_
);
}
}
sub
send_
data
sub
send_
raw
{
{
my
$self
=
shift
;
my
$self
=
shift
;
my
$type
=
shift
;
my
$type
=
shift
;
my
$
data
=
shift
;
my
$
raw
=
shift
;
my
$len
=
length
(
$
data
)
+
4
;
my
$len
=
length
(
$
raw
)
+
4
;
printf
"Sending message (length=%d, type=%s (%X))"
,
$len
,
Net::Skinny::Protocol::
skinny_message_type2str
(
$type
),
$type
;
printf
"Sending message (length=%d, type=%s (%X))"
,
$len
,
Net::Skinny::Protocol::
skinny_message_type2str
(
$type
),
$type
;
$self
->
send
(
$self
->
send
(
pack
(
"VVV"
,
$len
,
0
,
$type
)
.
$raw
);
pack
(
"VVV"
,
$len
,
0
,
$type
)
.
$data
);
printf
".\n"
;
printf
".\n"
;
}
}
...
@@ -33,11 +32,8 @@ sub send_message
...
@@ -33,11 +32,8 @@ sub send_message
{
{
my
$self
=
shift
;
my
$self
=
shift
;
my
$type
=
shift
;
my
$type
=
shift
;
return
Net::Skinny::
Message
->
new
(
my
$message
=
Net::Skinny::
Message
->
new
(
$type
,
@_
);
$self
,
return
$self
->
send_raw
(
$message
->
type
(),
$message
->
raw
());
$type
,
@_
)
->
send
();
}
}
sub
receive_message
sub
receive_message
...
@@ -58,20 +54,26 @@ sub receive_message
...
@@ -58,20 +54,26 @@ sub receive_message
printf
"type=%s (%X))"
,
Net::Skinny::Protocol::
skinny_message_type2str
(
$type
),
$type
;
printf
"type=%s (%X))"
,
Net::Skinny::Protocol::
skinny_message_type2str
(
$type
),
$type
;
if
(
$len
>
4
)
{
if
(
$len
>
4
)
{
$self
->
recv
(
$buf
,
$len
-
4
);
$self
->
recv
(
$buf
,
$len
-
4
);
}
else
{
$buf
=
''
;
}
}
printf
".\n"
;
printf
".\n"
;
return
Net::Skinny::
Message
->
new_raw
(
$type
,
$buf
);
}
}
sub
sleep
sub
sleep
{
{
my
$self
=
shift
;
my
$self
=
shift
;
my
$t
=
shift
;
my
$t
=
shift
;
my
%
args
=
@_
;
$args
{
'quiet'
}
=
0
if
not
$args
{
'quiet'
};
printf
"Sleeping %d seconds"
,
$t
;
printf
"Sleeping %d seconds"
,
$t
;
while
(
--
$t
){
while
(
--
$t
){
sleep
(
1
);
sleep
(
1
);
printf
"."
if
$t
%
10
;
if
(
!
$args
{
'quiet'
})
{
printf
"_"
unless
$t
%
10
;
printf
"."
if
$t
%
10
;
printf
"_"
unless
$t
%
10
;
}
}
}
printf
".\n"
;
printf
".\n"
;
}
}
...
...
src/mod/endpoints/mod_skinny/Net/Skinny/Client.pm
0 → 100644
浏览文件 @
924e960d
# Copyright (c) 2010 Mathieu Parent <math.parent@gmail.com>.
# All rights reserved. This program is free software; you can redistribute it
# and/or modify it under the same terms as Perl itself.
package
Net::Skinny::
Client
;
use
strict
;
use
warnings
;
use
Config
;
use
threads
;
use
threads::
shared
;
use
Thread::
Queue
;
require
Net::
Skinny
;
use
Net::Skinny::
Protocol
qw/:all/
;
use
Net::Skinny::
Message
;
our
(
@ISA
);
@ISA
=
qw(Net::Skinny)
;
my
$keep_alive_thread
;
my
$keep_alives
:
shared
;
our
$kept_self
;
my
$messages_send_queue
;
my
$messages_receive_queue
;
$Config
{
useithreads
}
or
die
(
'Recompile Perl with threads to run this program.'
);
sub
new
{
$kept_self
=
shift
->
SUPER::
new
(
@_
);
$messages_send_queue
=
Thread::
Queue
->
new
();
$messages_receive_queue
=
Thread::
Queue
->
new
();
threads
->
create
(
\&
send_messages_thread_func
);
threads
->
create
(
\&
receive_messages_thread_func
);
return
$kept_self
;
}
sub
send_message
{
my
$self
=
shift
;
$messages_send_queue
->
enqueue
(
\
@_
);
}
sub
receive_message
{
my
$self
=
shift
;
my
$message
=
$messages_receive_queue
->
dequeue
();
if
(
$message
->
type
()
==
0x100
)
{
#keepaliveack
if
(
1
)
{
lock
(
$keep_alives
);
$keep_alives
--
;
}
$message
=
$messages_receive_queue
->
dequeue
();
}
return
$message
;
}
sub
launch_keep_alive_thread
{
if
(
!
$keep_alive_thread
)
{
$keep_alive_thread
=
threads
->
create
(
\&
keep_alive_thread_func
);
}
else
{
print
"keep-alive thread is already running\n"
;
}
return
$keep_alive_thread
;
}
sub
keep_alive_thread_func
{
while
(
$kept_self
)
{
if
(
1
)
{
lock
(
$keep_alives
);
$keep_alives
++
;
$kept_self
->
send_message
(
KEEP_ALIVE_MESSAGE
);
}
#mutex unlocked
$kept_self
->
sleep
(
30
,
quiet
=>
0
);
}
}
sub
send_messages_thread_func
{
while
(
my
$message
=
$messages_send_queue
->
dequeue
())
{
my
$type
=
shift
@$message
;
$kept_self
->
SUPER::
send_message
(
$type
,
@$message
);
}
}
sub
receive_messages_thread_func
{
while
(
1
)
{
$messages_receive_queue
->
enqueue
(
$kept_self
->
SUPER::
receive_message
());
}
}
1
;
src/mod/endpoints/mod_skinny/Net/Skinny/Message.pm
浏览文件 @
924e960d
...
@@ -7,54 +7,96 @@ package Net::Skinny::Message;
...
@@ -7,54 +7,96 @@ package Net::Skinny::Message;
use
strict
;
use
strict
;
use
warnings
;
use
warnings
;
use
Net::Skinny::
Protocol
qw/:all/
;
use
threads
;
use
threads::
shared
;
use
Data::
Dumper
;
require
Exporter
;
use
Net::Skinny::
Protocol
qw/:all/
;
our
@ISA
=
qw(Exporter)
;
our
@EXPORT
=
qw(send)
;
sub
new
{
sub
new
_empty
{
my
$class
=
shift
;
my
$class
=
shift
;
my
$self
=
{};
my
$self
=
{};
bless
$self
,
$class
;
bless
$self
,
$class
;
$self
->
{
'socket'
}
=
shift
;
$self
->
{
'type'
}
=
undef
;
$self
->
{
'type'
}
=
shift
;
$self
->
{
'data'
}
=
undef
;
%
{
$self
->
{
'data'
}}
=
@_
;
$self
->
{
'raw'
}
=
undef
;
return
$
self
;
return
$self
;
}
sub
new
{
my
$self
=
shift
->
new_empty
();
$self
->
type
(
shift
);
$self
->
data
(
@_
)
if
@_
;
return
$self
;
}
sub
new_raw
{
my
$self
=
shift
->
new_empty
();
$self
->
type
(
shift
);
$self
->
raw
(
shift
);
return
$self
;
}
sub
type
{
my
$self
=
shift
;
my
$type
=
@_
?
shift
:
undef
;
if
(
defined
(
$type
))
{
$self
->
{
'type'
}
=
$type
;
}
return
$self
->
{
'type'
};
}
sub
data
{
my
$self
=
shift
;
my
@data
=
@_
;
if
(
@data
)
{
%
{
$self
->
{
'data'
}}
=
@data
;
$self
->
{
'raw'
}
=
undef
;
}
elsif
(
!
defined
(
$self
->
{
'data'
}))
{
printf
"Conversion from raw to data not implemented\n"
;
}
return
$self
->
{
'data'
};
}
}
sub
send
{
sub
raw
{
my
$self
=
shift
;
my
$self
=
shift
;
my
$struct
=
Net::Skinny::Protocol::
skinny_message_struct
(
$self
->
{
'type'
});
my
$raw
=
shift
||
undef
;
my
$raw
=
''
;
if
(
defined
(
$raw
))
{
my
$parsed_count
=
0
;
$self
->
{
'raw'
}
=
$raw
;
for
my
$info
(
@$struct
)
{
$self
->
{
'data'
}
=
undef
;
last
if
!
defined
(
$self
->
{
'data'
}{
@$info
[
1
]});
if
(
@$info
[
0
]
eq
'char'
)
{
$raw
.=
pack
(
"a"
.
@$info
[
2
],
$self
->
{
'data'
}{
@$info
[
1
]});
}
elsif
(
@$info
[
0
]
eq
'uint32_t'
)
{
$raw
.=
pack
(
"V"
.
@$info
[
2
],
$self
->
{
'data'
}{
@$info
[
1
]});
}
elsif
(
@$info
[
0
]
eq
'uint16_t'
)
{
$raw
.=
pack
(
"n"
.
@$info
[
2
],
$self
->
{
'data'
}{
@$info
[
1
]});
}
elsif
(
@$info
[
0
]
eq
'struct in_addr'
)
{
$raw
.=
pack
(
"V"
.
@$info
[
2
],
$self
->
{
'data'
}{
@$info
[
1
]});
}
elsif
(
@$info
[
0
]
eq
'struct station_capabilities'
)
{
$raw
.=
$self
->
{
'data'
}{
@$info
[
1
]};
}
else
{
printf
"Unknown type: %s\n"
,
@$info
[
0
];
return
;
}
$parsed_count
++
;
}
}
if
(
$parsed_count
!=
scalar
(
keys
%
{
$self
->
{
'data'
}}))
{
if
(
!
defined
(
$self
->
{
'raw'
}))
{
printf
"Incomplete message (type=%s (%X)) %d out of %d\n"
,
Net::Skinny::Protocol::
skinny_message_type2str
(
$self
->
{
'type'
}),
$self
->
{
'type'
},
my
$struct
=
Net::Skinny::Protocol::
skinny_message_struct
(
$self
->
{
'type'
});
$parsed_count
,
scalar
(
keys
%
{
$self
->
{
'data'
}});
my
$raw
=
''
;
print
Dumper
(
@$struct
);
my
$parsed_count
=
0
;
return
;
for
my
$info
(
@$struct
)
{
last
if
!
defined
(
$self
->
{
'data'
}{
@$info
[
1
]});
if
(
@$info
[
0
]
eq
'char'
)
{
$raw
.=
pack
(
"a"
.
@$info
[
2
],
$self
->
{
'data'
}{
@$info
[
1
]});
}
elsif
(
@$info
[
0
]
eq
'uint32_t'
)
{
$raw
.=
pack
(
"V"
.
@$info
[
2
],
$self
->
{
'data'
}{
@$info
[
1
]});
}
elsif
(
@$info
[
0
]
eq
'uint16_t'
)
{
$raw
.=
pack
(
"n"
.
@$info
[
2
],
$self
->
{
'data'
}{
@$info
[
1
]});
}
elsif
(
@$info
[
0
]
eq
'struct in_addr'
)
{
$raw
.=
pack
(
"V"
.
@$info
[
2
],
$self
->
{
'data'
}{
@$info
[
1
]});
}
elsif
(
@$info
[
0
]
eq
'struct station_capabilities'
)
{
$raw
.=
$self
->
{
'data'
}{
@$info
[
1
]};
}
else
{
printf
"Unknown type: %s\n"
,
@$info
[
0
];
return
;
}
$parsed_count
++
;
}
if
(
$parsed_count
!=
scalar
(
keys
%
{
$self
->
{
'data'
}}))
{
printf
"Incomplete message (type=%s (%X)) %d out of %d\n"
,
Net::Skinny::Protocol::
skinny_message_type2str
(
$self
->
{
'type'
}),
$self
->
{
'type'
},
$parsed_count
,
scalar
(
keys
%
{
$self
->
{
'data'
}});
return
;
}
$self
->
{
'raw'
}
=
$raw
;
}
}
$self
->
{
'socket'
}
->
send_data
(
$self
->
{
'type'
},
$raw
)
;
return
$self
->
{
'raw'
}
;
}
}
1
;
1
;
src/mod/endpoints/mod_skinny/Net/Skinny/Protocol.pm
浏览文件 @
924e960d
...
@@ -8,7 +8,6 @@ use strict;
...
@@ -8,7 +8,6 @@ use strict;
no
strict
"refs"
;
no
strict
"refs"
;
use
warnings
;
use
warnings
;
use
Carp
;
use
Carp
;
use
Data::
Dumper
;
require
Exporter
;
require
Exporter
;
our
@ISA
=
qw(Exporter)
;
our
@ISA
=
qw(Exporter)
;
...
@@ -69,7 +68,6 @@ sub _find {
...
@@ -69,7 +68,6 @@ sub _find {
printf
"Unparsed line '%s' in %s\n"
,
$_
,
$struct_name
;
printf
"Unparsed line '%s' in %s\n"
,
$_
,
$struct_name
;
}
}
}
}
#print "$name: ".Dumper($struct{$name});
}
}
}
}
@sub
{
@_
};
@sub
{
@_
};
...
@@ -77,6 +75,7 @@ sub _find {
...
@@ -77,6 +75,7 @@ sub _find {
sub
skinny_message_type2str
{
sub
skinny_message_type2str
{
my
$message_type
=
shift
;
my
$message_type
=
shift
;
return
"UndefinedTypeMessage"
if
!
defined
(
$message_type
);
keys
%
const
;
keys
%
const
;
while
(
my
(
$key
,
$value
)
=
each
%
const
)
{
while
(
my
(
$key
,
$value
)
=
each
%
const
)
{
...
...
src/mod/endpoints/mod_skinny/test-skinny.pl
浏览文件 @
924e960d
...
@@ -15,6 +15,7 @@ use Sys::Hostname;
...
@@ -15,6 +15,7 @@ use Sys::Hostname;
use
Net::
Skinny
;
use
Net::
Skinny
;
use
Net::Skinny::
Protocol
qw/:all/
;
use
Net::Skinny::
Protocol
qw/:all/
;
use
Net::Skinny::
Message
;
use
Net::Skinny::
Message
;
use
Net::Skinny::
Client
;
#Config
#Config
my
$skinny_server
=
hostname
;
my
$skinny_server
=
hostname
;
...
@@ -23,13 +24,13 @@ my $device_ip = 10+256*(11+256*(12+256*13)); # 10.11.12.13
...
@@ -23,13 +24,13 @@ my $device_ip = 10+256*(11+256*(12+256*13)); # 10.11.12.13
#======
#======
$|
=
1
;
$|
=
1
;
my
$socket
=
Net::
Skinny
->
new
(
my
$socket
=
Net::Skinny
::
Client
->
new
(
PeerAddr
=>
$skinny_server
,
PeerAddr
=>
$skinny_server
,
PeerPort
=>
2000
,
PeerPort
=>
2000
,
);
);
if
(
!
$socket
)
{
if
(
!
$socket
)
{
print
"Unable to connect to server\n"
;
print
f
"Unable to connect to server %s\n"
,
$skinny_server
;
exit
1
;
exit
1
;
}
}
# =============================================================================
# =============================================================================
...
@@ -84,11 +85,8 @@ $socket->send_message(
...
@@ -84,11 +85,8 @@ $socket->send_message(
count
=>
2
count
=>
2
);
);
for
(
my
$i
=
0
;
$i
<
1
;
$i
++
)
{
$socket
->
launch_keep_alive_thread
();
$socket
->
sleep
(
5
);
$socket
->
send_message
(
KEEP_ALIVE_MESSAGE
);
$socket
->
receive_message
();
# keepaliveack
}
$socket
->
sleep
(
5
);
$socket
->
sleep
(
5
);
#NewCall
#NewCall
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论