]>
git.saurik.com Git - wxWidgets.git/blob - src/common/protocol.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/protocol.cpp
3 // Purpose: Implement protocol base class
4 // Author: Guilhem Lavaux
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/protocol/protocol.h"
24 #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
;
49 next
= wxURL::ms_protocols
;
50 wxURL::ms_protocols
= this;
56 /////////////////////////////////////////////////////////////////
57 // wxProtocol ///////////////////////////////////////////////////
58 /////////////////////////////////////////////////////////////////
61 IMPLEMENT_ABSTRACT_CLASS(wxProtocol
, wxSocketClient
)
63 IMPLEMENT_ABSTRACT_CLASS(wxProtocol
, wxObject
)
66 wxProtocol::wxProtocol()
74 bool wxProtocol::Reconnect()
93 // ----------------------------------------------------------------------------
94 // Read a line from socket
95 // ----------------------------------------------------------------------------
98 wxProtocolError
wxProtocol::ReadLine(wxSocketBase
*sock
, wxString
& result
)
100 static const int LINE_BUF
= 4095;
104 wxCharBuffer
buf(LINE_BUF
);
105 char *pBuf
= buf
.data();
106 while ( sock
->WaitForRead() )
108 // peek at the socket to see if there is a CRLF
109 sock
->Peek(pBuf
, LINE_BUF
);
111 size_t nRead
= sock
->LastCount();
112 if ( !nRead
&& sock
->Error() )
113 return wxPROTO_NETERR
;
115 // look for "\r\n" paying attention to a special case: "\r\n" could
116 // have been split by buffer boundary, so check also for \r at the end
117 // of the last chunk and \n at the beginning of this one
119 const char *eol
= strchr(pBuf
, '\n');
121 // if we found '\n', is there a '\r' as well?
126 // check for case of "\r\n" being split
127 if ( result
.empty() || result
.Last() != _T('\r') )
129 // ignore the stray '\n'
132 //else: ok, got real EOL
134 // read just this '\n' and restart
137 else // '\n' in the middle of the buffer
139 // in any case, read everything up to and including '\n'
140 nRead
= eol
- pBuf
+ 1;
142 if ( eol
[-1] != '\r' )
144 // as above, simply ignore stray '\n'
150 sock
->Read(pBuf
, nRead
);
151 if ( sock
->LastCount() != nRead
)
152 return wxPROTO_NETERR
;
155 result
+= wxString::FromAscii(pBuf
);
159 // remove trailing "\r\n"
160 result
.RemoveLast(2);
162 return wxPROTO_NOERR
;
166 return wxPROTO_NETERR
;
169 wxProtocolError
wxProtocol::ReadLine(wxString
& result
)
171 return ReadLine(this, result
);
174 // old function which only chops '\n' and not '\r\n'
175 wxProtocolError
GetLine(wxSocketBase
*sock
, wxString
& result
)
177 #define PROTO_BSIZE 2048
179 char tmp_buf
[PROTO_BSIZE
], tmp_str
[PROTO_BSIZE
];
183 avail
= sock
->Read(tmp_buf
, PROTO_BSIZE
).LastCount();
184 if (sock
->Error() || avail
== 0)
185 return wxPROTO_NETERR
;
187 memcpy(tmp_str
, tmp_buf
, avail
);
189 // Not implemented on all systems
190 // ret = (char *)memccpy(tmp_str, tmp_buf, '\n', avail);
192 for (ret
=tmp_str
;ret
< (tmp_str
+avail
); ret
++)
200 return wxPROTO_PROTERR
;
204 result
= wxString::FromAscii( tmp_str
);
205 result
= result
.Left(result
.length()-1);
207 size
= ret
-tmp_str
+1;
208 sock
->Unread(&tmp_buf
[size
], avail
-size
);
210 return wxPROTO_NOERR
;
213 #endif // wxUSE_SOCKETS
215 #endif // wxUSE_PROTOCOL