C + + implementation of mobile gaming LAN online battle Library

Source: Internet
Author: User

Preface

I always want to be a LAN online game, but whether it is used or Bluetooth to develop the way to learn the knowledge of the relevant platform. In fact, I have learned this knowledge last year, and it is not reliable to show that I will encounter these problems:

    1. Bluetooth on iOS cannot connect to other manufacturers ' Bluetooth devices;
    2. WiFi seems to be the same as the first one;
    3. How do I know if a Bluetooth or WiFi device is on the phone where my game is launched?

Because of the different platforms involved, there are many problems, I also do not bother to solve these problems. So I chose a lazy way, using the TCP protocol to the LAN online battle, so that there is a premise that more than one device must be in a LAN. But it can be cross-platform, and it doesn't have to be that troublesome. So the Buddy library was born, this name is used to commemorate the childhood play the small game machine's small friends.

Implementation Principle

The realization principle is very simple, first the host player opens a game room, broadcasts the host player's address port information to the LAN, and launches the game server. Other players receive the host player broadcast message (the broadcast information contains the game server address and port information), connected to the game server, so as to achieve the purpose of online battle. These steps Buddy Library has been packaged, we can use directly.

Instructions for use

Use is also relatively simple, here the service side and the use of the client separate to explain, in fact, they are very similar.

Service Side

Example code:

1#include <iostream>2#include"Classes/buddy.h"3 4 enumemessagedelegate5 {6Buddyserviceid = (int) Reserveddelegateid::usercustom +1,7 };8 9 classSessionmanage: PublicservicedelegateTen { One  Public: A     Virtual voidOnplayerjoin (SOCKET session,ConstAddress &address)Override -     { -Std::cout <<"Onplayerjoin"<<Std::endl; the  -         CharBuffer[] ="Hello"; -SendData (Session, Buffer,sizeof(buffer)); -     } +  -     Virtual voidOnplayerleave (SOCKET session,ConstAddress &address)Override +     { AStd::cout <<"Onplayerleave"<<Std::endl; at     } -  -     Virtual voidOnmessagereceive (SOCKET session,void*data, size_t size)Override -     { -Std::cout <<"onmessagereceive"<<Std::endl; -     } in }; -  to intMainintargcChar**argv) + { - Initbuddy (); the  * Sessionmanage Manage; $Buddyservice Server (Address (khostport),Ten, Buddyserviceid, &manage);Panax Notoginseng server. Accept (); -  the      while(true) +     { A std::this_thread::sleep_for (Std::chrono::milliseconds (Kframedeltatime)); theMessagemanager::getinstance ()->update (0); + server. Broadcast (); -     } $  $     return 0; -}

First, line 4th declares an enumeration type that represents the ID of the message agent. A message queue is implemented inside the Buddy library and requires a game Master loop to update it. We can specify an ID for an object (the class type of the object must inherit from Messagedelegate) so that when the message is sent to that ID, the object's Onmessagereceive function is called.

Implement a class Sessionmanage on line 9th, inheriting from Servicedelegate. In fact, this class is the management class for all sessions, and when the player enters or leaves or receives data from the player, the virtual function of the Servicedelegate class is called. In addition, the Servicedelegate class also provides several other functions to manage the player.

The 33rd line is to initialize the library, which must be called in the main thread.

Line 36th creates a game server in which the parameters of the constructor are the address of the server, the ID of the message agent, and the pointer to the service proxy. The service agent is actually an instance of the conversation management class that was said earlier.

After the 37th line calls accept, the connection request within the LAN will be accepted and the thread will not be blocked.

The 39th line is actually in the simulation game's main loop, is generally 60 frames per second, a frame time is 16 milliseconds.

The 42nd line is to update the message queue in the main game loop.

The 43rd line is to broadcast messages within the LAN so that the players on the LAN can discover the server.

Client

Example code:

1 1#include <iostream>2 2#include"Classes/buddy.h"3 4 enumemessagedelegate5 {6Buddyclientid = (int) Reserveddelegateid::usercustom +1,7 };8 9 classConnectmanage: PublicclientdelegateTen { One  Public: A     Virtual voidOnfoundhost (ConstAddress &address,intNumber ) -     { -Std::cout <<"Foundhost:"<< address. ToString () <<Std::endl; theStd::cout <<"Number of people online:"<< number <<Std::endl; -Client_->Connect (address); -     } -  +     Virtual voidonnotfoundhost () -     { +Std::cout <<"Onnotfoundhost"<<Std::endl; A     } at  -     Virtual voidonconnectsuccess () -     { -Std::cout <<"onconnectsuccess"<<Std::endl; -     } -  in     Virtual voidOnconnectfail () -     { toStd::cout <<"Onconnectfail"<<Std::endl; +     } -  the     Virtual voidOnDisconnect () *     { $Std::cout <<"OnDisconnect"<<Std::endl;Panax Notoginseng     } -  the     Virtual voidOnmessagereceive (void*data, size_t size) +     { AStd::cout <<"onmessagereceive"<<Std::endl; theStd::cout << (Char*) Data <<Std::endl; +     } -  $  Public: $     voidSetbuddyclient (Buddyclient *client) -     { -Client_ =client; the     } - Wuyi  Public: thebuddyclient* Client_ ={nullptr}; - }; Wu  - intMainintargcChar**argv) About { $ init_sockets (); - messagemanager::getinstance (); -  - Connectmanage Manage; ABuddyclient Client (Buddyclientid, &manage); +Manage. Setbuddyclient (&client); theClient. Searchhost ( -); -  $      while(true) the     { theStd::this_thread::sleep_for (Std::chrono::milliseconds ( -)); theMessagemanager::getinstance ()->update (0); the     } -  in     return 0; the}

You'll find the usage of the server very similar, right? Here's a different place to talk.

The 9th line implements a class Connectmanage, which is a connection management class (always only one connection).

Line 61st is similar to the usage of the server, creating a client that constructs an ID for the incoming message agent and a pointer to the management class.

Line 63rd calls the Searchhost function will be in the LAN search has no game server, the parameter is the time-out, in seconds. This operation does not block the main thread.

The 65th line is also the main thread of the simulation game, which updates the message queue in the main thread.

It is important to note that the logic of the game is usually implemented in the session management class or the connection management class. Only one message queue is updated per frame in the main thread of the game. The proxy ID of a message must be unique for each object, meaning that two different objects cannot have the same message agent ID.

Source Download

Https://github.com/zhangpanyi/Buddy

C + + implementation of mobile gaming LAN online battle Library

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.