亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用HTTP實現二進制傳輸

發布時間:2021-11-17 14:36:57 來源:億速云 閱讀:154 作者:柒染 欄目:軟件技術

本篇文章給大家分享的是有關如何使用HTTP實現二進制傳輸,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

HTTP實現二進制傳輸。曾經為這事還糾結過。剛工作時游戲都是端游。基本學習和接觸過得都是TCP發送二進制方式。當時剛開始轉型研發頁游。原來刪除的都是C++開發。突然要研究flash搞網頁游戲。ActionScript(簡稱AS)這語言不熟悉。弄網絡封裝時肯定潛意識設計思路是HTTP實現二進制傳輸。然后結合網絡消息采用工具自動化生成方式。自己不善于HTTP。故意招個比較擅長的人協助搞下底層。我把上面想法告訴他。他斬釘截鐵告訴我HTTP肯定無法傳輸二進制。這讓我不爽研究好幾天終于搞出來了。有時成長需要時間需要去沉淀。經歷過就對如何解決記憶更加深刻。

同理。HTTP傳輸依然靠Gk8HttpClient類。具體實現參閱。 網絡通信3:HTTP實現文本傳輸。炮彈抽象轉實體就是Gk8ByteMaker。 網絡通信1:字節流的封裝。實現Gk8HttpService類提供發送和接收。設置HttpUrl,設置消息Header,是否壓縮等API。方便邏輯應用直接使用。

C++實現HttpService類:Gk8HttpService.h

#ifndef __GK8HTTPSERVICE_H__
#define __GK8HTTPSERVICE_H__
#pragma once
#include "curl/curl.h"
#include "Gk8HttpClient.h"
#include "Gk8HttpRequest.h"
#include "Gk8HttpResponse.h"
class Gk8HttpService:public Gk8BaseObj
{
    DECLARE_TOSPP_MAP;
private:
    Gk8Str m_sHttpUrl;                //[HTTP請求地址]
    Gk8ByteMaker m_iRequestData;    //[HTTP請求數據]
    GK8_BOOL m_bUseZip;                //[HTTP消息使用壓縮]
    Gk8Str m_sUserId;                //[用戶ID]
    Gk8Str m_sUniqueKey;            //[緩存唯一KEY]
    GK8_VOID HttpRequestStarted();
    GK8_VOID HttpRequestCompleted(GK8_LPVOID lpHttpResponse);
public:
    Gk8HttpService();
    ~Gk8HttpService();
    static Gk8HttpService* Create(Gk8BaseObj* pSuperObj=NULL);
    GK8_VOID Destroy();
    GK8_VOID TOSPPFUNC SetHttpUrl(GK8_LPCSTR lpHttpUrl);
    GK8_VOID TOSPPFUNC SetHeader(GK8_LPCSTR lpUserId,GK8_LPCSTR lpUniqueKey);
    GK8_VOID TOSPPFUNC SetUseZip(GK8_BOOL bUseZip);
    GK8_VOID TOSPPFUNC SendMessage(Gk8ByteMaker* pRequestData);
    GK8_VOID TOSPPFUNC PutMessage(Gk8ByteMaker* pRequestData);
    GK8_VOID TOSPPFUNC SendCachedMessage();
};
#endif

C++實現HttpService類:Gk8HttpService.cpp

#include "Gk8HttpService.h"
#include "Gk8OperSys.h"
#include "Gk8Helper.h"
#define segment_size 1024*100
static Gk8Str sg_ReceiveMessageFun("ReceiveMessage");//[接收網絡信息]
/////////////////////////////////////////////CLASS-TOLUA////////////////////////////////////////////////////
TOLUA_CLASS_COLLECT_FUNC(Gk8HttpService)
BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,Create)
    if(!CheckToLuaFunParam(L,"Create",Gk8Var()<<ISUSERTABLE("Gk8HttpService"))) return 0;
    Gk8BaseObj* pSuperObj=NULL;
    if(IFUSERTYPE(2,Gk8BaseObj)) pSuperObj=TOLUAGETOBJ(Gk8BaseObj,2);
    Gk8HttpService* pHttpService=Gk8HttpService::Create(pSuperObj);
    toluafix_pushusertype_ccobject(L,TOLUAOBJID(pHttpService),TOLUALPLUAID(pHttpService),(GK8_LPVOID)pHttpService,"Gk8HttpService");
END_TOLUA_CLASS_FUNC
BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,SetHttpUrl)
    if(!CheckToLuaFunParam(L,"SetHttpUrl",Gk8Var()<<ISUSERTYPE("Gk8HttpService")<<ISSTRING<<ISNOOBJ)) return 0;
    TOLUAGETOBJ(Gk8HttpService,1)->SetHttpUrl(TOLUAGETSTRING(2));
END_TOLUA_CLASS_FUNC
BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,SetHeader)
    if(!CheckToLuaFunParam(L,"SetHeader",Gk8Var()<<ISUSERTYPE("Gk8HttpService")<<ISSTRING<<ISSTRING<<ISNOOBJ)) return 0;
    TOLUAGETOBJ(Gk8HttpService,1)->SetHeader(TOLUAGETSTRING(2),TOLUAGETSTRING(3));
END_TOLUA_CLASS_FUNC
BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,SetUseZip)
    if(!CheckToLuaFunParam(L,"SetUseZip",Gk8Var()<<ISUSERTYPE("Gk8HttpService")<<ISBOOLEAN<<ISNOOBJ)) return 0;
    TOLUAGETOBJ(Gk8HttpService,1)->SetUseZip(TOLUAGETBOOLEAN(2));
END_TOLUA_CLASS_FUNC
BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,SendMessage)
    if(!CheckToLuaFunParam(L,"SendMessage",Gk8Var()<<ISUSERTYPE("Gk8HttpService")<<ISUSERTYPE("Gk8ByteMaker")<<ISNOOBJ)) return 0;
    TOLUAGETOBJ(Gk8HttpService,1)->SendMessage(TOLUAGETOBJ(Gk8ByteMaker,2));
END_TOLUA_CLASS_FUNC
BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,PutMessage)
    if(!CheckToLuaFunParam(L,"PutMessage",Gk8Var()<<ISUSERTYPE("Gk8HttpService")<<ISUSERTYPE("Gk8ByteMaker")<<ISNOOBJ)) return 0;
    TOLUAGETOBJ(Gk8HttpService,1)->PutMessage(TOLUAGETOBJ(Gk8ByteMaker,2));
END_TOLUA_CLASS_FUNC
BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,SendCachedMessage)
    if(!CheckToLuaFunParam(L,"SendCachedMessage",Gk8Var()<<ISUSERTYPE("Gk8HttpService")<<ISNOOBJ)) return 0;
    TOLUAGETOBJ(Gk8HttpService,1)->SendCachedMessage();
END_TOLUA_CLASS_FUNC
//[啟動注冊類的全部TOLUA函數]
BEGIN_TOLUA_FUN_MAP(Gk8HttpService)
    TOLUA_CLASS(Gk8HttpService,Gk8BaseObj,"[網絡類]","[HTTP服務類]")
    TOLUA_CLASS_FUNC(Gk8HttpService,Create,"[Gk8HttpService* Create(Gk8BaseObj* pSuperObj=NULL)")
    TOLUA_CLASS_FUNC(Gk8HttpService,SetHttpUrl,"[GK8_VOID SetHttpUrl(GK8_LPCSTR lpHttpUrl)]")
    TOLUA_CLASS_FUNC(Gk8HttpService,SetHeader,"[GK8_VOID SetHeader(GK8_LPCSTR lpUserId,GK8_LPCSTR lpUniqueKey)]")
    TOLUA_CLASS_FUNC(Gk8HttpService,SetUseZip,"[GK8_VOID SetUseZip(GK8_BOOL bUseZip)]")
    TOLUA_CLASS_FUNC(Gk8HttpService,SendMessage,"[GK8_VOID SendMessage(Gk8ByteMaker* pRequestData)]")
    TOLUA_CLASS_FUNC(Gk8HttpService,PutMessage,"[GK8_VOID PutMessage(Gk8ByteMaker* pRequestData)]")
    TOLUA_CLASS_FUNC(Gk8HttpService,SendCachedMessage,"[GK8_VOID SendCachedMessage()]")
END_TOLUA_FUN_MAP
/////////////////////////////////////////////CLASS-TOSPP////////////////////////////////////////////////////
BEGIN_TOSPP_MAP(Gk8HttpService,Gk8BaseObj)
    TOSPP_FUNC(Gk8HttpService,SetHttpUrl,' ',"s","SetHttpUrl(lpHttpUrl)")
    TOSPP_FUNC(Gk8HttpService,SetHeader,' ',"ss","SetHeader(lpUserId,lpUniqueKey)")
    TOSPP_FUNC(Gk8HttpService,SetUseZip,' ',"b","SetUseZip(bUseZip)")
    TOSPP_FUNC(Gk8HttpService,SendMessage,' ',"p","SendMessage(iRequestDataPtr)")
    TOSPP_FUNC(Gk8HttpService,PutMessage,' ',"p","PutMessage(iRequestDataPtr)")
    TOSPP_FUNC(Gk8HttpService,SendCachedMessage,' '," ","SendCachedMessage()")
END_TOSPP_MAP()
Gk8HttpService::Gk8HttpService()
{
    Destroy();
}
Gk8HttpService::~Gk8HttpService()
{
    Destroy();
}
Gk8HttpService* Gk8HttpService::Create(Gk8BaseObj* pSuperObj)
{
    Gk8HttpService* pHttpService=new Gk8HttpService();
    if(pSuperObj) pHttpService->SetSuper(pSuperObj);
    return pHttpService;
}
GK8_VOID Gk8HttpService::Destroy()
{
    m_sHttpUrl="";
    m_iRequestData.Destroy();
    m_bUseZip=false;
    m_sUserId="0";
    m_sUniqueKey="0";
}
//[設置HTTP地址]
GK8_VOID Gk8HttpService::SetHttpUrl(GK8_LPCSTR lpHttpUrl)
{
    m_sHttpUrl=lpHttpUrl;
}
//[設置頭信息]
GK8_VOID Gk8HttpService::SetHeader(GK8_LPCSTR lpUserId,GK8_LPCSTR lpUniqueKey)
{
    m_sUserId=lpUserId;
    m_sUniqueKey=lpUniqueKey;
}
//[設置是否使用壓縮]
GK8_VOID Gk8HttpService::SetUseZip(GK8_BOOL bUseZip)
{
    m_bUseZip=bUseZip;
}
//[發送消息]
GK8_VOID Gk8HttpService::SendMessage(Gk8ByteMaker* pRequestData)
{
    pRequestData->ShiftTo(m_iRequestData);
    HttpRequestStarted();
}
//[把二進制流重組成新的對象]
GK8_VOID Gk8HttpService::PutMessage(Gk8ByteMaker* pRequestData)
{
    pRequestData->ShiftTo(m_iRequestData);
}
GK8_VOID Gk8HttpService::SendCachedMessage()
{
    HttpRequestStarted();
}
//[開始HTTP請求]
GK8_VOID Gk8HttpService::HttpRequestStarted()
{
    //[全部消息前加上用戶ID和緩存唯一KEY]
    m_iRequestData.WriteStrAt(0,m_sUniqueKey);
    m_iRequestData.WriteStrAt(0,m_sUserId);
    Gk8HttpRequest* pHttpRequest=new Gk8HttpRequest();
    pHttpRequest->SetRequestType(Gk8HttpRequest::kHttpPost);
    pHttpRequest->SetHttpUrl(m_sHttpUrl);
    pHttpRequest->SetResponseCallBack(this,(HTTPREQUEST_CALLBACK_FUN)&Gk8HttpService::HttpRequestCompleted);
    pHttpRequest->SetRequestData(&m_iRequestData);
    Gk8HttpClient::GetInstance()->Send(pHttpRequest);
}
//[HTTP請求完成二進制通信回調]
GK8_VOID Gk8HttpService::HttpRequestCompleted(GK8_LPVOID lpHttpResponse)
{
    Gk8HttpResponse* pHttpResponse=(Gk8HttpResponse*)lpHttpResponse;
    if(!pHttpResponse) return;
    if(!pHttpResponse->IsSucceed())
    {
        _GK8ERR<<"Response Failed Error Is "<<pHttpResponse->GetErrorBuffer()<<CR;
        return;
    }
    Gk8ByteMaker* pResponseData=pHttpResponse->GetResponseData();
    //[把服務器數據派遣到腳本中]
    Gk8Var iByteMakerVar(pResponseData,pResponseData->GetObjId());
#ifdef _GK8_SPP_
    OnCall((GK8_LPCSTR)sg_ReceiveMessageFun,iByteMakerVar);
#else
    RunEventWithArgs(sg_ReceiveMessageFun,iByteMakerVar);
#endif
}

以上就是如何使用HTTP實現二進制傳輸,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

黑水县| 鹤峰县| 江油市| 新巴尔虎左旗| 曲阜市| 洛隆县| 宁陵县| 岳阳县| 运城市| 吉木萨尔县| 铁岭市| 尖扎县| 阜宁县| 米林县| 辽宁省| 汶上县| 中江县| 庆城县| 黔西| 扎兰屯市| 诸城市| 襄樊市| 沾化县| 三门峡市| 浏阳市| 枣庄市| 江孜县| 绥阳县| 江油市| 长宁县| 山东省| 正镶白旗| 长白| 武胜县| 绩溪县| 兴隆县| 木里| 惠州市| 南京市| 玉门市| 宁阳县|