60 lines
1.1 KiB
C++
60 lines
1.1 KiB
C++
/**
|
|
* http
|
|
* */
|
|
|
|
#ifndef MYHTTP_H_
|
|
#define MYHTTP_H_
|
|
|
|
#include <sstream>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <netinet/tcp.h>
|
|
#include <netdb.h>
|
|
#include <cstring>
|
|
#include <unistd.h>
|
|
#include <mutex>
|
|
|
|
namespace ai_matrix
|
|
{
|
|
class MyHttp final
|
|
{
|
|
public:
|
|
static MyHttp *getins();
|
|
|
|
std::string socketHttp(std::string host, std::string request);
|
|
|
|
std::string postData(std::string host, std::string path, std::string post_content);
|
|
|
|
std::string getData(std::string host, std::string path, std::string get_content);
|
|
|
|
private:
|
|
|
|
MyHttp() = default;
|
|
MyHttp(const MyHttp &) = delete;
|
|
MyHttp(MyHttp &&) = delete;
|
|
MyHttp &operator=(const MyHttp &) = delete;
|
|
MyHttp &operator=(MyHttp &&) = delete;
|
|
~MyHttp() = default;
|
|
|
|
//定义一个嵌套类,负责释放内存,操作系统自动完成,不用担心内存泄露
|
|
class GarbageCollector
|
|
{
|
|
public:
|
|
~GarbageCollector()
|
|
{
|
|
if (MyHttp::ins)
|
|
{
|
|
delete MyHttp::ins;
|
|
MyHttp::ins = nullptr;
|
|
}
|
|
}
|
|
};
|
|
|
|
static GarbageCollector gc;
|
|
static MyHttp *ins;
|
|
static std::mutex mx; //锁,保证线程安全
|
|
};
|
|
|
|
}
|
|
|
|
#endif |