Web Game mobile game server (I)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.