196 lines
4.3 KiB
Lua
196 lines
4.3 KiB
Lua
--网络核心功能处理类
|
||
--author:--
|
||
|
||
-- a net client
|
||
NetClient= {
|
||
}
|
||
|
||
ConnectionProtocol = {
|
||
Tcp = 0,
|
||
--http 短连接
|
||
Web = 1,
|
||
}
|
||
|
||
SocketCode = {
|
||
--连接成功
|
||
Connect = 0,
|
||
--断开
|
||
Disconnect = 1,
|
||
--未知异常
|
||
Exception = 2,
|
||
--连接服务异常
|
||
ExceptionOnConnect = 3,
|
||
--发送数据流错误
|
||
SendError = 4,
|
||
--接收服务器数据流异常
|
||
ExceptionOnReceive = 5,
|
||
--服务器连接超时
|
||
TimeoutDisconnect = 6,
|
||
--服务器断开连接
|
||
DisconnectByServer = 7,
|
||
--客户端网络异常
|
||
NetworkException = 8,
|
||
--连接安全异常,一般为防火墙阻止
|
||
SecurityExceptionOnConnect = 9
|
||
}
|
||
|
||
---
|
||
-- @type NetClient
|
||
local R = {
|
||
--网络地址
|
||
hsot = "",
|
||
--网络端口
|
||
port = 0,
|
||
--session
|
||
session = "",
|
||
--网络类型
|
||
protocol = ConnectionProtocol.Tcp,
|
||
--c#端处理类
|
||
c__netClient = nil,
|
||
--拦截
|
||
holdCallback = nil
|
||
}
|
||
|
||
local LuaNetClient = taurus.unity.LuaNetClient
|
||
|
||
--- Create a new NetClient
|
||
-- @function [parent=#NetClient] new
|
||
-- @param #string host
|
||
-- @param #string game
|
||
-- @param #number protocol
|
||
-- @return #NetClient the created NetClient
|
||
function NetClient.new(host, game,protocol)
|
||
local self = {}
|
||
self.host = host or ""
|
||
self.game = game or ""
|
||
self.protocol = protocol or ConnectionProtocol.Tcp
|
||
-- self.responseMap = {}
|
||
self.onevent = event("onevent",false)
|
||
self.onconnect = event("onconnect",false)
|
||
--print("222222222222222222222222222222222222222222 ",host," ",host," ",game," ",self.protocol)
|
||
|
||
self.c__netClient = LuaNetClient(host,game,self,self.protocol)
|
||
self.c__netClient:SetCallBackListener(R.c__ondata)
|
||
self.c__netClient:SetNetEventListener(R.c__onevent)
|
||
self.c__netClient:SetNetConnectListener(R.c__onconnect)
|
||
setmetatable(self, {__index = R})
|
||
return self
|
||
end
|
||
|
||
function R.connect(self)
|
||
if self.c__netClient == nil then
|
||
return
|
||
end
|
||
self.c__netClient:Connect()
|
||
end
|
||
local TYPE_STRING = "string"
|
||
local TYPE_FUNC = "function"
|
||
local TYPE_TABLE = "table"
|
||
local NULL_JSON = "{}"
|
||
|
||
--- send
|
||
function R.send(self,cmd, data, callback)
|
||
if(debug_print) then
|
||
print("send host:"..self.host)
|
||
end
|
||
if self.c__netClient == nil then
|
||
return
|
||
end
|
||
if callback and type(callback) ~= TYPE_FUNC then return end
|
||
if data then
|
||
if type(data) ~= TYPE_TABLE then return end
|
||
end
|
||
local str = NULL_JSON
|
||
if data then
|
||
str = json.encode(data)
|
||
end
|
||
print("cmd:"..cmd.."str"..str)
|
||
self.c__netClient:Send(cmd,str,callback)
|
||
end
|
||
|
||
|
||
---c#网络层回调函数
|
||
function R.c__ondata(self,cmd,result,data,func)
|
||
local _response = nil
|
||
|
||
if type(data) == TYPE_STRING and string.len(data) > 0 then
|
||
_response = json.decode(data)
|
||
end
|
||
local new_response = {}
|
||
new_response.ReturnCode = result
|
||
new_response.Data = _response
|
||
|
||
|
||
if self.holdCallback ~=nil then
|
||
if self.holdCallback(new_response) then
|
||
return
|
||
end
|
||
end
|
||
|
||
func(new_response)
|
||
end
|
||
|
||
function R.setSession(self,session)
|
||
printlog("setSession==>>>",session)
|
||
|
||
self.session = session
|
||
if self.c__netClient == nil then
|
||
return
|
||
end
|
||
self.c__netClient.Session = session
|
||
end
|
||
|
||
function R.getSession(self)
|
||
printlog("getSession===>>>",self.session)
|
||
return self.session
|
||
end
|
||
|
||
function R.getAveragePingTime(self)
|
||
if self.c__netClient == nil then
|
||
return
|
||
end
|
||
return self.c__netClient.AveragePingTime
|
||
end
|
||
|
||
|
||
---c#网络层回调函数
|
||
function R.c__onevent(self,cmd,data)
|
||
local new_response = {}
|
||
local _response = data and json.decode(data) or nil
|
||
new_response.Command = cmd
|
||
new_response.Data = _response
|
||
self.onevent(new_response)
|
||
|
||
end
|
||
|
||
function R.clearActionQueue(self)
|
||
if self.c__netClient == nil then
|
||
return
|
||
end
|
||
self.c__netClient:ClearResponse()
|
||
end
|
||
|
||
---c#网络层回调函数
|
||
function R.c__onconnect(self,code)
|
||
if(debug_print) then
|
||
print("codeccccccccccccccccccccccccccccccccccccccc"..code)
|
||
end
|
||
self.onconnect(code)
|
||
end
|
||
|
||
function R.clearEvent(self)
|
||
self.onevent:Clear()
|
||
self.onconnect:Clear()
|
||
end
|
||
|
||
function R.setHold(self,func)
|
||
self.holdcallback = func
|
||
end
|
||
|
||
function R.destroy(self)
|
||
if self.c__netClient then self.c__netClient:Destroy() end
|
||
self.onconnect:Clear()
|
||
self.onevent:Clear()
|
||
self.c__netClient=nil
|
||
end
|