Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the jwEngine framework

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you how to use the jwEngine framework, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

JwEngine

A cross-platform c++lua server quick solution, the framework can quickly respond to server development work, the design idea: "make things easier"

The bottom layer of the network uses libuv (node.js underlying library), asynchronous io helps single thread to release surging power, and supports epoll, iocp and ipv6 across platforms. The framework supports tcp, udp/kcp, websocket, http, and ensures the consistency of interfaces. Sol2 is used to export all interfaces to lua, and you can choose to develop logic with lua.

Use modern C++ development, as much as possible to use std::move, std::string_view to reduce memory replication.

The framework uses asynchronous events, not recommended to use multi-threading, to avoid multi-threaded context switching overhead and destroy the beauty of the code, the network part and logic part use a main event loop driver. The proposed scheme is the horizontal expansion of multi-process and single-thread, which controls the granularity of each process according to the business. Of course, mysql and redis can be added to the thread pool.

Create a tcp server

It only takes a few lines of code to create a tcp high-performance server, and automatically process packet headers and sticky packets (where packet headers contain message length and protocol number), and build a good NetPacket for you.

Class INetEvent: public NetEvent {public: virtual void onAccept (NetConnect * conn) {} virtual void onClose (NetConnect * conn) {} virtual void onMsg (NetConnect * conn, int msgtype, NetPacket * pack) {}}; int main () {EventLoop::Instance ()-> init (); INetEvent eve; NetServer server (EventLoop::Instance (), & eve); server.listen ("127.0.0.1", 3001); return EventLoop::Instance ()-> run () } create a kcp server

C++ kcp server example, quickly build your frame synchronization server to ensure the reliability of messages

Class KNetEvent: public KcpEvent {public: virtual void onAccept (KcpSession * conn) {}; virtual void onClose (KcpSession * conn) {}; virtual void onMsg (KcpSession * conn, int msgtype, UdpPacket * pack) {} virtual void onUdpTimeout (KcpSession * s) {}}; int main () {EventLoop::Instance ()-> init (); KNetEvent eve; KcpServer server (EventLoop::Instance (), & eve); server.start ("127.0.0.1", 3001) Return EventLoop::Instance ()-> run ();} create a websocket server

Automatically complete the work of parsing websocket protocol

Class IWebEvent: public WebSocketEvent {public: virtual void onHandshake (WebSocketConnect * conn) {}; virtual void onAccept (WebSocketConnect * conn) {}; virtual void onClose (WebSocketConnect * conn) {}; virtual void onMsg (WebSocketConnect * conn, WebSocketPacket * pack) {};}; int main () {EventLoop::Instance ()-> init (); IWebEvent wevent; WebSocketServer server (EventLoop::Instance (), & wevent); server.listen ("127.0.0.1", 8080) Return EventLoop::Instance ()-> run ();} create a http server

Http only supports simple get post requests

Const char * html = R "(login

Hello world!

) "; const char * succeed ="login succeed"; const char * failing = "login failing"; int main () {EventLoop::Instance ()-> init (); HttpServer server (EventLoop::Instance ()); server.listen ("127.0.0.1", 80) Server.addGet ("/", [] (HttpConnect * conn, std::string_view & data) {conn- > autoMsg (html);}); server.addPost ("/ login", [] (HttpConnect * conn, std::string_view & data) {HttpParam hp (data); if (hp.getStr ("user") = = "jw" & hp.getStr ("pass") = = "1111") {conn- > autoMsg (succeed) } else {conn- > autoMsg (failing);}}); return EventLoop::Instance ()-> run ();} mysql and thread pool

This time we use the lua example:

Local config = DBConfig:new () config.device = "mysql" config.ip = "127.0.0.1" config.dbname = "jw_test" config.user = "root" config.pswd = "1111" config.port = 3306 pool = DBThreadPool:new (config) pool:create (1) func = function (err) Result) while (result:fetch ()) do local id = result:getInt32 () local num = result:getInt32 () local name = result:getString () local str = "id:". Id.. "num:". Num.. "name:". Name print (str) end end function exec () local sql = SqlCommand:new ("select * from test where id =?" Sql:pushInt32 (1) sql:addToPool (pool, func) end event_init () exec () timer = UTimer:new () timer:start (function () pool:update () end, 10,10) event_run () any extension process node

You can expand your process as much as you like, for example:

Base process cell process db process start engine.exe base.luastart engine.exe cell.luastart engine.exe db.lua provides a serialization serialization tool

The syntax, similar to C++, is very easy to write. Examples:

Struct testmsg {int32 x int32 y int32 z int8 state vector vec read {[x, y, z, state] if (state = = 1) {[vec]}} write {[x, y, z, state, vec]}}

Through the serialization tool, the description file of the protocol can be generated into C++ and lua code, and the function implementation of read () and write () can be generated automatically, which makes the data structure quickly mapped to SocketBuffer.

The current serialization serialization tool is experimental and may be fragile, and a more powerful protobuf is recommended. The project has integrated lua-protobuf, making the agreement between C++ and lua seamless.

Open source address of lua-protobuf: lua-protobuf

This example shows the communication between the C++ client and the lua server: quickly build the server Demo

Construction

You need a modern caterpillar 17 compiler

Vs2017 test passed

Gcc version 9.3.0 test passed

The above is how to use the jwEngine framework. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report