如何使用HTTP实现二进制传输-创新互联
本篇文章给大家分享的是有关如何使用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()<SetHttpUrl(TOLUAGETSTRING(2)); END_TOLUA_CLASS_FUNC BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,SetHeader) if(!CheckToLuaFunParam(L,"SetHeader",Gk8Var()< SetHeader(TOLUAGETSTRING(2),TOLUAGETSTRING(3)); END_TOLUA_CLASS_FUNC BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,SetUseZip) if(!CheckToLuaFunParam(L,"SetUseZip",Gk8Var()< SetUseZip(TOLUAGETBOOLEAN(2)); END_TOLUA_CLASS_FUNC BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,SendMessage) if(!CheckToLuaFunParam(L,"SendMessage",Gk8Var()< SendMessage(TOLUAGETOBJ(Gk8ByteMaker,2)); END_TOLUA_CLASS_FUNC BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,PutMessage) if(!CheckToLuaFunParam(L,"PutMessage",Gk8Var()< PutMessage(TOLUAGETOBJ(Gk8ByteMaker,2)); END_TOLUA_CLASS_FUNC BEGIN_TOLUA_CLASS_FUNC(Gk8HttpService,SendCachedMessage) if(!CheckToLuaFunParam(L,"SendCachedMessage",Gk8Var()< 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 "< GetErrorBuffer()< GetResponseData(); //[把服务器数据派遣到脚本中] Gk8Var iByteMakerVar(pResponseData,pResponseData->GetObjId()); #ifdef _GK8_SPP_ OnCall((GK8_LPCSTR)sg_ReceiveMessageFun,iByteMakerVar); #else RunEventWithArgs(sg_ReceiveMessageFun,iByteMakerVar); #endif }
以上就是如何使用HTTP实现二进制传输,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联-成都网站建设公司行业资讯频道。
分享名称:如何使用HTTP实现二进制传输-创新互联
本文URL:http://myzitong.com/article/csissp.html