]>
git.saurik.com Git - wxWidgets.git/blob - src/common/protocol.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implement protocol base class
4 // Author: Guilhem Lavaux
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "protocol.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/protocol/protocol.h"
27 #include "wx/module.h"
31 /////////////////////////////////////////////////////////////////
33 /////////////////////////////////////////////////////////////////
36 * --------------------------------------------------------------
37 * --------- wxProtoInfo CONSTRUCTOR ----------------------------
38 * --------------------------------------------------------------
41 wxProtoInfo::wxProtoInfo(const wxChar
*name
, const wxChar
*serv
,
42 const bool need_host1
, wxClassInfo
*info
)
47 m_needhost
= need_host1
;
48 next
= wxURL::ms_protocols
;
49 wxURL::ms_protocols
= this;
52 /////////////////////////////////////////////////////////////////
53 // wxProtocol ///////////////////////////////////////////////////
54 /////////////////////////////////////////////////////////////////
57 IMPLEMENT_ABSTRACT_CLASS(wxProtocol
, wxSocketClient
)
59 IMPLEMENT_ABSTRACT_CLASS(wxProtocol
, wxObject
)
62 wxProtocol::wxProtocol()
70 bool wxProtocol::Reconnect()
87 // ----------------------------------------------------------------------------
88 // Read a line from socket
89 // ----------------------------------------------------------------------------
91 // TODO ReadLine() should use buffers private to wxProtocol for efficiency!
94 wxProtocolError
wxProtocol::ReadLine(wxSocketBase
*socket
, wxString
& result
)
97 char ch
, chLast
= '\0';
98 while ( !socket
->Read(&ch
, sizeof(ch
)).Error() )
103 // remember it, if the following is '\n', we're done
108 // only ends line if the previous character was '\r'
109 if ( chLast
== '\r' )
112 return wxPROTO_NOERR
;
128 return wxPROTO_NETERR
;
131 wxProtocolError
wxProtocol::ReadLine(wxString
& result
)
133 return ReadLine(this, result
);
136 // old function which only chops '\n' and not '\r\n'
137 wxProtocolError
GetLine(wxSocketBase
*sock
, wxString
& result
) {
138 #define PROTO_BSIZE 2048
140 char tmp_buf
[PROTO_BSIZE
], tmp_str
[PROTO_BSIZE
];
144 avail
= sock
->Read(tmp_buf
, PROTO_BSIZE
).LastCount();
145 if (sock
->Error() || avail
== 0)
146 return wxPROTO_NETERR
;
148 memcpy(tmp_str
, tmp_buf
, avail
);
150 // Not implemented on all systems
151 // ret = (char *)memccpy(tmp_str, tmp_buf, '\n', avail);
153 for (ret
=tmp_str
;ret
< (tmp_str
+avail
); ret
++)
160 return wxPROTO_PROTERR
;
164 result
= result
.Left(result
.Length()-1);
166 size
= ret
-tmp_str
+1;
167 sock
->Unread(&tmp_buf
[size
], avail
-size
);
168 return wxPROTO_NOERR
;
171 #endif // wxUSE_SOCKETS
173 #endif // wxUSE_PROTOCOL