Summarize the server-related content for several years through the server solution.
The main body of the implementation is the lua script. The lua implementation mainly lacks two major parts:
1. Network
2. Database
These two parts must be extended through c/c ++
Net, mainly Server net and client net.
The connection initiated by the server is mainly used for platform verification, reporting, and inter-service communication. The interface is as follows:
_ Connect (ip, port, onconnect, onclose, timeout)
Parameters:
Ip address
Port
Onconnect connection callback
Onclose closed callback
The timeout value of the client socket connected by timeout. If the timeout value is exceeded, close is called back.
Returns a net object.
The server net monitors and collects messages from the client. The interface is as follows:
_ Listen (ip, port, onconnect, onclose, timeout)
Parameters:
Ip address
Port
Onconnect connection callback
Onclose closed callback
The timeout value of the client socket connected by timeout. If the timeout value is exceeded, close is called back.
Returns a net object.
Net object method:
Net: receive (sep1, sep2, sep3, maxlen, onrec), set receiving Conditions
Net: send (data)
Net: close (), actively close the connection
Assume that the communication protocol between the client and the server is: 4 bytes represents the length len, And the next len byte represents the real message.
Typical Server net Usage
Lolinoleic function onbody (net, data)
-- Data is a message package and can be processed as needed
Net: receive (4, onhead)
End
Local funciton onhead (net, data)
Local len = string. from32 (data)
-- Len is the length of the message package in bytes.
Net: receive (len, onbody)
End
Local function onconnect (net, snet)
Net: receive (4, onhead)
End
Local function onclose (net, msg)
Print ("net closed", msg)
End
Local snet = _ listen ("0.0.0.0", 80, onconnect, onclose, 60)
Assume that the message packet sent from the server to the client is as follows:
Typical use of the client is as follows:
Lolinoleic function onbody (net, data)
-- Data is a message package and can be processed as needed
Net: receive (4, onhead)
End
Local function onconnect (net, snet)
Net: receive (4, onhead)
End
Local function onclose (net, msg)
Print ("net closed", msg)
End
Local cnet = _ connect ("X. X", 8081, onconnect, onclose, 60)
Net: send (# data)
Net: send (data)
The libiop library is used in the network, because some of its APIs do not meet my needs. I made some modifications and sorted out the source code when I had no time (wait for a series of articles to finish writing, upload after the test is passed.
Both net and SQL expand lua. The highlights are in lua.