diff horori/searcher/include/net.h @ 0:aaaa401818a1 draft

first commit.
author pyon <pyon@macmini>
date Mon, 24 May 2021 21:32:58 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/horori/searcher/include/net.h	Mon May 24 21:32:58 2021 +0900
@@ -0,0 +1,125 @@
+// Filename   : net.h
+// Last Change: 2020-05-01 ‹à 08:47:39.
+//
+#pragma once
+
+#include <wx/sstream.h>
+#include <wx/wfstream.h>
+#include <wx/zstream.h>
+#include <wx/tarstrm.h>
+#include <wx/protocol/http.h>
+
+wxString HttpGetText(wxString addr, wxString port, wxString url)
+{
+	wxHTTP get;
+	get.SetFlags(wxSOCKET_WAITALL|wxSOCKET_BLOCK);
+	while (!get.Connect(addr, wxAtoi(port)))
+		wxSleep(1);
+
+	wxString res;
+	wxInputStream *httpStream = get.GetInputStream(url);
+	if (get.GetError() == wxPROTO_NOERR) {
+		wxStringOutputStream out_stream(&res);
+		httpStream->Read(out_stream);
+	}
+
+	wxDELETE(httpStream);
+	get.Close();
+
+	return res;
+};
+
+bool HttpGetFile(wxString addr, wxString port, wxString url, wxString file)
+{
+	bool ret = false;
+	wxHTTP get;
+	get.SetFlags(wxSOCKET_WAITALL|wxSOCKET_BLOCK);
+	while (!get.Connect(addr, wxAtoi(port)))
+		wxSleep(1);
+
+	wxInputStream *httpStream = get.GetInputStream(url);
+	if (get.GetError() == wxPROTO_NOERR) {
+		wxFileOutputStream out_stream(file);
+		httpStream->Read(out_stream);
+		ret = true;
+	}
+
+	wxDELETE(httpStream);
+	get.Close();
+	return ret;
+};
+
+bool HttpGetTgzFile(wxString addr, wxString port, wxString url, wxString dir)
+{
+	bool ret = false;
+	wxHTTP get;
+	get.SetTimeout(30);
+	get.SetFlags(wxSOCKET_WAITALL|wxSOCKET_BLOCK);
+	while (!get.Connect(addr, wxAtoi(port)))
+		wxSleep(1);
+
+	wxInputStream *httpStream = get.GetInputStream(url);
+	if (get.GetError() == wxPROTO_NOERR) {
+		//int size = httpStream->GetSize();
+		wxZlibInputStream zlib_istream(httpStream);
+
+		wxTarEntry* entry;
+		wxTarInputStream tar_istream(zlib_istream);
+		int i = 1;
+		while ((entry = tar_istream.GetNextEntry()) != NULL) {
+			//wxString name = entry->GetName();
+			wxFileOutputStream file_ostream(wxString::Format(wxT("%s/%d"), dir, i++));
+			file_ostream.Write(tar_istream);
+			file_ostream.Close();
+		}
+		ret = true;
+		//wxDELETE(httpStream);
+	}
+
+	get.Close();
+	return ret;
+};
+
+bool HttpPostFile(wxString addr, wxString port, wxString url, wxString file)
+{
+	bool ret = false;
+
+	wxMemoryBuffer buf;
+	wxByte b[8192];
+	wxFFile f;
+	if (!f.Open(file, wxT("rb"))) {
+        wxMessageBox(wxT("Cannot open file."));
+        return ret;
+    }
+	for (;;) {
+		size_t len = f.Read(b, sizeof(b));
+		if ((size_t)wxInvalidOffset == len) {
+			return ret;
+		}
+		if (len == 0) break; // success
+		buf.AppendData(b, len);
+	}
+	f.Close();
+
+	wxHTTP post;
+	post.SetTimeout(30);
+	post.SetFlags(wxSOCKET_WAITALL|wxSOCKET_BLOCK);
+	post.SetPostBuffer(wxT("application/gzip"), buf);
+
+	while (!post.Connect(addr, wxAtoi(port)))
+		wxSleep(1);
+
+	wxInputStream *httpStream = post.GetInputStream(url);
+	if (httpStream != NULL) {
+		wxString res;
+		wxStringOutputStream out_stream(&res);
+		httpStream->Read(out_stream);
+		wxDELETE(httpStream);
+		//wxMessageBox(res);
+		ret = true;
+	}
+
+	post.Close();
+	return ret;
+};
+