admin
2024-10-18 8ea6d363df77de2dca288397da8d4f9c3d3a5c4d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "NetworkApi.h"
#include <chrono>
#include <random>
#include "md5.h"
#include "SocketManager.h"
 
string NetworkApi::_TRADE_SERVER_ADDR ="43.138.167.68";
int NetworkApi::_TRADE_SERVER_PORT = 11008;
string NetworkApi::_COMMON_SERVER_ADDR;
int NetworkApi::_COMMON_SERVER_PORT;
 
string NetworkApi::get_rquest_id()
{
    string requestIdStr;
    std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
    // ½«µ±Ç°Ê±¼äµãת»»Îªtime_tÀàÐÍ
    std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
    requestIdStr.append("_").append(std::to_string(currentTime)).append("_");
    // Éú³ÉËæ»úÊý
    std::random_device rd;
    std::mt19937 rng(rd());
    std::uniform_int_distribution<int> intDist(1, 100000); // Éú³É·¶Î§ÔÚ1-100Ö®¼äµÄÕûÊý
    int randomInt = intDist(rng);
    requestIdStr.append(std::to_string(randomInt));
    return requestIdStr;
}
 
string NetworkApi::get_sign(rapidjson::Document& document)
{
    //¼ÆËãÇ©Ãû
    std::list<string> strList;
    for (rapidjson::Value::ConstMemberIterator itr = document.MemberBegin(); itr != document.MemberEnd(); ++itr) {
        if (itr->value.IsObject() || itr->value.IsArray()) {
            //Èç¹ûÊǶÔÏó
            rapidjson::StringBuffer buffer;
            rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
            itr->value.Accept(writer);
            std::string jsonString = buffer.GetString();
            strList.push_back(string(itr->name.GetString()).append("=").append(jsonString));
        }
        else {
            if (itr->value.IsString()) {
                strList.push_back(string(itr->name.GetString()).append("=").append(itr->value.GetString()));
            }
            else     if (itr->value.IsInt()) {
                strList.push_back(string(itr->name.GetString()).append("=").append(std::to_string(itr->value.GetInt())));
            }
        }
 
    }
 
    strList.sort();
    strList.push_back("%Yeshi2014@#.");
    string src = "";
    int index = 0;
    for (std::list<string>::iterator el = strList.begin(); el != strList.end(); el++) {
        src.append(*el);
        index++;
        if (index != strList.size()) {
            src.append("&");
        }
    }
    //MD5
    //cout << "¼ÓÃÜǰ×Ö·û´®£º" <<src << endl;
    string md5 = MD5(src).toStr();
    return md5;
}
 
 
void NetworkApi::set_server_info(string trade_addr, int trade_port, string common_addr, int common_port)
{
    _TRADE_SERVER_ADDR = trade_addr;
    _TRADE_SERVER_PORT = trade_port;
    _COMMON_SERVER_ADDR = common_addr;
    _COMMON_SERVER_PORT = common_port;
}
 
std::string NetworkApi::load_request_data(std::string data)
{
    rapidjson::Document document;
    document.Parse(data.c_str());
    string requestIdStr = (document["type"].IsInt() ? std::to_string(document["type"].GetInt()) : document["type"].GetString()).append(get_rquest_id());
    rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
 
    // Ìí¼Ó request_id ×Ö¶Î
    rapidjson::Value requestId(rapidjson::kStringType);
    requestId.SetString(requestIdStr.c_str(), allocator);
    document.AddMember("request_id", requestId, allocator);
    // Ìí¼ÓÇ©Ãû£¬sign
    string sign = get_sign(document);
    rapidjson::Value signV(rapidjson::kStringType);
    signV.SetString(sign.c_str(), allocator);
    document.AddMember("sign", signV, allocator);
 
    rapidjson::StringBuffer buffer;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
    document.Accept(writer);
 
    std::string jsonString = buffer.GetString();
    //·¢ËÍÊý¾Ý
    std::stringstream ss;
    ss << std::setw(8) << std::setfill('0') << jsonString.length();
    std::string length_str = string("##").append(ss.str());
    std::string fstr = length_str.append(jsonString);
    return fstr;
}
 
 
//ÍøÂçÇëÇó 
std::string  NetworkApi::base_request(std::string data) {
    std::string fstr = load_request_data(data);
    try {
        return    SocketManager::sendMsg(_COMMON_SERVER_ADDR, _COMMON_SERVER_PORT, fstr.c_str());
    }
    catch (...) {
        throw L"ÍøÂçÇëÇó³ö´í";
    }
}
 
std::string NetworkApi::base_trade_request(std::string data)
{
    rapidjson::Document document;
    document.Parse(data.c_str());
    string requestIdStr = document["type"].IsInt()? to_string(document["type"].GetInt()) : string(document["type"].GetString()).append(get_rquest_id());
    rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
 
    // Ìí¼Ó request_id ×Ö¶Î
    rapidjson::Value requestId(rapidjson::kStringType);
    requestId.SetString(requestIdStr.c_str(), allocator);
    document.AddMember("request_id", requestId, allocator);
    // Ìí¼ÓÇ©Ãû£¬sign
    string sign = get_sign(document);
    rapidjson::Value signV(rapidjson::kStringType);
    signV.SetString(sign.c_str(), allocator);
    document.AddMember("sign", signV, allocator);
 
    rapidjson::StringBuffer buffer;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
    document.Accept(writer);
 
    std::string jsonString = buffer.GetString();
    //·¢ËÍÊý¾Ý
    std::stringstream ss;
    ss << std::setw(8) << std::setfill('0') << jsonString.length();
    std::string length_str = string("##").append(ss.str());
    std::string fstr = length_str.append(jsonString);
 
    try {
        return    SocketManager::sendMsg(_TRADE_SERVER_ADDR, _TRADE_SERVER_PORT, fstr.c_str());
    }
    catch (...) {
        throw wstring(L"ÍøÂçÇëÇó³ö´í");
    }
}