Netty Building a game server (i)

Source: Internet
Author: User

One, what is Netty?

1,netty is a Java open source framework provided by JBOSS.

2,netty is a jar package that can be developed using the All-in-one jar package.

3,netty does not need to run on a server like Tomcat, he is building a server alone.

4,netty can build HTTP server, socket server, WebSocket server and so on.

5,netty is actually a package of NIO that is offered after JDK1.4, and NiO is the new i/o,jdk1.7 launch NIO2.

Second, Netty service architecture diagram

The following official map shows the basic services provided by Netty:

The main provision is the HTTP server, socket server, WebSocket server.

Three, Netty schematic diagram

From the following original diagram, you can basically see the principle of Netty building server

1, component noun explanation

(1), bootstrap/serverbootstrap (Establish connection):

The Netty boot component, which simplifies the development steps of NIO, is the beginning of a Netty program that configures and concatenates individual components.

(2) Eventloopgroup (Event loop Group):

is a eventloop combination that can contain multiple eventloop. When creating a Eventloopgroup, the internally contained method creates a sub-object EventLoop.

(3) EventLoop (event loop):

The circular service channel can contain multiple channel.

(4) Channel (Channels):

Represents a scoket connection, or other IO operation component.

(5) Channelinitializer (Initialize connection):

Mainly provides a transmission channel channelpipeline.

(6) Channelpipeline (transmission channel):

The main management of various Channelhandler business controller, provide a chain management mode.

(7) Channelhandler (Business Controller):

Where the main business is written by developers, Netty also provides a lot of well-written controllers and adapters that can be referenced directly.

(8) Channelinboundhandler (channel incoming controller):

Inherit to Channelhandler and control incoming events in the transport channel.

(9) Channeloutboundhandler (channel outgoing controller):

Inherit to Channelhandler to control outgoing events in the transport channel.

(10) Decoder (decoding):

Network transmission is a byte transmission, so Netty first received a byte, need to decode, programming Java object.

Netty provides a number of decoders, including the Google Protobuf encoding shown on the service architecture diagram (top), which is a cross-platform, small-volume encoding that Google offers, which can be decoded directly in Netty.

(11) Encoder (Code):

Similar to decoding, the outgoing server needs to be encoded into a byte to be transmitted to the client.

(12) Future/channelfuture (message returned, not on the graph):

Netty provides a return result, similar to a callback function, that tells you what the result of the execution is.

2, building the server basic method

As you can see from the schematic diagram, there are 3 basic steps to building a Netty server:

1), Serverbootstrap (Establish connection): Constructs a scoket or other connection to establish the channel through the event loop.

2), Channelinitializer (Initialize connection): Build the transport channel for managing the controller.

3), Channelhandler (Business Controller): Build business controller, the most basic is decoding, encoding, information in/out.

Similarly, building a client is also these steps, but establishing a connection is using bootstrap and using an event loop, and the server typically uses two event loops.

The specific code can look at the section content: Build a simplest socket server and client.

Iv. What are the advantages of Netty building a game server?

1, the game server is a complex composition, the basic requirements are low latency and high concurrency. Game servers often require a variety of communications, such as map servers, managed servers, and chat servers. Netty's asynchronous NIO framework guarantees high-performance communication capabilities.

2, flexible encoding and decoding customization capabilities, to meet the different game scenarios of multi-protocol and private Protocol codec.

3, configurable thread pool, TCP parameters, provides differentiated customization capabilities.

4, Black and white list filter (IP filter), SSL security, can be used to login authentication link.

5,netty Heartbeat detection (link validity detection) to detect if the client is active. Heartbeat detection means that the server periodically sends the specified content to the client, through feedback to determine whether the link is available, whether the client is alive, and whether the message can be received and sent normally.

6, Traffic shaping, an initiative to adjust the flow rate of output measures. function is

1), prevent the downstream network element is crushed due to the uneven performance of the upper and lower network elements, business process interruption;

2), to prevent the communication module received the message too fast, the backend business thread processing is not timely caused by the "dead" problem.

The popular shaping principle is to cache discarded messages first, put them in a queue, and send cached messages when there are enough tokens. This approach can relieve stress, but there is a certain delay.

7, the ability to log statistics, to provide game service serviceability.

8, based on the memory pool of object reuse technology, reduce GC frequency, so that players do not card.

Netty Building a game server (i)

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.