OutputStreamPtr Directory::OpenWrite(const String& path, const String& filename, bool append) { String fullpath = URL::GetAppendedPath(path, filename); std::FILE* file = 0; #if NEX_LINUX == 1 file = std::fopen(fullpath.c_str(), append ? "ab" : "wb"); #elif NEX_WINDOWS == 1 FOPEN_W(file, StringUtils::ToUtf16(fullpath).c_str(), append ? L"ab" : L"wb"); #else #error no standard way to open files? #endif if (!file) { return OutputStreamPtr(); } FileOutputStream* ptr = NEX_NEW( FileOutputStream(file, static_cast<uint32>(Constants::INVALID_SIZE))); return Assign<OutputStream>(ptr); }
// // void *FTPThread::Entry() // // FTP file download thread void *FTPThread::Entry() { wxCommandEvent Event(EVENT_FTP_THREAD, wxID_ANY ); wxInputStream *InputStream; int FileSize = 0; URIHandler URI(m_File); switch (URI.ParseURL(m_URL)) { case URI_BADDOMAIN: { Event.SetId(FTP_BADURL); Event.SetString(wxT("No domain specified")); wxQueueEvent(m_EventHandler, Event.Clone()); return NULL; } break; case URI_BADPATH: { Event.SetId(FTP_BADURL); Event.SetString(wxT("Path to file not specified")); wxQueueEvent(m_EventHandler, Event.Clone()); return NULL; } break; case URI_BADFILE: { Event.SetId(FTP_BADURL); Event.SetString(wxT("This is a directory, not a file")); wxQueueEvent(m_EventHandler, Event.Clone()); return NULL; } break; default: { } break; } m_File = URI.GetFile(); wxUint16 Port = URI.GetPort(); if (!Port) Port = 21; // Blame wx wxString User = URI.GetUser(); wxString Password = URI.GetPassword(); if (User != wxT("")) m_FTP.SetUser(User); if (Password != wxT("")) m_FTP.SetPassword(Password); wxIPV4address IPV4address; IPV4address.Hostname(URI.GetServer()); IPV4address.Service(Port); // Stupid wxFTP.. wxLog::EnableLogging(false); // Try passive mode first m_FTP.SetPassive(true); // Try to connect to the server // Why can't this accept a port parameter? :'( if (m_FTP.Connect(IPV4address)) { // Successful connection Event.SetId(FTP_CONNECTED); Event.SetString(URI.GetServer()); Event.SetInt(Port); wxQueueEvent(m_EventHandler, Event.Clone()); } else { // Try connecting again in Active mode m_FTP.SetPassive(false); if (m_FTP.Connect(IPV4address)) { Event.SetId(FTP_CONNECTED); Event.SetString(URI.GetServer()); Event.SetInt(Port); wxQueueEvent(m_EventHandler, Event.Clone()); } else { // We failed miserably Event.SetId(FTP_DISCONNECTED); Event.SetString(URI.GetServer()); Event.SetInt(Port); wxQueueEvent(m_EventHandler, Event.Clone()); return NULL; } } // Change the directory m_FTP.ChDir(URI.GetDirectory()); // Binary transfer mode m_FTP.SetBinary(); // Try and get the file size first int statuscode; wxString Command; Command.Printf(wxT("SIZE %s"), m_File); char ret = m_FTP.SendCommand(Command); if (ret == '2') { if ( wxSscanf(m_FTP.GetLastResult().c_str(), wxT("%i %i"), &statuscode, &FileSize) != 2) { // Try wx's version if (!FileSize) FileSize = m_FTP.GetFileSize(m_File); } } else { // Try wx's version FileSize = m_FTP.GetFileSize(m_File); } // Try to locate the file if ((InputStream = m_FTP.GetInputStream(m_File))) { // We now got the stream for the file, return some data Event.SetId(FTP_GOTFILEINFO); Event.SetInt(FileSize); wxQueueEvent(m_EventHandler, Event.Clone()); } else { // Location of file is invalid Event.SetId(FTP_DOWNLOADERROR); Event.SetString(URI.GetPath()); wxQueueEvent(m_EventHandler, Event.Clone()); return NULL; } // Create the file wxFileName FileName(m_SaveLocation, m_File); wxMyFileOutputStream FileOutputStream(FileName.GetFullPath(), m_EventHandler, EVENT_FTP_THREAD, this); if (FileOutputStream.IsOk()) { Event.SetId(FTP_DOWNLOADING); Event.SetString(FileName.GetFullPath()); wxQueueEvent(m_EventHandler, Event.Clone()); } else { Event.SetId(FTP_DOWNLOADERROR); Event.SetString(FileName.GetFullPath()); wxQueueEvent(m_EventHandler, Event.Clone()); delete InputStream; return NULL; } // Download the file FileOutputStream.Write(*InputStream); // Download done Event.SetId(FTP_DOWNLOADCOMPLETE); Event.SetString(FileName.GetFullPath()); wxQueueEvent(m_EventHandler, Event.Clone()); delete InputStream; return NULL; }
// // void *HTTPThread::Entry() // // HTTP file download thread void *HTTPThread::Entry() { wxCommandEvent Event(EVENT_HTTP_THREAD, wxID_ANY ); wxInputStream *InputStream; size_t FileSize = 0; URIHandler URI(m_File); switch (URI.ParseURL(m_URL)) { case URI_BADDOMAIN: { Event.SetId(HTTP_BADURL); Event.SetString(wxT("No domain specified")); wxQueueEvent(m_EventHandler, Event.Clone()); return NULL; } break; case URI_BADPATH: { Event.SetId(HTTP_BADURL); Event.SetString(wxT("Path to file not specified")); wxQueueEvent(m_EventHandler, Event.Clone()); return NULL; } break; case URI_BADFILE: { Event.SetId(HTTP_BADURL); Event.SetString(wxT("This is a directory, not a file")); wxQueueEvent(m_EventHandler, Event.Clone()); return NULL; } break; default: { } break; } m_File = URI.GetFile(); m_HTTP.SetUser(URI.GetUser()); m_HTTP.SetPassword(URI.GetPassword()); wxUint16 Port = URI.GetPort(); if (!Port) Port = 80; // Try to connect to the server if (m_HTTP.Connect(URI.GetServer(), Port)) { // Successful connection Event.SetId(HTTP_CONNECTED); Event.SetString(URI.GetServer()); Event.SetInt(Port); wxQueueEvent(m_EventHandler, Event.Clone()); } else { // We failed miserably Event.SetId(HTTP_DISCONNECTED); Event.SetString(URI.GetServer()); Event.SetInt(Port); wxQueueEvent(m_EventHandler, Event.Clone()); return NULL; } // Try to locate the file if ((InputStream = m_HTTP.GetInputStream(URI.GetPath()))) { FileSize = InputStream->GetSize(); // We now got the stream for the file, return some data Event.SetId(HTTP_GOTFILEINFO); Event.SetInt((size_t)FileSize); wxQueueEvent(m_EventHandler, Event.Clone()); } else { // Location of file is invalid Event.SetId(HTTP_DOWNLOADERROR); Event.SetString(URI.GetPath()); wxQueueEvent(m_EventHandler, Event.Clone()); return NULL; } // Create the file wxFileName FileName(m_SaveLocation, m_File); wxMyFileOutputStream FileOutputStream(FileName.GetFullPath(), m_EventHandler, EVENT_HTTP_THREAD, this); if (FileOutputStream.IsOk()) { Event.SetId(HTTP_DOWNLOADING); Event.SetString(FileName.GetFullPath()); wxQueueEvent(m_EventHandler, Event.Clone()); } else { Event.SetId(HTTP_DOWNLOADERROR); Event.SetString(FileName.GetFullPath()); wxQueueEvent(m_EventHandler, Event.Clone()); delete InputStream; return NULL; } FileOutputStream.Write(*InputStream); // Download done Event.SetId(HTTP_DOWNLOADCOMPLETE); Event.SetString(FileName.GetFullPath()); wxQueueEvent(m_EventHandler, Event.Clone()); delete InputStream; return NULL; }
void* FileReceiveThread::Entry() { #if defined(UxNO_SPEED_RECV) //Basic flow: SocketIn->ZipIn->FileOut // If we don't receive anything for 10 seconds, assume a timeout m_socket->SetTimeout(10); // Wait for some data to come in, or for an error // and block on the socket calls m_socket->SetFlags(wxSOCKET_WAITALL | wxSOCKET_BLOCK); // Output to the specified file wxFileOutputStream FileOutputStream(m_filename); // Stream data in from the socket wxSocketInputStream SocketInputStream(*m_socket); // The zlib decompression will decompress data from the // socket stream wxZlibInputStream ZlibInputStream(SocketInputStream); // Write to the file stream the results of reading from the // zlib input stream FileOutputStream.Write(ZlibInputStream); //wxMessageBox(_("Transmision completed")); return NULL; #else //Basic flow: SocketIn->ZipIn[%->]FileOut // If we don't receive anything for 10 seconds, assume a timeout m_socket->SetTimeout(10); // Wait for some data to come in, or for an error // and block on the socket calls m_socket->SetFlags(wxSOCKET_WAITALL | wxSOCKET_BLOCK); // Output to the specified file wxFileOutputStream FileOutputStream(m_filename); // Stream data in from the socket wxSocketInputStream SocketInputStream(*m_socket); // The zlib decompression will decompress data from the // socket stream wxZlibInputStream ZlibInputStream(SocketInputStream); // Write to the file stream the results of reading from the // zlib input stream const size_t buf_size = 1024 * 1024; wxMemoryBuffer buf(buf_size); wxULongLong count = 0; size_t lastRead = 0; int percent; while(!ZlibInputStream.Eof()) { ZlibInputStream.Read(buf.GetWriteBuf(buf_size), buf_size); lastRead = ZlibInputStream.LastRead(); count += lastRead; FileOutputStream.Write(buf.GetData(), lastRead); percent = count.ToDouble() * 100 / m_filesize.ToDouble(); wxMutexGuiEnter(); if(percent < 100) m_progDlg->Update(percent); wxMutexGuiLeave(); } //FileOutputStream.Write(ZlibInputStream); wxMutexGuiEnter(); m_progDlg->Update(99); wxMutexGuiLeave(); m_progDlg->Destroy(); m_progDlg = (wxProgressDialog *)NULL; return NULL; #endif }