Online Game communication engine, high-performance iocp model (. NET)

Source: Internet
Author: User
Document directory
  • Steed. appbase
  • Steed. ASIO
  • Steed. ASIO. net
Preface

Steed is a general network communication library developed based on years of experience in communication engine development. It is developed in C ++ and supports High-concurrency connections. The bottom layer uses iocp technology. The design mode refers to the ASIO library in the famous C ++ quasi-standard library boost. The steed network communication library is released with the c ++ Dynamic Link Library version (networkioservices. DLL) and. NET Component version (networkengineservices. DLL ).

This is a set of code that has been tested for more than two years in Enterprise Project Practice. Now I am sharing it with programmers for free. You are welcome to download and use it.

 

Engine Overview

 

Network Engine Services is a set of network engine libraries used to develop TCP-based c/s structured applications, the purpose of the engine library is to allow network application developers to get rid of the development and maintenance of the underlying functions of the network and focus on the business logic development at the upper layer. It provides a very simple way to develop powerful network programs and solves some common network problems in advance:

1. Complete packet reception. If only some data is received, the underlying layer will wait for the remaining data and submit it to the upper layer completely.

2. Regular checks for malicious connections. If the client only connects to and does not send data, this occupies the number of connections, the underlying layer will detect and automatically close the connection.

3. prevent malicious data sending and occupy memory. If the client sends data in an invalid format, an error message is sent to the logic layer when the memory usage exceeds the standard size.

4. Use the memory pool internally to prevent memory fragments. The memory pool technology is used for allocation of underlying objects, and the release time is entirely determined by the user. If you forget to release the objects, the program will be automatically released when it exits.

Using Network engine services for development has the following features:

I. Flexible I/O services. I/O service is the iocp service, which is one of the core functions of the entire database. You can configure threads for it and flexibly dispatch thread tasks.

2. Controllable timer. Controllable means that the logic processing thread of the timer can be set. You can set a single thread to be multi-threaded and set the same thread as the logic processing.

3. Simple and high-performance thread pool. The I/O service-based thread pool can increase or decrease the number of threads at any time. A task queue is set up in the thread pool to automatically allocate threads to execute tasks. Developers only need to deliver tasks to the threads, you can use the I/O service mentioned above to deliver tasks.

4. concise code. There are only a dozen classes in the entire library, and there are only two or three main functions for each class. A few simple classes and several simple method calls can be used to develop enterprise-level applications with powerful functions and high performance.

5. Elegant Design. The main classes are dependent on the IO service class and are dependent on injection through constructor. The entire structure is elegant and easy to understand.

The underlying layer of network engine services is developed using C ++ and uses boost for reference. the development idea and mode of ASIO, but it does not use any file of boost, and does not rely on any function of boost. The code is completely compiled by the author. The underlying code is very lightweight and small, and the overall capacity is only about KB. After being encapsulated by. Net (C #), it is easier to use and efficient.

 

Running Environment

System Environment:

Windows Server 2003, Windows Server 2008, or Windows Server 2008 r2

Running platform:

Based on Microsoft. NET Framework 3.5 SP1.

Usage

1. Put networkengineservices. dll in the bin directory of the project (other directories are also acceptable ).

2. Reference networkengineservices. dll in the project. The specific method is to right-click Project> Add reference> browse, browse the directory where the component is located, select networkengineservices. dll, and click OK.

3. Import the namespace. There are four steed. appbase, Steed. ASIO, Steed. ASIO. net, and steed. ASIO. UDP. You do not need to import all data as needed.

View C ++ API Description: http://blog.csdn.net/xiaoluo123/article/details/6845636

 

View server-side code example: http://blog.csdn.net/xiaoluo123/article/details/6629779

View client code example: http://blog.csdn.net/xiaoluo123/article/details/6629795

 

Network Engine Services core class description

Network Engine Services contains three major namespaces: steed. appbase, Steed. ASIO, and steed. ASIO. net.

 

Steed. appbase

The steed. appbase namespace has two classes: errorcode and errorresult. Errorcode is used to receive and return errors. Errorresult has two values. One value indicates no error (errorresult. None), the other value indicates an error returned (errorresult. Error), and the other value indicates some errors in the network engine.

Example:

Errorcode EC = new errorcode (errorresult. None, "no error ");

Or

Errorcode EC = new errorcode (errorresult. error, "error message ");

 

Steed. ASIO

The steed. ASIO namespace has three classes: ioservice, steedtimer, and threadpool. The core class of the ioservice engine, providing the iocp function. Both steedtimer (timer) and threadpool (thread pool) are based on ioservice.

Ioservice example:

Ioservice = new ioservice ();

Ioservice. Dispatch (eventhandler); // assign a task.

Ioservice. Dispatch (eventhandler, ARG); // assign a task with an object-type parameter.

 

 

 

Steedtimer example:

Steedtimer timer = new steedtimer (ioservice );

Timer. ontick = ontimertick; // ontimertick is the timer event processing method.

Timer. Start (1000); // an event is triggered every 1000 milliseconds.

Timer. Stop (); // stop the trigger event.

 

 

 

Threadpool example:

Threadpool = new threadpool (ioservice );

Threadpool. Run (1); // start the thread. Parameter 1 indicates that the number of start threads is 1.

Threadpool. addthreads (1); // Add a thread.

Threadpool. abortthreads (1); // exit a thread.

Threadpool. Exit (); // exit all threads.

 

 

Steed. ASIO. net

The steed. ASIO. Net namespace has six classes: acceptor, connector, contextsocket, globalsettingsmanager, netdispatcher, and netsender.

Netdispatcher example:

Ioservice handlerioservice = new ioservice ();

Netdispatcher dispatcher = new netdispatcher (handlerioservice );

// Event processing function.

Dispatcher. onconnected = connectedcompletion;

Dispatcher. onreceived = receivecompletion;

Dispatcher. onsended = sendcompletion;

Dispatcher. Open (1); // start the event processing thread. This thread pool is built-in and the parameter is the number of startup threads.

 

Example of acceptor:

Ioservice acceptioservice = new ioservice ();

Acceptor = newacceptor (acceptioservice );

Acceptor. Open (6666, 1, dispatcher); // The parameters are listener port, thread pool thread count, and event Dispatcher in sequence.

Isocketcontext newsocket = new socketcontext (acceptioservice );

Errorcode EC = acceptor. acceptasync (newsocket );

If (errorresult. None! = EC. value)

Newsocket. Destroy ();

 

Socketcontext example:

The socketcontext object must be connected successfully before it can be used.

Newsocket. receiveasync ();

Newsocket. sendasync (encoding. Default. getbytes ("sent data ."));

 

Globalsettingsmanager is a static class that allows you to set the buffer size for receiving and sending programs. It must be called before the program runs.

Globalsettingsmanager. setbufferlength (1024 );

 

View C ++ API Description: http://blog.csdn.net/xiaoluo123/article/details/6845636

 

View server-side code example: http://blog.csdn.net/xiaoluo123/article/details/6629779

View client code example: http://blog.csdn.net/xiaoluo123/article/details/6629795

 

 

Download csdn Resources

 

If you have any questions, you can add qq630307889.

 

 

 

 

 

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.