Add libwx-perl
[pkg-perl] / deb-src / libwx-perl / libwx-perl-0.96 / ext / socket / cpp / socket.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        ext/socket/cpp/socket.h
3 // Purpose:     c++ wrapper for wxSocket*
4 // Author:      Graciliano M. P.
5 // Modified by:
6 // Created:     06/03/2003
7 // RCS-ID:      $Id: socket.h 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 #include "wx/socket.h"
14 #include "cpp/v_cback.h"
15
16 #define DO_WRITE(CODE, sv, size)                                             \
17     if ( size == 0 ) size = SvCUR(sv) ;                                      \
18     char* buffer = SvPV_nolen(sv);                                           \
19     CODE ;                                                                   \
20     RETVAL = THIS->LastCount() ;
21
22 #define DO_READ(CODE, sv, size, offset)                                      \
23     /* Upgrade the SV to scalar if needed. If the scalar is undef */         \
24     /* can't use SvGROW. */                                                  \
25     SvUPGRADE(sv , SVt_PV) ;                                                 \
26     /* Tell that the scalar is string only (not integer, float, utf8...): */ \
27     SvPOK_only(sv) ;                                                         \
28     /* Grow the scalar to receive the data and return a char* point: */      \
29     char* buffer = SvGROW( sv , offset + size + 2 ) ;                        \
30     /* To read at the offset the user specified */                           \
31     if ( offset > 0 ) buffer += offset ;                                     \
32                                                                              \
33     CODE ;                                                                   \
34     int nread = THIS->LastCount() ;                                          \
35                                                                              \
36     /* Null-terminate the buffer, not necessary, but does not hurt: */       \
37     buffer[nread] = 0 ;                                                      \
38     /* Tell Perl how long the string is: */                                  \
39     SvCUR_set( sv , offset + nread ) ;                                       \
40     /* Undef on read error: */                                               \
41     if( THIS->Error() ) XSRETURN_UNDEF ;                                     \
42     /* Return the amount of data read, like Perl read(). */                  \
43     RETVAL = nread ;
44
45 class wxPlSocketBase:public wxSocketBase
46 {
47     WXPLI_DECLARE_DYNAMIC_CLASS( wxPlSocketBase );
48     WXPLI_DECLARE_V_CBACK();
49 public:
50     wxPlSocketBase( const char* package )
51         : m_callback( "Wx::SocketBase" )
52     {
53         m_callback.SetSelf( wxPli_make_object( this, package ), true );
54     }
55 };
56
57 WXPLI_IMPLEMENT_DYNAMIC_CLASS( wxPlSocketBase , wxSocketBase );
58
59 ///////////////////////////////////////////////////////////////////////////////
60
61 class wxPliSocketClient:public wxSocketClient
62 {
63     WXPLI_DECLARE_DYNAMIC_CLASS( wxPliSocketClient );
64     WXPLI_DECLARE_V_CBACK();
65 public:
66     WXPLI_DEFAULT_CONSTRUCTOR( wxPliSocketClient, "Wx::SocketClient", true );
67
68     // this fixes the crashes, for some reason
69     wxPliSocketClient( const char* package, long _arg1 )
70         : wxSocketClient( _arg1 ),
71           m_callback( "Wx::SocketClient" )
72     {
73         m_callback.SetSelf( wxPli_make_object( this, package ), true );
74     }
75 };
76
77 WXPLI_IMPLEMENT_DYNAMIC_CLASS( wxPliSocketClient , wxSocketClient );
78
79 ///////////////////////////////////////////////////////////////////////////////
80
81 class wxPlSocketServer:public wxSocketServer
82 {
83     WXPLI_DECLARE_DYNAMIC_CLASS( wxPlSocketServer );
84     WXPLI_DECLARE_V_CBACK();
85 public:
86     wxPlSocketServer( const char* package , wxIPV4address _arg1 , long _arg2 )
87         : wxSocketServer( _arg1 , _arg2 ),
88           m_callback( "Wx::SocketServer" )
89     {
90         m_callback.SetSelf( wxPli_make_object( this, package ), true );
91     }
92
93     wxSocketBase* Accept(bool wait)
94     {
95         wxSocketBase* sock = new wxPlSocketBase( "Wx::SocketBase" );
96
97         sock->SetFlags(GetFlags());
98
99         if (!AcceptWith(*sock, wait))
100         {
101             sock->Destroy();
102             sock = NULL;
103         }
104
105         return sock;
106     }
107 };
108
109 WXPLI_IMPLEMENT_DYNAMIC_CLASS( wxPlSocketServer , wxSocketServer );
110
111 ///////////////////////////////////////////////////////////////////////////////
112
113 class wxPliDatagramSocket : public wxDatagramSocket
114 {
115     WXPLI_DECLARE_DYNAMIC_CLASS( wxPliDatagramSocket );
116     WXPLI_DECLARE_V_CBACK();
117 public:
118 //    WXPLI_DEFAULT_CONSTRUCTOR( wxPliDatagramSocket,
119 //                               "Wx::DatagramSocket", true );
120
121     // this fixes the crashes, for some reason
122     wxPliDatagramSocket( const char* package, wxSockAddress& _arg1,
123                          wxSocketFlags _arg2 )
124         : wxDatagramSocket( _arg1, _arg2 ),
125           m_callback( "Wx::SocketClient" )
126     {
127         m_callback.SetSelf( wxPli_make_object( this, package ), true );
128     }
129 };
130
131 WXPLI_IMPLEMENT_DYNAMIC_CLASS( wxPliDatagramSocket , wxDatagramSocket );
132
133 // local variables:
134 // mode: c++
135 // end: