1 #############################################################################
2 ## Name: ext/socket/lib/Wx/Socket.pm
4 ## Author: Graciliano M. P.
7 ## RCS-ID: $Id: Socket.pm 2057 2007-06-18 23:03:00Z mbarbon $
8 ## Copyright: (c) 2003-2004 Graciliano M. P.
9 ## Licence: This program is free software; you can redistribute it and/or
10 ## modify it under the same terms as Perl itself
11 #############################################################################
18 use vars qw($VERSION);
22 Wx::load_dll( 'net' );
23 Wx::wx_boot( 'Wx::Socket', $VERSION );
26 package Wx::DatagramSocket ; @ISA = qw(Wx::SocketBase) ;
27 package Wx::SocketClient ; @ISA = qw(Wx::SocketBase) ;
28 package Wx::SocketServer ; @ISA = qw(Wx::SocketBase) ;
29 package Wx::SocketEvent ; @ISA = qw(Wx::Event) ;
30 package Wx::IPaddress ; @ISA = qw(Wx::SockAddress);
31 package Wx::IPV4address ; @ISA = qw(Wx::IPaddress);
32 package Wx::IPV6address ; @ISA = qw(Wx::IPaddress);
33 package Wx::UNIXaddress ; @ISA = qw(Wx::SockAddress);
40 package Wx::Socket::Event ;
41 use Wx qw(wxSOCKET_INPUT_FLAG wxSOCKET_OUTPUT_FLAG wxSOCKET_CONNECTION_FLAG
42 wxSOCKET_LOST_FLAG wxSOCKET_INPUT wxSOCKET_OUTPUT
43 wxSOCKET_CONNECTION wxSOCKET_LOST) ;
47 sub EVT_SOCKET($$$) { $_[0]->Connect($_[1] , -1, &Wx::wxEVT_SOCKET , $_[2] ); }
48 sub EVT_SOCKET_ALL($$$) { &MAKE_EVT('all',@_) ;}
49 sub EVT_SOCKET_INPUT($$$) { &MAKE_EVT(wxSOCKET_INPUT,@_) ;}
50 sub EVT_SOCKET_OUTPUT($$$) { &MAKE_EVT(wxSOCKET_OUTPUT,@_) ;}
51 sub EVT_SOCKET_CONNECTION($$$) { &MAKE_EVT(wxSOCKET_CONNECTION,@_) ;}
52 sub EVT_SOCKET_LOST($$$) { &MAKE_EVT(wxSOCKET_LOST,@_) ;}
56 my( $handler, $sock, $callback ) = @_;
57 &ENABLE_SKEVT($sock , $handler , $callback) ;
58 $sock->{_WXEVT}{SUB}{$type} = $callback ;
59 if (!$sock->{_WXEVT}{CONNECT}) {
60 $handler->Connect( $sock->{_WXEVT}{ID} , -1 , &Wx::wxEVT_SOCKET ,
61 sub{ &CHECK_EVT_TYPE($sock,@_) } );
62 $sock->{_WXEVT}{CONNECT} = 1 ;
67 my ( $sock , $parent ) = @_ ;
68 if ( $sock->{_WXEVT}{ENABLE} ) { return ;}
69 $sock->{_WXEVT}{ID} = ++$EVTID ;
70 $sock->SetEventHandler($parent, $sock->{_WXEVT}{ID}) ;
71 $sock->SetNotify(wxSOCKET_INPUT_FLAG|wxSOCKET_OUTPUT_FLAG|
72 wxSOCKET_CONNECTION_FLAG|wxSOCKET_LOST_FLAG) ;
74 $sock->{_WXEVT}{ENABLE} = 1 ;
78 my ( $sock , $this , $evt ) = @_ ;
80 my $evt_type = $evt->GetSocketEvent ;
81 my $sub = $sock->{_WXEVT}{SUB}{$evt_type} || $sock->{_WXEVT}{SUB}{all} ;
82 if ($sub) { return &$sub($sock , $this , $evt) ;}
96 Wx::Socket - wxSocket* classes
101 use Wx::Event qw(EVT_SOCKET_INPUT EVT_SOCKET_LOST) ;
102 use Wx::Event qw(EVT_SOCKET_CONNECTION) ;
108 my $sock = Wx::SocketClient->new(wxSOCKET_WAITALL);
110 EVT_SOCKET_INPUT($parent , $sock , \&onInput ) ;
111 EVT_SOCKET_LOST($parent , $sock , \&onClose ) ;
113 $sock->Connect('localhost',5050) ;
115 if (! $sock->IsConnected ) { print "ERROR\n" ;}
118 my ( $sock , $this , $evt ) = @_ ;
121 $sock->Read($buffer , 1024 , $length ) ;
128 my $sock = Wx::SocketServer->new('localhost',5050,wxSOCKET_WAITALL);
130 EVT_SOCKET_CONNECTION($parent , $sock , \&onConnect ) ;
132 if ( !$sock->Ok ) { print "ERROR\n" ;}
135 my ( $sock , $this , $evt ) = @_ ;
136 my $client = $sock->Accept(0) ;
138 my ($local_host,$local_port) = $client->GetLocal ;
139 my ($peer_host,$peer_port) = $client->GetPeer ;
141 $client->Write("This is a data test!\n") ;
145 $client->Write( $data , length($data) ) ;
152 All the methods work as in wxWidgets (see the documentation).
154 The functions for reading data (Read, ReadMsg, Peek) take 3 arguments,
155 like the Perl read() function:
157 ## To read the data into the variable
158 $sock->Read($buffer , 1024) ;
162 ## To append data at the given offset:
163 $sock->Read($buffer , 1024 , $offset ) ;
165 The write functions (Write, WriteMsg, Unread) can be used with
168 $client->Write("This is a data test!\n") ;
170 $client->Write($data , $length) ;
180 EVT_SOCKET_CONNECTION
183 The EVT_SOCKET works as in wxWidgets, the others are wxPerl extensions.
185 Note that EVT_SOCKET events of wxSocketClient and wxSocketServer
186 work differently than other event types.
188 First you need to set the event handler:
190 $sock->SetEventHandler($handler, $id) ;
192 Then you set what types of event you want to receive:
195 $sock->SetNotify(wxSOCKET_INPUT_FLAG|wxSOCKET_OUTPUT_FLAG|
196 wxSOCKET_CONNECTION_FLAG|wxSOCKET_LOST_FLAG) ;
198 Enable the event notification:
202 And only after this use:
204 ## note that $handler must be the same that was used in
206 EVT_SOCKET($handler, $id , sub{...} )
208 To make the events easier to use, all the proccess is automatic,
211 EVT_SOCKET_INPUT($handler , $socket , sub{...} )
212 EVT_SOCKET_OUTPUT($handler , $socket , sub{...} )
213 EVT_SOCKET_CONNECTION($handler , $socket , sub{...} )
214 EVT_SOCKET_LOST($handler , $socket , sub{...} )
216 ## This is for the events not used yet by the above:
217 EVT_SOCKET_ALL($parent , $socket , sub{...} )
219 ** The new way is better to handle more than one socket in the same time too.
220 Take a look in the demos.
224 L<Wx>, The wxWxwindows documentation at L<http://www.wxwindows.org/>
228 Graciliano M. P. <gm@virtuasites.com.br>
232 This program is free software; you can redistribute it and/or
233 modify it under the same terms as Perl itself.