--[[
file name : SocketServerManager.Lua
author : Clark/陈泽丹
created : 2011-12-22
--]]
function GetLogicServerID()
return 1;
end
function GetCurServerID()
local ID = API_GetServerID()
myPrint("当前设备ID: " .. ID,1)
return ID
end
--创建服务器管理者
function newSocketServerManager()
local ID_MAX = 10000
local m_public = {}
m_public.tClient = {}
--获得ID
function m_public.CreateID()
for i=1, ID_MAX do
if nil == m_public.tClient[i] then
return i
end
end
return nil
end
--删除ID
function m_public.DeleteID(_ID)
m_public.tClient[_ID] = nil
end
--初始化ID
function m_public.InitObj(_ID, _Obj)
m_public.tClient[_ID] = _Obj
end
--获得大小
function m_public.getSize()
return ID_MAX
end
--钩子生成器
function m_public.newBaseSocket(_szCmd)
local ID = m_public.CreateID()
if nil == ID then
return nil
end
local pubComm = {}
--删除钩子器
function pubComm.delete()
m_public.DeleteID(ID)
end
--获得用户标示
function pubComm.getCmd()
return _szCmd
end
--初始化钩子器
function pubComm.InitObj(thisPoint)
m_public.InitObj(ID, thisPoint)
end
return pubComm
end
return m_public
end
--发送端服务器管理者
function newSendServerManager()
local m_protected = newSocketServerManager()
local m_public = {}
--发送生成器
function m_public.newSocket(_szCmd)
local pubComm = m_protected.newBaseSocket(_szCmd)
--发送给指定用户的信息
function pubComm.Send(_tBuf)
local t = { CMD = _szCmd, tBuf = _tBuf}
local szBuf = sz_T2S(t)
API_BroadcastData(szBuf, "ServerRecv_CallBack")
end
pubComm.InitObj(pubComm)
return pubComm
end
return m_public
end
--接收端服务器管理者
function newRecvServerManager()
local m_protected = newSocketServerManager()
local m_public = {}
--接收生成器
function m_public.newSocket(_szCmd, _szRecvFun)
local pubComm = m_protected.newBaseSocket(_szCmd)
if nil == pubComm then
return nil
end
--处理接收到的信息
function pubComm.Recv(_szCmd, _tBuf)
_szRecvFun(_szCmd, _tBuf)
end
pubComm.InitObj(pubComm)
return pubComm
end
--接收端
function m_protected.recv(_fromServerID, _szRecvFun)
local t = t_S2T(_szRecvFun)
if nil ~= t then
for i=1, m_protected.getSize() do
if nil ~= m_protected.tClient[i] and t.CMD == m_protected.tClient[i].getCmd() then
m_protected.tClient[i].Recv(t.CMD, t.tBuf)
end
end
end
end
return m_public, m_protected
end
SEND_G_SOCKET_SERVER_MANAGER = newSendServerManager()
RECV_G_SOCKET_SERVER_MANAGER, RECV_G_SOCKET_SERVER_MANAGER_FRIEND = newRecvServerManager()
function ServerRecv_CallBack(_SourceID, _buf1, _buf2, _buf3, _buf4, _buf5, _buf6, _buf7, _buf8, _buf9, _buf10, _buf11, _buf12, _buf13, _buf14, _buf15, _buf16)
local szBuf = _buf1 .. _buf2 .. _buf3 .. _buf4 .. _buf5 .. _buf6 .. _buf7 .. _buf8 .. _buf9 .. _buf10 .. _buf11 .. _buf12 .. _buf13 .. _buf14 .. _buf15 .. _buf16
RECV_G_SOCKET_SERVER_MANAGER_FRIEND.recv(_SourceID, szBuf)
end
------------------------------------------------------------------
--[[
file name : test.lua
author : Clark/陈泽丹
created : 2012-1-12
purpose :
--]]
------------------------------------------------------------------
G_CMD_INIT = "CMD_INIT"
G_CMD_LOGIN = "CMD_LOGIN"
G_CMD_ENTER = "CMD_ENTER"
G_CMD_TALK = "CMD_TALK"
G_CMD_MOVE = "CMD_MOVE"
function newLogicServer()
local m_public = {}
local m_protected = {}
local data = nil
local skSendInit = nil
local skRecvLogin = nil
local skRecvEnter = nil
local skSendTalk = nil
local skSendMove = nil
--------- 相关操作 ----------
--更新所有服务器
function m_public.UpdateData()
skSendInit.Send(data)
end
function m_protected.RecvLogin(_szCmd, _tBuf)
local szBuf = sz_T2S(_tBuf)
myPrint(_szCmd .. "|" .. szBuf, 1)
end
function m_protected.RecvEnter(_szCmd, _tBuf)
local szBuf = sz_T2S(_tBuf)
myPrint(_szCmd .. "|" .. szBuf, 1)
end
function m_public.Talk()
skSendTalk.Send({"你妈喊你回家吃饭!"})
end
function m_public.Move()
skSendMove.Send({"移动玩家"})
end
-------- 初始化与相关释放 ----------
data = { "初始化"}
skSendInit = SEND_G_SOCKET_SERVER_MANAGER.newSocket(G_CMD_INIT)
skRecvLogin = RECV_G_SOCKET_SERVER_MANAGER.newSocket(G_CMD_LOGIN, m_protected.RecvLogin)
skRecvEnter = RECV_G_SOCKET_SERVER_MANAGER.newSocket(G_CMD_ENTER, m_protected.RecvEnter)
skSendTalk = SEND_G_SOCKET_SERVER_MANAGER.newSocket(G_CMD_TALK)
skSendMove = SEND_G_SOCKET_SERVER_MANAGER.newSocket(G_CMD_MOVE)
function m_public.Delete()
skSendInit.delete()
skRecvLogin.delete()
skRecvEnter.delete()
skSendTalk.delete()
skSendMove.delete()
end
return m_public
end
function newProxyServer()
local m_public = {}
local m_protected = {}
local skRecvInit = nil
local skSendLogin = nil
local skSendEnter = nil
--------- 相关操作 ----------
function m_protected.RecvInit(_szCmd, _tBuf)
local szBuf = sz_T2S(_tBuf)
myPrint(_szCmd .. "|" .. szBuf, 1)
end
function m_public.Login()
skSendLogin.Send({"登陆"})
end
function m_public.Enter()
skSendEnter.Send({"进入"})
end
function m_protected.RecvTalk(_szCmd, _tBuf)
local szBuf = sz_T2S(_tBuf)
myPrint(_szCmd .. "|" .. szBuf, 1)
end
function m_protected.RecvMove(_szCmd, _tBuf)
local szBuf = sz_T2S(_tBuf)
myPrint(_szCmd .. "|" .. szBuf, 1)
end
-------- 初始化与相关释放 ----------
skRecvInit = RECV_G_SOCKET_SERVER_MANAGER.newSocket(G_CMD_INIT, m_protected.RecvInit)
skSendLogin = SEND_G_SOCKET_SERVER_MANAGER.newSocket(G_CMD_LOGIN)
skSendEnter = SEND_G_SOCKET_SERVER_MANAGER.newSocket(G_CMD_ENTER)
skRecvTalk = RECV_G_SOCKET_SERVER_MANAGER.newSocket(G_CMD_TALK, m_protected.RecvTalk)
skRecvMove = RECV_G_SOCKET_SERVER_MANAGER.newSocket(G_CMD_MOVE, m_protected.RecvMove)
function m_public.Delete()
skRecvInit.delete()
skSendLogin.delete()
skSendEnter.delete()
skRecvTalk.delete()
skRecvMove.delete()
end
return m_public
end
LS = newLogicServer()
LS.UpdateData()
LP = newProxyServer()
LP.Login()
LP.Enter()
LS.Talk()
LS.Move()