在LUA中收发网络数据

添加捕鱼网络通讯协议的C++<->LUA交互接口;用UltraEdit另存为DOS换行符、utf-8无BOM格式,CCLuaLog打印提示最好用英文。
调试时注意点:
1.将框架最新编译的ClientSkeleton32_d.dll、CoreClientShell_d.dll拷贝到Debug.win32目录下
2.先运行Decoda,然后将Game.lua拖进来,最后设置断点进行调试
3.断线重连,服务器端没有发数据包给客户端。所以目前调试网络通讯时,最好重启捕鱼房间服务器,再启动捕鱼客户端。


Game.lua中处理服务器端发往客户端的数据包的脚本代码如下:

-- 收到完成包进行业务及异常处理
function OnSocketRecv(proxy,maincode,subcode,data,len)
 CCLuaLog("OnSocketRecv.proxy["..tostring(proxy).."].maincode["..maincode.."].subcode["..subcode.."].data["..tostring(data).."].len["..len.."]")
 
 -- 各种异常
 if (maincode==0) then
  if (subcode==0) then
   CCLuaLog("网络连接已断开")
  elseif (subcode==-1) then
   CCLuaLog("网络通讯异常")
  elseif (subcode==-2) then
   CCLuaLog("连接失败")
  elseif (subcode==-3) then
   CCLuaLog("发送失败")
  else
   CCLuaLog("未知的通讯错误")
  end
  return
 end
 
 -- 下面是各种业务处理
 --add by Ivan_han 20130519
 --房间消息
    if (maincode==ROOM_MAIN) then
        handle_room_msg(subcode,data,len)
        return
    end
 --桌子消息
    if (maincode==TABLE_MAIN) then
        handle_table_msg(subcode,data,len)
        return
    end   
end

 

--处理房间消息
function handle_room_msg(subcode,data,len)
 if (subcode==ROOM_SUB_LOGIN_FAIL) then
  --printf("用户登录失败,错误码:%d  \n", pReal->iErrCode);
  data = tolua.cast(data,"LOGIN_ROOM_FAIL")
  CCLuaLog("用户登陆失败")


 elseif (subcode==ROOM_SUB_USER_INFO) then
  --printf("用户信息, 姓名:%s,  昵称:%s, \n", pReal->szName, pReal->szNickname);
  data = tolua.cast(data,"USER_INFO")
  CCLuaLog("用户信息")

 elseif (subcode==ROOM_SUB_USER_LIST) then
  data = tolua.cast(data,"USER_INFO")
  local int64=SIX_LONGLONG:new(data.lUserId)
  local iT=type(data.szName)
    local strName=SIX_Utility:GetInstance():G2U(data.szName)
    CCLuaLog(strName)
    CCLuaLog("接收用户列表")
   
 elseif (subcode==ROOM_SUB_USER_LIST_OVER) then
  CCLuaLog("接收用户列表结束")
  

 elseif (subcode==ROOM_SUB_LEAVE_ROOM) then  
  data = tolua.cast(data,"LEAVE_ROOM_RESULT") 
  CCLuaLog("离房成功")
   
   
 else

 end
end

--处理桌子消息
function handle_table_msg(subcode,data,len)
 if (subcode==TABLE_SUB_SITDOWN) then
  data = tolua.cast(data,"SITDOWN")
  if(0==data.iRes)then
    CCLuaLog(string.format("Sitdown,iTableNo=%d ,iSeatNo=%d ", data.iTableNo, data.iSeatNo));
   else
    CCLuaLog(string.format("Sitdown Fail,iTableNo=%d ,iSeatNo=%d , ErrorCode=%d ", data.iTableNo, data.iSeatNo, data.iRes));
  end

 elseif (subcode==TABLE_SUB_LEAVE_SEAT) then
  local  pReal= tolua.cast(data,"LEAVE_SEAT")
   if(0==pReal.iRes)then
    CCLuaLog(string.format("iTableNo=%d ,iSeatNo=%d,LeaveSeat ", pReal.iTableNo, pReal.iSeatNo));
   else
    CCLuaLog(string.format("LeaveSeat Fail"));
   end

 elseif (subcode==TABLE_SUB_FIRE) then
  local pReal = tolua.cast(data,"SHOT_FIRE_RESULT")
   if(0==pReal.iRes)then
    CCLuaLog(string.format("%s Fire, iTableNo=%d ,iSeatNo=%d, iBulletNum=%d ",pReal.szName, pReal.iTableNo, pReal.iSeatNo, pReal.iBulletNum));
   else
    CCLuaLog(string.format("Fire Fail,ErrorCode=%d ", pReal.iRes));
   end

 elseif (subcode==RSP_USER_SHOOT) then
  local pReal = tolua.cast(data,"Rsp_UserShoot")
  if(0==pReal.iRes)then
   CCLuaLog(string.format("%s Fire, iTableNo=%d ,iSeatNo=%d, GunGrade=%d,BulletID=%d ",pReal.szName, pReal.TableID, pReal.ChairID, pReal.GunGrade,pReal.BulletID));
  else
   CCLuaLog(string.format("Fire Fail,ErrorCode=%d ", pReal.iRes));
  end

 elseif (subcode==RSP_ADD_ONE_FISH) then
  local pReal = tolua.cast(data,"Rsp_AddOneFish")
  CCLuaLog(string.format("鱼轨迹, 桌号%d 鱼种类%d,鱼标识%d,鱼轨迹类型%d ",pReal.TableID, pReal.FishKind,pReal.FishID,pReal.FishPathType)); 

 elseif (subcode==RSP_ADD_ONE_FISH_ARR) then
  local pReal = tolua.cast(data,"Rsp_AddOneFish_Arr")
  CCLuaLog(string.format("鱼轨迹阵, 桌号%d 鱼轨迹阵类型%d,鱼数目%d ",pReal.TableID, pReal.FishGroupType,pReal.nFishCount));  

 elseif (subcode==RSP_ADD_ONE_FISH_LIST) then  
  local pReal = tolua.cast(data,"Rsp_AddOneFish_List") 
  CCLuaLog(string.format("鱼轨迹阵, 桌号%d 鱼轨迹阵类型%d,鱼数目%d ",pReal.TableID, pReal.FishGroupType,pReal.nFishCount));  
   
 else

 end
end

你可能感兴趣的:(在LUA中收发网络数据)