Add libwx-perl
[pkg-perl] / deb-src / libwx-perl / libwx-perl-0.96 / blib / lib / Wx / Socket.pm
1 #############################################################################
2 ## Name:        ext/socket/lib/Wx/Socket.pm
3 ## Purpose:     Wx::Socket
4 ## Author:      Graciliano M. P.
5 ## Modified by:
6 ## Created:     27/02/2003
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 #############################################################################
12
13 package Wx::Socket ;
14
15 use Wx;
16 use strict;
17
18 use vars qw($VERSION);
19
20 $VERSION = '0.01';
21
22 Wx::load_dll( 'net' );
23 Wx::wx_boot( 'Wx::Socket', $VERSION );
24
25 no strict ;
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);
34 use strict ;
35
36 #####################
37 # WX::SOCKET::EVENT #
38 #####################
39
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) ;
44
45 my $EVTID ;
46
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,@_) ;}
53
54 sub MAKE_EVT {
55   my $type = shift ;
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 ;
63   }
64 }
65
66 sub ENABLE_SKEVT {
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) ;
73   $sock->Notify(1) ;
74   $sock->{_WXEVT}{ENABLE} = 1 ;
75 }
76
77 sub CHECK_EVT_TYPE {
78   my ( $sock , $this , $evt ) = @_ ;
79   #print "$sock\n" ;
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) ;}
83   return( undef ) ;
84 }
85
86 #######
87 # END #
88 #######
89
90 1;
91
92 __END__
93
94 =head1 NAME
95
96 Wx::Socket - wxSocket* classes
97
98 =head1 USAGE
99
100   use Wx qw(:socket) ;
101   use Wx::Event qw(EVT_SOCKET_INPUT EVT_SOCKET_LOST) ;
102   use Wx::Event qw(EVT_SOCKET_CONNECTION) ;
103
104   ##########
105   # CLIENT #
106   ##########
107
108   my $sock = Wx::SocketClient->new(wxSOCKET_WAITALL);
109
110   EVT_SOCKET_INPUT($parent , $sock , \&onInput ) ;
111   EVT_SOCKET_LOST($parent , $sock , \&onClose ) ;
112
113   $sock->Connect('localhost',5050) ;
114
115   if (! $sock->IsConnected ) { print "ERROR\n" ;}
116
117   sub onInput {
118     my ( $sock , $this , $evt ) = @_ ;
119     my $length = 123;
120     my $buffer ;
121     $sock->Read($buffer , 1024 , $length ) ;
122   }
123
124   ##########
125   # SERVER #
126   ##########
127
128   my $sock = Wx::SocketServer->new('localhost',5050,wxSOCKET_WAITALL);
129
130   EVT_SOCKET_CONNECTION($parent , $sock , \&onConnect ) ;
131
132   if ( !$sock->Ok ) { print "ERROR\n" ;}
133
134   sub onConnect {
135     my ( $sock , $this , $evt ) = @_ ;
136     my $client = $sock->Accept(0) ;
137
138     my ($local_host,$local_port) = $client->GetLocal ;
139     my ($peer_host,$peer_port) = $client->GetPeer ;
140
141     $client->Write("This is a data test!\n") ;
142
143 ... or ...
144
145     $client->Write( $data , length($data) ) ;
146
147     $client->Close ;
148   }
149
150 =head1 METHODS
151
152 All the methods work as in wxWidgets (see the documentation).
153
154 The functions for reading data (Read, ReadMsg, Peek) take 3 arguments,
155 like the Perl read() function:
156
157   ## To read the data into the variable
158   $sock->Read($buffer , 1024) ;
159
160 ... or ...
161
162   ## To append data at the given offset:
163   $sock->Read($buffer , 1024 , $offset ) ;
164
165 The write functions (Write, WriteMsg, Unread) can be used with
166 1 or 2 arguments:
167
168   $client->Write("This is a data test!\n") ;
169
170   $client->Write($data , $length) ;
171
172 =head1 EVENTS
173
174 The events are:
175
176     EVT_SOCKET
177     EVT_SOCKET_ALL
178     EVT_SOCKET_INPUT
179     EVT_SOCKET_OUTPUT
180     EVT_SOCKET_CONNECTION
181     EVT_SOCKET_LOST
182
183 The EVT_SOCKET works as in wxWidgets, the others are wxPerl extensions.
184
185 Note that EVT_SOCKET events of wxSocketClient and wxSocketServer
186 work differently than other event types.
187
188 First you need to set the event handler:
189
190     $sock->SetEventHandler($handler, $id) ;
191
192 Then you set what types of event you want to receive:
193
194     ## this select all.
195     $sock->SetNotify(wxSOCKET_INPUT_FLAG|wxSOCKET_OUTPUT_FLAG|
196                      wxSOCKET_CONNECTION_FLAG|wxSOCKET_LOST_FLAG) ;
197
198 Enable the event notification:
199
200     $sock->Notify(1) ;
201
202 And only after this use:
203
204     ## note that $handler must be the same that was used in
205     ## SetEventHandler
206     EVT_SOCKET($handler, $id , sub{...} )
207
208 To make the events easier to use, all the proccess is automatic,
209 and you just use:
210
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{...} )
215
216     ## This is for the events not used yet by the above:
217     EVT_SOCKET_ALL($parent , $socket , sub{...} )
218
219 ** The new way is better to handle more than one socket in the same time too.
220    Take a look in the demos.
221
222 =head1 SEE ALSO
223
224 L<Wx>, The wxWxwindows documentation at L<http://www.wxwindows.org/>
225
226 =head1 AUTHOR
227
228 Graciliano M. P. <gm@virtuasites.com.br>
229
230 =head1 COPYRIGHT
231
232 This program is free software; you can redistribute it and/or
233 modify it under the same terms as Perl itself.
234
235 =cut