]>
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 licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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()
89 // ----------------------------------------------------------------------------
90 // Read a line from socket
91 // ----------------------------------------------------------------------------
93 // TODO ReadLine() should use buffers private to wxProtocol for efficiency!
96 wxProtocolError
wxProtocol::ReadLine(wxSocketBase
*socket
, wxString
& result
)
99 char ch
, chLast
= '\0';
100 while ( !socket
->Read(&ch
, sizeof(ch
)).Error() )
105 // remember it, if the following is '\n', we're done
110 // only ends line if the previous character was '\r'
111 if ( chLast
== '\r' )
114 return wxPROTO_NOERR
;
122 result
+= wxString::FromAscii( chLast
);
126 result
+= wxString::FromAscii( ch
);
130 return wxPROTO_NETERR
;
133 wxProtocolError
wxProtocol::ReadLine(wxString
& result
)
135 return ReadLine(this, result
);
138 // old function which only chops '\n' and not '\r\n'
139 wxProtocolError
GetLine(wxSocketBase
*sock
, wxString
& result
)
141 #define PROTO_BSIZE 2048
143 char tmp_buf
[PROTO_BSIZE
], tmp_str
[PROTO_BSIZE
];
147 avail
= sock
->Read(tmp_buf
, PROTO_BSIZE
).LastCount();
148 if (sock
->Error() || avail
== 0)
149 return wxPROTO_NETERR
;
151 memcpy(tmp_str
, tmp_buf
, avail
);
153 // Not implemented on all systems
154 // ret = (char *)memccpy(tmp_str, tmp_buf, '\n', avail);
156 for (ret
=tmp_str
;ret
< (tmp_str
+avail
); ret
++)
164 return wxPROTO_PROTERR
;
168 result
= wxString::FromAscii( tmp_str
);
169 result
= result
.Left(result
.Length()-1);
171 size
= ret
-tmp_str
+1;
172 sock
->Unread(&tmp_buf
[size
], avail
-size
);
174 return wxPROTO_NOERR
;
177 #endif // wxUSE_SOCKETS
179 #endif // wxUSE_PROTOCOL