- 论坛徽章:
- 0
|
应用场景如下:
服务器启动一个端口监听8000端口等待用户连接,当用户连接后用在线程中向客户机发送信息,因为是群播,所以就是在线程中遍历所有的连接句柄然后发送信息,所有的句柄信息都保存在%CLIENTS这个变量中,而%CLIENTS在线程中无法共享,所以也就没有客户的连接句柄可以使用,意味着说线程也就没法向客户端发送信息,使用threads::shared也无法共享%CLIENTS
目前客户连接和线程的过程如下
- #!/usr/bin/perl -w
- use EV;
- use AnyEvent;
- use AnyEvent::Handle;
- use AnyEvent::Socket;
- use threads;
- use threads::shared;
- use Thread::Queue;
- my $blockQueue = Thread::Queue->new();
- my $thr = threads->create(sub {
- while (my $block = $blockQueue->dequeue()) {
- $log->debug(Dumper(%CLIENTS));
- }
- }
- )->detach();
-
- tcp_server undef, 8000 , \&new_client;
- EV::loop;
- #Handles new connections
- sub new_client {
- my ( $fh, $host, $port ) = @_;
- my $handle = new AnyEvent::Handle(
- fh => $fh,
- );
- $handle->on_read( \&client_input );
- $CLIENTS{$handle} = $handle;
-
- }
- sub client_input {
- return 1;
- }
复制代码
如何共享%CLIENTS的变量信息 |
|