II. Photon V4 Engine Infrastructure Development framework--development of the first Photon program

Source: Internet
Author: User

Photon Basic Development Framework

Basic framework of Photon (V4). Development Framework main photon and game logic (C #) two parts, such as the latest Photon V4 support 4 kinds of low-level protocols, game development logic photon is mainly divided into load balancing and MMO (large multiplayer simultaneous online game).

First, Photon server Example 1, server-side development

New Solution Testphotonserver and new Class Library project Myphotonserver, class Library Add Photon reference (found in Lib of Photon installation directory)

Photon.SocketServer.dll
PhotonHostRuntimeInterfaces.dll

Why create a new class library project? All photon service-side programs are compiled into DLLs and run by PhotonControl.exe through configuration file calls.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingPhoton.socketserver;namespacemyphotonserver{ Public classMyserverapplication:applicationbase {protected Overridepeerbase Createpeer (initrequest initrequest) {return NewMyserverpeer (initrequest); }        protected Override voidSetup () {//Initialize                    }        protected Override voidTearDown () {//Close        }    }}
myserverapplication

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingPhoton.socketserver;usingphotonhostruntimeinterfaces;namespacemyphotonserver{ Public classMyserverpeer:clientpeer { PublicMyserverpeer (initrequest initrequest):Base(initrequest) {}protected Override voidOnDisconnect (Disconnectreason Reasoncode,stringreasondetail) {            //responding to client disconnects        }        protected Override voidonoperationrequest (operationrequest operationrequest, sendparameters sendparameters) {//Responding to client action requests        }    }}
Myserverpeer

Look at the code, here is the simplest photon server:

1, application for the service end of the program portal, all developers own program must have a inherit ApplicationBase class of the program entrance

2, peer for the service side and the client peering point of communication, the server and the client through the respective peer communication.

3. V4 version of the photon to. NET Development API has been adjusted in the original peerbase based on the different division of peer, here call Clientpeer, you can see the official source and Clientpeer There is nothing, careful friends can think why do

 Public Abstract class clientpeer:peerbase{    //  Methods    protectedbase( Initrequest)    {    }}

2. Service-Side deployment

1, PhotonControl.exe: First of all the Photon server program is compiled into a DLL, and then by the configuration by the PhotonControl.exe call run.

2, Photonserver.config file: Photoncontrol in the running directory will find this file, the main configuration of the developer program to Photoncontrol call.

3. Log:PhotonControl.exe will generate logs at the root of the run, and a log file on all service side will be generated under deploy.

Deployment:

in the Create a folder under the Deploy directory Testphotonserver, Right-click on our service-side class library project properties to redirect the release DLL under. Note To set the DLL in the new bin directory in Testphotonserver

(Because the example is simple release time Photon.SocketServer.dll, PhotonHostRuntimeInterfaces.dll, ExitGamesLibs.dll These DLLs are not published to the bin, here manually copied to deploy under the Testphotonserver/bin inside )

Configure Photonserver.config

Add application node to Photonserver.config applications, here I put the applications under loadblancing

      <ApplicationName= "Testphotonserver"basedirectory= "Testphotonserver"Assembly= "Myphotonserver"Type= "Myphotonserver.myserverapplication"Forceautorestart= "true"Watchfiles= "Dll;config"Excludefiles= "Log4net.config">      </Application>        

Name: Server program names

BaseDirectory: Set the Deploy directory as the base setting, where the service-side program file folder in the Deploy Testphotonserver

Assembly:application Entrance Program where namespace

Type: The full qualified name of the Ingress class

Forceautorestart: Forced restart as the name implies

Watchfiles: Called file suffix, dll, and config

Excludefiles: Typically the log profile name

Run PhotonControl.exe's loadbalancing to see that the custom service side is already running

Second, the client

The client temporarily adds a console project under the solution with a simple console, adding a reference Photon3DotNet.dll

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingExitGames.Client.Photon;namespacemyphotonclient{classMyphotonclientpeerlistener:iphotonpeerlistener { Public BOOLIsconnect =false;  Public voidDebugreturn (DebugLevel level,stringmessage) {                   }         Public voidOnEvent (EventData EventData) {} Public voidOnMessage (Objectmessages) {                    }         Public voidonoperationresponse (Operationresponse operationresponse) {} Public voidonstatuschanged (StatusCode StatusCode) {//the connection state with the server has changedConsole.WriteLine ("Current connection status to server:"+StatusCode); Switch(statusCode) { CaseStatusCode.Connect:IsConnect=true;  Break; }                    }    }}
Myphotonclientpeerlistener
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingExitGames.Client.Photon;namespacemyphotonclient{classProgram {Static voidMain (string[] args) {Myphotonclientpeerlistener Listener=NewMyphotonclientpeerlistener (); Photonpeer Peer=NewPhotonpeer (Listener, CONNECTIONPROTOCOL.UDP); if(peer.) Connect ("localhost:5055","MyServer") {Console.WriteLine ("The client prepares the connection request ...");  while(!Listener. Isconnect) {Console.WriteLine ("the connection ..."); Peer.                    Service (); System.Threading.Thread.Sleep ( -); } Console.WriteLine ("connected ..."); //Peer. Disconnect ();Console.readkey (); }            Else{Console.Write ("Server not found"); }        }            }}
Program

1, Iphotonpeerlistener interface mainly has 5 methods

Debugreturn Method : Mainly provide various kinds of errors and warnings "for developers" to review, in the development state to assist developers to correct. For example: The above client Program.cs address localhost:5055, changed to localhost:5050 run time or will not stop the request, but can not successfully connect, the program will not error. This time we'll print a message in the Debugreturn method to help find the source of the problem

OnEvent (EventData EventData): handles the events that photon server sends over to the client for processing. The event is used for client and server-side communication, and operations (operation) usually trigger an event, which can be passed through event code until the type. The message content of the time is usually contained in its parameters. Here for a brief introduction

OnMessage (Object messages): Message callback function

onoperationresponse (Operationresponse operationresponse): Responds to Operation callback functions, such as adding a game room operation, the server assigns a number to each client. The number of the client can be obtained by responding to the callback function.

onstatuschanged (StatusCode StatusCode): Connection state function, when the game's asynchronous operation completes the live error, the state changes the callback function

2, Photonpeer class

Photonpeer primary functionality is client and photon Server communication. Can be understood as peer communication points or barely understood as messengers. Photonpeer communicates via listener and communication protocols and service-side. Each application can have multiple photonpeer, but each of the different photonpeer should have its own listener to listen for events, actions, callback functions. The listener here is an instance of the class that inherits the Iphotonpeerlistener interface.

Peer. Connect calls do not go directly to the server, only when the Peer.service () call will be sent to the server request.

After the text and then the explanation

II. Photon V4 Engine Infrastructure Development framework--development of the first Photon program

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.