view src/kaigo/horori/searcher/include/net.h @ 45:20b42e2deae1

add alloc, (new)mover, merger & (new)searcher.
author pyon@macmini
date Thu, 16 Apr 2020 20:59:35 +0900
parents
children 169936fed61b
line wrap: on
line source

// Filename   : net.h
// Last Change: 2020-03-30 ŒŽ 15:05:58.
//
#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;
};