diff --git a/source/clients/nfsclient.cpp b/source/clients/nfsclient.cpp index 8bf2f12..a9443f4 100644 --- a/source/clients/nfsclient.cpp +++ b/source/clients/nfsclient.cpp @@ -123,7 +123,7 @@ int NfsClient::Get(const std::string &outputfile, const std::string &ppath, uint nfs_lseek(nfs, nfsfh, offset, SEEK_SET, NULL); } - while ((count = nfs_read(nfs, nfsfh, buff, BUF_SIZE)) > 0) + while ((count = nfs_read(nfs, nfsfh, BUF_SIZE, buff)) != 0) { if (count < 0) { @@ -177,7 +177,7 @@ int NfsClient::GetRange(void *fp, DataSink &sink, uint64_t size, uint64_t offset do { size_t bytes_to_read = std::min(BUF_SIZE, bytes_remaining); - count = nfs_read(nfs, nfsfh, buff, bytes_to_read); + count = nfs_read(nfs, nfsfh, bytes_to_read, buff); if (count > 0) { bytes_remaining -= count; diff --git a/source/clients/sftpclient.cpp b/source/clients/sftpclient.cpp index 57e6781..9a9f149 100644 --- a/source/clients/sftpclient.cpp +++ b/source/clients/sftpclient.cpp @@ -210,26 +210,25 @@ int SFTPClient::Get(const std::string &outputfile, const std::string &path, uint } char *buff = (char *)malloc(FTP_CLIENT_BUFSIZ); - int rc, count = 0; + int count = 0; *g_bytes_transfered = offset; if (offset > 0) { libssh2_sftp_seek64(sftp_handle, offset); } - do - { - rc = libssh2_sftp_read(sftp_handle, buff, FTP_CLIENT_BUFSIZ); - if (rc > 0) - { - *g_bytes_transfered += rc; - FS::Write(out, buff, rc); - } - else - { - break; - } - } while (1); + while ((count = libssh2_sftp_read(sftp_handle, buff, FTP_CLIENT_BUFSIZ))!= 0) + { + if (count < 0) + { + free((char *)buff); + FS::Close(out); + libssh2_sftp_close(sftp_handle); + return 0; + } + FS::Write(out, buff, count); + *g_bytes_transfered += count; + } free((char *)buff); FS::Close(out); diff --git a/source/clients/smbclient.cpp b/source/clients/smbclient.cpp index 8db3f72..e845312 100644 --- a/source/clients/smbclient.cpp +++ b/source/clients/smbclient.cpp @@ -122,7 +122,7 @@ int SmbClient::Get(const std::string &outputfile, const std::string &ppath, uint smb2_lseek(smb2, in, offset, SEEK_SET, NULL); } - while ((count = smb2_read(smb2, in, buff, max_read_size)) > 0) + while ((count = smb2_read(smb2, in, buff, max_read_size)) != 0) { if (count < 0) { diff --git a/source/main.cpp b/source/main.cpp index af4ba4a..109a284 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -23,7 +23,6 @@ extern "C" } static int g_libnet_mem_id = -1; -static volatile sig_atomic_t g_running = 1; static int NetInit(void) { @@ -33,7 +32,7 @@ static int NetInit(void) return -1; } - g_libnet_mem_id = sceNetPoolCreate("nanodns", 5 * 1024 * 1024, 0); + g_libnet_mem_id = sceNetPoolCreate("ezremote_server", 5 * 1024 * 1024, 0); if (g_libnet_mem_id < 0) { errno = EIO; @@ -64,17 +63,15 @@ static void NetTerm(void) } } -static void OnSignal(int signo) +static void OnSignal() { - (void)signo; - g_running = 0; + dbglogger_log("Terminating OnSignal"); + HttpServer::Stop(); + HttpServer::StopDownloadThread(); } int main(int argc, char *argv[]) { - signal(SIGINT, OnSignal); - signal(SIGTERM, OnSignal); - if (NetInit() != 0) { NetTerm(); @@ -94,9 +91,12 @@ int main(int argc, char *argv[]) return 0; } + atexit(OnSignal); + HttpServer::StartDownloadThread(); HttpServer::Start(); Util::Notify("ezRemote Server stopped."); + HttpServer::StopDownloadThread(); NetTerm(); return 0; diff --git a/source/server/http_server.cpp b/source/server/http_server.cpp index b7aa9e7..aaeb5ee 100644 --- a/source/server/http_server.cpp +++ b/source/server/http_server.cpp @@ -25,6 +25,7 @@ Server *svr; int http_server_port = 6701; static pthread_t bg_download_thread; static uint64_t g_dl_offset; +static bool stop_download = false; namespace HttpServer { @@ -193,7 +194,7 @@ namespace HttpServer uint64_t tmp_file_size; int ret; - while (true) + while (!stop_download) { for (int i=0; i < bg_download_list.size(); i++) { @@ -218,7 +219,6 @@ namespace HttpServer ret = tmp_client->Get(temp_file, bg_download_list[i].src_path); - FS::Rename(temp_file, bg_download_list[i].dest_path); if (ret == 0) { bg_download_list[i].state = STATE_FAILED; @@ -226,6 +226,7 @@ namespace HttpServer } else { + FS::Rename(temp_file, bg_download_list[i].dest_path); Util::Notify("Completed download %s", bg_download_list[i].dest_path.c_str()); bg_download_list[i].state = STATE_SUCCESS; } @@ -233,7 +234,7 @@ namespace HttpServer DeleteRemoteClient(tmp_client); } - else if (bg_download_list[i].state == STATE_DOWNLOADING) + else if (bg_download_list[i].state == STATE_DOWNLOADING || bg_download_list[i].state == STATE_FAILED) { // Resume interrupted download RemoteClient *tmp_client = GetRemoteClient(&(bg_download_list[i].host_info)); @@ -263,7 +264,6 @@ namespace HttpServer ret = tmp_client->Get(temp_file, bg_download_list[i].src_path); } - FS::Rename(temp_file, bg_download_list[i].dest_path); if (ret == 0) { bg_download_list[i].state = STATE_FAILED; @@ -271,6 +271,7 @@ namespace HttpServer } else { + FS::Rename(temp_file, bg_download_list[i].dest_path); Util::Notify("Completed download %s", bg_download_list[i].dest_path.c_str()); bg_download_list[i].state = STATE_SUCCESS; } @@ -561,6 +562,12 @@ namespace HttpServer pthread_create(&bg_download_thread, NULL, DownloadFilesThread, NULL); } + void StopDownloadThread() + { + stop_download = true; + pthread_cancel(bg_download_thread); + } + bool IsStarted() { httplib::Client client = httplib::Client("http://127.0.0.1:6701"); diff --git a/source/server/http_server.h b/source/server/http_server.h index fb2aff0..14c9a4a 100644 --- a/source/server/http_server.h +++ b/source/server/http_server.h @@ -14,6 +14,7 @@ namespace HttpServer void Start(); void Stop(); void StartDownloadThread(); + void StopDownloadThread(); bool IsStarted(); }