From 375abe3dac3d7816738ccd1ab495d9a0441550d4 Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Sat, 27 Feb 1999 21:01:05 +0000 Subject: [PATCH] * wxSocket fixes: FTP, HTTP works really now. GTK fixes to prevent infinite loop. * wxSocket stream fix: they can be encapsulated by now * Doc update: wxURL::GetPath git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1818 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/url.tex | 9 +++++++++ include/wx/sckstrm.h | 4 ++++ include/wx/socket.h | 5 ++++- include/wx/url.h | 1 + samples/wxsocket/client.cpp | 2 +- src/common/ftp.cpp | 8 ++++++-- src/common/sckstrm.cpp | 10 ++++++++++ src/common/socket.cpp | 32 ++++++++++++++++++++++++++++++-- src/common/url.cpp | 3 +-- 9 files changed, 66 insertions(+), 8 deletions(-) diff --git a/docs/latex/wx/url.tex b/docs/latex/wx/url.tex index 5b9963ce1f..fd05953aad 100644 --- a/docs/latex/wx/url.tex +++ b/docs/latex/wx/url.tex @@ -62,6 +62,15 @@ Returns the name of the protocol which will be used to get the URL. Returns a reference to the protocol which will be used to get the URL. +% +% GetPath +% +\membersection{wxURL::GetPath} + +\func{wxString}{GetPath}{\void} + +Returns the path of the file to fetch. This path was encoded in the URL. + % % GetError % diff --git a/include/wx/sckstrm.h b/include/wx/sckstrm.h index 0f05c8c3f7..57484b17fc 100644 --- a/include/wx/sckstrm.h +++ b/include/wx/sckstrm.h @@ -32,6 +32,8 @@ class WXDLLEXPORT wxSocketOutputStream : public wxOutputStream protected: wxSocketBase *m_o_socket; + + size_t OnSysWrite(const void *buffer, size_t bufsize); }; class WXDLLEXPORT wxSocketInputStream : public wxInputStream @@ -48,6 +50,8 @@ class WXDLLEXPORT wxSocketInputStream : public wxInputStream protected: wxSocketBase *m_i_socket; + + size_t OnSysRead(void *buffer, size_t bufsize); }; class WXDLLEXPORT wxSocketStream : public wxSocketInputStream, diff --git a/include/wx/socket.h b/include/wx/socket.h index ebd5a5ecd6..174c763734 100644 --- a/include/wx/socket.h +++ b/include/wx/socket.h @@ -81,7 +81,10 @@ class WXDLLEXPORT wxSocketBase : public wxEvtHandler { DECLARE_CLASS(wxSocketBase) #ifdef __WXMAC__ -friend void wxMacSocketOnRequestProc(void *refcon , short event) ; + friend void wxMacSocketOnRequestProc(void *refcon , short event) ; +#endif +#if defined(__WXGTK__) && defined(WXSOCK_INTERNAL) + friend void wxPrereadSocket(wxSocketBase *sock); #endif public: diff --git a/include/wx/url.h b/include/wx/url.h index f37d4a9d90..aa224f7f3b 100644 --- a/include/wx/url.h +++ b/include/wx/url.h @@ -62,6 +62,7 @@ public: { return m_protoinfo->m_protoname; } inline wxProtocol& GetProtocol() { return *m_protocol; } inline wxURLError GetError() const { return m_error; } + inline wxString GetPath() const { return m_path; } wxInputStream *GetInputStream(); diff --git a/samples/wxsocket/client.cpp b/samples/wxsocket/client.cpp index e4a57fba5e..27051c68a8 100644 --- a/samples/wxsocket/client.cpp +++ b/samples/wxsocket/client.cpp @@ -235,7 +235,7 @@ void MyFrame::OnExecTest1(wxCommandEvent& WXUNUSED(evt)) wxYield(); /* Init */ - buf = copystring("Salut ! Salut ! Salut ! Salut Toto\n"); + buf = copystring("Hi ! Hi ! Hi !\n"); buf2 = new char[strlen(buf)+1]; char c = 0xbe; sock->WriteMsg(&c, 1); diff --git a/src/common/ftp.cpp b/src/common/ftp.cpp index d0b2a80211..3f15d7620f 100644 --- a/src/common/ftp.cpp +++ b/src/common/ftp.cpp @@ -253,7 +253,7 @@ public: wxInputFTPStream(wxFTP *ftp_clt, wxSocketBase *sock) : wxSocketInputStream(*sock), m_ftp(ftp_clt) {} - size_t StreamSize() { return m_ftpsize; } + size_t StreamSize() const { return m_ftpsize; } virtual ~wxInputFTPStream(void) { if (LastError() != wxStream_NOERROR) @@ -351,7 +351,7 @@ wxInputStream *wxFTP::GetInputStream(const wxString& path) pos_size = m_lastResult.Index('('); if (pos_size != wxNOT_FOUND) { - wxString str_size = m_lastResult(pos_size, m_lastResult.Index(')')); + wxString str_size = m_lastResult(pos_size+1, m_lastResult.Index(')')-1); in_stream->m_ftpsize = atoi(WXSTRINGCAST str_size); } @@ -401,5 +401,9 @@ wxList *wxFTP::GetList(const wxString& wildcard) return NULL; } + sock->SetEventHandler(*GetNextHandler(), m_id); + sock->Notify(m_notifyme); + sock->SetNotify(m_neededreq); + return file_list; } diff --git a/src/common/sckstrm.cpp b/src/common/sckstrm.cpp index 85562ae51c..8dde4ba92e 100644 --- a/src/common/sckstrm.cpp +++ b/src/common/sckstrm.cpp @@ -45,6 +45,11 @@ wxOutputStream& wxSocketOutputStream::Write(const void *buffer, size_t size) return *this; } +size_t wxSocketOutputStream::OnSysWrite(const void *buffer, size_t size) +{ + return m_o_socket->Write((const char *)buffer, size).LastCount(); +} + // --------------------------------------------------------------------------- // wxSocketInputStream // --------------------------------------------------------------------------- @@ -64,6 +69,11 @@ wxInputStream& wxSocketInputStream::Read(void *buffer, size_t size) return *this; } +size_t wxSocketInputStream::OnSysRead(void *buffer, size_t size) +{ + return m_i_socket->Read((char *)buffer, size).LastCount(); +} + // --------------------------------------------------------------------------- // wxSocketStream // --------------------------------------------------------------------------- diff --git a/src/common/socket.cpp b/src/common/socket.cpp index e9a222b142..2968f4fcd3 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -363,8 +363,10 @@ wxSocketBase& wxSocketBase::Read(char* buffer, size_t nbytes) // If we have got the whole needed buffer or if we don't want to // wait then it returns immediately. - if (!nbytes || (m_lcount && !(m_flags & WAITALL)) ) + if (!nbytes || (count && !(m_flags & WAITALL)) ) { + m_lcount = count; return *this; + } m_lcount = 0; WantBuffer(buffer, nbytes, EVT_READ); @@ -681,6 +683,20 @@ bool wxSocketBase::WaitForLost(long seconds, long microseconds) // --------- wxSocketBase callback management ------------------- // -------------------------------------------------------------- +#ifdef __WXGTK__ +void wxPrereadSocket(wxSocketBase *sock) +{ + char tmp_buf[1024]; + int got = 0; + + do { + got = recv(sock->m_fd, tmp_buf, 1024, 0); + if (got > 0) + sock->CreatePushbackAfter(tmp_buf, got); + } while (got > 0); +} +#endif + #if defined(__WXMOTIF__) || defined(__WXXT__) || defined(__WXGTK__) #if defined(__WXMOTIF__) || defined(__WXXT__) static void wx_socket_read(XtPointer client, int *fid, @@ -708,6 +724,15 @@ static void wx_socket_read(gpointer client, gint fd, { if (!(sock->NeededReq() & wxSocketBase::REQ_READ)) { +#ifdef __WXGTK__ + // We can't exit from the GDK main loop because it doesn't accept + // destroying input event while we are in a event dispatch. + // So we will preread socket and we put the data in the pushback. + wxPrereadSocket(sock); + // Then we set the socket as BLOCKING + int flag = 0; + ioctl(fd, FIONBIO, &flag); +#endif return; } @@ -1119,7 +1144,10 @@ void wxSocketBase::CreatePushbackAfter(const char *buffer, size_t size) { char *curr_pos; - m_unread = (char *) realloc(m_unread, m_unrd_size+size); + if (m_unread != NULL) + m_unread = (char *) realloc(m_unread, m_unrd_size+size); + else + m_unread = (char *) malloc(size); curr_pos = m_unread + m_unrd_size; memcpy(curr_pos, buffer, size); diff --git a/src/common/url.cpp b/src/common/url.cpp index 6df0b8ff58..8c7ddb27d2 100644 --- a/src/common/url.cpp +++ b/src/common/url.cpp @@ -160,10 +160,9 @@ bool wxURL::PrepHost(wxString& url) // Retrieve service number pos2 = temp_url.Find(':', TRUE); if (pos2 != -1 && pos2 < pos) { - m_servname = url(pos2, pos); + m_servname = temp_url(pos2+1, pos); if (!m_servname.IsNumber()) return FALSE; - pos2 = pos; temp_url = temp_url(0, pos2); } -- 2.45.2