Skip to content

FireflyAPI netconnect API

9miao Mobile Game edited this page Aug 7, 2014 · 1 revision

FireflyAPI-netconnect API

package netconnect

Netconnect contains some handling methods of server to client communication, such as encapsulation of sending data and protocol header, subpackage in TCP communication and stick pack handling.

class Connection

A connection between server and client is called as object, we can communicate with client, active lose connections and push messages via this object.

loseConnection

Lose the connection with client

type method
//cwd firefly/ netconnect / connection.py
Params loseConnection(self)

safeToWriteData

Send messages to client
type method
//cwd firefly/ netconnect / connection.py
Params safeToWriteData(self,topicID,msg)
topicID: command number id, str type
msg: messages that need to send, str type
Send msg to client according to the method with command number topicID

class DataPackProtoc

the definition of data package protocol. We need to import HEAD_0: int protocol header 0
HEAD_1: int protocol header 1
HEAD_2: int protocol header 2
HEAD_3: int protocol header 3
protoVersion: int protocol header version number
serverVersion: int protocol header version number

These parameters form a complete protocol header, default to 0 if we do not import these parameters before the instantiation, but we can also customize setup by using below methods after the instantiation.

setHEAD_0
Set protocol header 0
type method
//cwd firefly/ netconnect / datapack.py
Params setHEAD_0(HEAD_0)
HEAD_0: protocol header 0, int type
Set protocol header 0 as HEAD_0

setHEAD_1
set protocol header 1
type method
//cwd firefly/ netconnect / datapack.py
Params setHEAD_1(HEAD_1)
HEAD_1: protocol header 1, int type
Set protocol header 1 as HEAD_1

setHEAD_2
set protocol header 2
type method
//cwd firefly/ netconnect / datapack.py
Params setHEAD_2(HEAD_2)
HEAD_2: protocol header 2, int type
Set protocol header 2 as HEAD_2

setHEAD_3
set protocol header 3
type method
//cwd firefly/ netconnect / datapack.py
Params setHEAD_3(HEAD_3)
HEAD_3: protocol header 3, int type
Set protocol header 3 as HEAD_3

setprotoVersion
Protocol header version number
type method
//cwd firefly/ netconnect / datapack.py
Params setprotoVersion(protoVersion)
protoVersion: protocol header version number, int type
Set protocol hader version number as protoVersion

setserverVersion
Set server version number
type method
//cwd firefly/ netconnect / datapack.py
Params setserverVersion(serverVersion)
serverVersion: server version number, int type
Set server version number as serverVersion

getHeadLenght

Obtain data package length
type method
//cwd firefly/ netconnect / datapack.py
Params getHeadLenght()
Return value is 17

unpack
Unpacking
type method
//cwd firefly/ netconnect / datapack.py
Params unpack(dpack)
Dpack: packaged data, str type
Analyze the packaged data and return a dictionary {'result':xxx,'command':xxx,'length':xxx}, here ‘result’ indicates whether the analysis is successful or not, ‘command’ indicates command number and ‘length’ indicates the length of received message.

pack
Pack the data package
type method
//cwd firefly/ netconnect / datapack.py
Params pack(response,command)
Response: data that need to package
Command: command number
Return packaged data

class ConnectionManager

Connection manager

getNowConnCnt

Obtain the ongoing connections number
type method
//cwd firefly/ netconnect / manager.py
Params getNowConnCnt()
Return the ongoing connections number

addConnection

Add a connection
type method
//cwd firefly/ netconnect / manager.py
Params addConnection(conn)
Conn: connection object, object type

dropConnectionByID

Delete connection instances by ID
type method
//cwd firefly/ netconnect / manager.py
Params dropConnectionByID(connID)
connID: connection id, int type

getConnectionByID

Obtain a connection by ID
type method
//cwd firefly/ netconnect / manager.py
Params getConnectionByID(connID)
connID: connection id, int type
Return connection object, return None otherwise.

loseConnection

Lose connection with client according to connection ID
type method
//cwd firefly/ netconnect / manager.py
Params loseConnection(connID)
connID: connection id, int type

pushObject

Active push messages
type method
//cwd firefly/ netconnect / manager.py
Params pushObject(topicID , msg, sendList)
topicID: command number id, int type
msg: messages that need to send, str type
SendList: connection object list of msg, list type [object,object]
Send msg to client that connects with all connection objects of list according to the method with topicID command number.

class LiberateProtocol

Protocol class that communicate with clients

connectionMade

The processing when connection with client is made
type method
//cwd firefly/ netconnect / protoc.py
Params connectionMade()

connectionLost

The processing when connection with client is lost
type method
//cwd firefly/ netconnect / protoc.py
connectionLost(reason)

safeToWriteData

Send data to client
type method
//cwd firefly/ netconnect / protoc.py
safeToWriteData(data,command)
data: data that need to write into client, str type
Command: command number, int type

dataHandleCoroutine

Accept coroutine of data processing in client
type method
//cwd firefly/ netconnect / protoc.py
Params dataHandleCoroutine()

dataReceived

The processing of data received.
@param data: str : data that send from client
type method
//cwd firefly/ netconnect / protoc.py
Params dataReceived(data)

class LiberateFactory

Protocol factory

setDataProtocl

Set data package protocol
type method
//cwd firefly/ netconnect / protoc.py
Params setDataProtocl(dataprotocl)
Dataprotocl: protocol instance, instance type
Customize data package protocol

doConnectionMade

The processing when connection is made
type method
//cwd firefly/ netconnect / protoc.py
Params doConnectionMade(conn)

doConnectionLost

The processing when connection is lost)
type method)
//cwd firefly/ netconnect / protoc.py)
Params doConnectionLost(conn))

addServiceChannel

Add service channel
type method
//cwd firefly/ netconnect / protoc.py
Params addServiceChannel(service)
Service: service instance, instance type

doDataReceived

The processing when data is received
type method
//cwd firefly/ netconnect / protoc.py
Params doDataReceived(conn,commandID,data)

produceResult

Produce ultimate result that client needs
@param response: str : result that distributed client obtained
type method
//cwd firefly/ netconnect / protoc.py
Params produceResult(command,response)
Command: command number, int type
Response: data that need to send
Return data that have been packaged by protocol.

loseConnection

Active lose connection with client
type method
//cwd firefly/ netconnect / protoc.py
Params loseConnection(connID)
connID: connection id, int type
Lose connection with client according to connID

pushObject

Push messages from server to client
type method
//cwd firefly/ netconnect / protoc.py
Params pushObject(topicID , msg, sendList)
topicID: int command number
msg: message content, protobuf structure type
sendList: target list that is sending to (client id list)
send msg to all clients in sendList by using command number topicID