文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

如何使用HTTP实现二进制传输

2023-06-05 03:48

关注

本篇文章给大家分享的是有关如何使用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*100static 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_FUNCBEGIN_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_FUNCBEGIN_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_FUNCBEGIN_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_FUNCBEGIN_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_FUNCBEGIN_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_FUNCBEGIN_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实现二进制传输,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网行业资讯频道。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯