JGSL_serverproxy
epolling style server proxy for client communicating with server.
Title |
epolling style server proxy for client communicating with server. |
Author(s) |
LiXizhi |
Date |
2008/12/23 |
File |
script/kids/3DMapSystemNetwork/JGSL_serverproxy.lua |
Description
Event polling implements a messaging pattern that the client only send another packet when server replies.
Sample Code
NPL.load("(gl)script/kids/3DMapSystemNetwork/JGSL_serverproxy.lua");
local proxy = Map3DSystem.JGSL.ServerProxy:new({DefaultFile="your_server_neuron_filename.lua"});
if(proxy:CheckState()) then
proxy:Send(...);
end
Member Functions
ServerProxy:new
proxy status
local proxystate = {
-- proxy is reset
none = nil,
-- waiting for server response
waiting = 1,
-- server already responded, the client can send message at anytime
ready = 2,
-- the server is timed out at least once
timeout = 3,
-- the server is dead, because it has timed out too many times.
dead = 5,
}
------------------------------------
a server proxy is used by client to communicate with a epoll server.
------------------------------------
local ServerProxy = {
-- nil means we are ready to send a packet.
state = proxystate.none,
-- the last time the client sends message to a server proxy, this is measured by the local clock.
LastSendTime,
-- the last time the client receives message from the server, this is measured by the local clock.
LastReceiveTime,
-- default neuron file
DefaultFile,
-- JID
jid,
-- the jabber client to use. if nil, JGSL.GetJC() is used.
jc,
-- whether we have signed in to the server and has its session key in cookies,
SignedIn,
-- cookies: nil or a table containing name value pairs, such as {sk="sessionkeyhere"}
cookies,
-- id of this grid node on the server side.
id,
-- default server timeout time
-- usually if the server is not responding in 20 seconds, we will report to user about connecion lost or unsuccessful.
ServerTimeOut = 20000,
-- we will retry this number of times if the server time outs. If this is 0, we will never retry, but put to this proxy to proxystate.dead immediately upon first time out.
MaxTimeOutRetry = 1,
-- how many timeout retry times we have done.
TimeOutCount = 0,
-- default keep alive interval
KeepAliveInterval = 10000,
-- the following is just for grid server proxy. host user id in paraworldAPI
uid = nil,
-- The role that the server assigned to this client. It can be one of the "guest", "administrator", "friend".
UserRole = "guest",
-- we will recover at most maxrecoversize number of intact agents at a time.
maxrecoversize = 5,
-- a commar separated string containing the nid of to be recovered agents.
recoverlist = nil,
-- world path: worlds/MyWorlds/ABC
worldpath = nil,
-- world name: my_worlds
worldname = nil,
-- description
desc = nil,
-- current online user number
OnlineUserNum = 0,
StartTime = 0,
VisitsSinceStart = 0,
ServerVersion = 0,
ClientVersion = 0,
-- server session key, it is assigned when connected with a remote server. sk is forwarded for each client to server packet.
sk = nil,
-- client time as seen by server
ct = nil,
-- last server time as tolde by server
st = nil,
-- grid tile pos, it marks the simulation region within the self.worldpath.
-- from (x*size, y*size) to (x*size+size, y*size+size)
x=nil,
y=nil,
-- grid tile size, if nil, it means infinite size. we always return the smallest sized grid server
size=nil,
-- these are computed on demand
from_x=nil, from_y=nil, to_x=nil, to_y=nil,
}
ServerProxy =
ServerProxy;
syntax
function ServerProxy:new (o)
parameters
ServerProxy:Reset
it will send CS_Logout if old connection contains cookies
syntax
function ServerProxy:Reset()
ServerProxy:Logout
log out only if it has signed in before
but retains session key and self.st and put the proxy to ready state.
NOTE: this proxy can be reused when the client need it again, since it retains all session key and server time (self.st).
syntax
function ServerProxy:Logout()
ServerProxy:CanEdit
check whether this grid node allow users to edit the world
- param rule : if nil, it means editing right.
syntax
function ServerProxy:CanEdit()
ServerProxy:IsEqual
return true if the input proxy is same as self.
they are only same when the jid, world path and region all matches.
syntax
function ServerProxy:IsEqual(proxy)
parameters
ServerProxy:Contains
whether this node contains the 3d point x,y,z in worldpath
syntax
function ServerProxy:Contains(worldpath,x,y,z)
parameters
ServerProxy:Send
send a message to server using this proxy.
- param msg : msg to send
- param neuronfile : if nil, self.DefaultFile is used.
syntax
function ServerProxy:Send(msg, neuronfile)
parameters
msg |
msg to send |
neuronfile |
|
ServerProxy:OnRespond
call this function whenever the proxy has a response from the server.
it makes the proxy ready to send another message.
syntax
function ServerProxy:OnRespond()
ServerProxy:UpdateSessionKey
update session key
- param bInsertToCookie : if true, it will insert sk to cookies, so that sk will be sent along with all subsequent Send calls.
syntax
function ServerProxy:UpdateSessionKey(sk, bInsertToCookie)
parameters
sk |
|
bInsertToCookie |
if true, it will insert sk to cookies, so that sk will be sent along with all subsequent Send calls. |
ServerProxy:IsReady
whether the proxy is in ready state
ready state means that the proxy is connected and not waiting for response.
syntax
function ServerProxy:IsReady()
ServerProxy:MakeReady
force the proxy to ready state
syntax
function ServerProxy:MakeReady()
ServerProxy:IsKeepAlive
whether deltaTime is passed since the last time that we send message to server.
if return true, we usually need to send a normal update to server to keep the client alive.
- param deltaTime : in milliseconds. usually several times the normal update interval. If nil, self.KeepAliveInterval is used.
syntax
function ServerProxy:IsKeepAlive(deltaTime)
parameters
deltaTime |
in milliseconds. usually several times the normal update interval. If nil, self.KeepAliveInterval is used. |
ServerProxy:IsDead
whether the server is dead
syntax
function ServerProxy:IsDead()
ServerProxy:IsTimeOut
return true if we are not receiving server response for too long
syntax
function ServerProxy:IsTimeOut(curTime)
parameters
ServerProxy:AddToRecoverList
append to the recover list of this proxy.
the recover list has a max size. if max size is reached, we will ignore and return nil.
- return __ : return true if added to recover list or already added before.
syntax
function ServerProxy:AddToRecoverList(agent)
parameters
agent |
|
return |
return true if added to recover list or already added before. |
ServerProxy:log
output a log message
syntax
function ServerProxy:log(...)
ServerProxy:Dump
output to log. for debugging only.
syntax
function ServerProxy:Dump()
ServerProxy:CheckState
update the self.state according to the current time.
it will change the state from wait to timeout if the server is not responding since last send
- return __ : return true if state is proxystate.ready or proxystate.timeout, meaning that u should send an update immediately.
syntax
function ServerProxy:CheckState(curTime)
parameters
curTime |
|
return |
return true if state is proxystate.ready or proxystate.timeout, meaning that u should send an update immediately. |