Photon Server implementation of the registration and log in (b) --- server-side code cleanup

  

First, some of the code will be used front and rear. Code number of such requests. New projects need to use to store common code.

  New Project Common store common code:

EventCode: storage server automatically send information to the client code
OperationCode: distinguish between requests and responses
ReturnCode: server response code
ParameterCode: resolution parameters 
Toos / DicTool.cs: data are basically read with Dictionary, where the tool bit words.

 

 

 

Second, the code

Toos/DicTool.cs
using System.Collections.Generic;

namespace Common.Toos
{
    public class DictTool
    {
        // define a static function, passing the key, can be taken Dict values corresponding 
        public  static T2 the GetValue <Tl, T2> (the Dictionary <Tl, T2> dict, Tl Key)
        {
            T2 value;
            bool ok = dict.TryGetValue(key, out value);
            if (ok)
            {
                return value;
            }
            else
            {
                return default(T2);
            }
        }
    }
}
View Code
 EventCode.cs
namespace Common
{
    // server automatically sends to the client event type 
    public  enum EventCode: byte
    {
        
    }
}
View Code
OperationCode.cs
namespace Common
{
    // distinguish request and response requests 
    public  enum OperationCode: byte
    {
        Login,
        Register,
        Default
    }
}
View Code
ReturnCode.cs
namespace Common
{
    public enum ReturnCode
    {
        Success,
        Failed
    }
}
View Code
ParameterCode.cs
namespace Common
 {
     // parameter type distinguishing data transmission 
     public  enum ParameterCode: byte
     {
         UserName,
         Password
     }
 }
View Code
 
Third, to achieve registration and login, two requests. Receives the service request is mainly in the distal OnOperationRequest ClientPeer.cs file () function. If the requested increases in the future, this piece of code that will be more and more. So now sort out the code, using a modular distinguish each request.

  Use Handler distinguish each request, so that their own request processing logic. Handler basic definitions OnOperationRequest () function, other Handler inherit this BaseHandler.

 

Introducing public class Common:

  

 

 

 

 

New directories and files. BaseHandler parent, LoginHandler and RegisterHandler were treated login and registration, DefaultHandler default processing the request.

 

 

 

  (1), BaseHandler.cs, all other handler classes inherit this class.

using Common;
using Photon.SocketServer;

namespace MyGameServer.Hander
{
    public  abstract  class BaseHandler
    { 
     // distinguish the request interface
public OperationCode OpCode; public abstract void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters, ClientPeer peer); } }

   (2)、DefaultHandler.cs

using Common;

using Photon.SocketServer;

namespace MyGameServer.Hander
{
    public class DefaultHandler:BaseHandler
    {
        public DefaultHandler()
        {
            OpCode = OperationCode.Default;
        }

        public override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters, ClientPeer peer)
        {
           
        }
    }
}

    (3)、LoginHandler.cs

using System;
using Common;
using Common.Toos;
using MyGameServer.Manager;
using Photon.SocketServer;

namespace MyGameServer.Hander
{
    public class LoginHander:BaseHandler
    {
        public LoginHander()
        {
            OpCode = OperationCode.Login;
        }

        public override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters, ClientPeer peer)
        {
           
        }
    }
}

   (4)、RegisterHandler.cs

using Common;
using Common.Toos;
using MyGameServer.Manager;
using MyGameServer.Model;
using Photon.SocketServer;

namespace MyGameServer.Hander
{
    public class RegisterHandler:BaseHandler
    {
        public RegisterHandler()
        {
            OpCode = OperationCode.Register;
        }

        public override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters, ClientPeer peer)
        {
            
        }
    }
}

 

四、注册Handler。添加Handler后,我们需要使用方便,得在主函数中进行注册。

修改MyGameSerer.cs文件:

  (1)、添加一个变量,用于存放Handler

    public Dictionary<OperationCode,BaseHandler> HandlerDict = new Dictionary<OperationCode, BaseHandler>();

       (2)、添加MyGameServer管理单例类。

 public new static MyGameServer Instance { get; private set; }

      (3)、注册Handler,在Setup()函数中添加

 Instance = this;
 InitHandler();

      (4)、添加Handler,新建函数 InitHandler()

       private void InitHandler()
        {
            LoginHander loginHander = new LoginHander();
            HandlerDict.Add(loginHander.OpCode,loginHander);
            
            DefaultHandler defaultHandler = new DefaultHandler();
            HandlerDict.Add(defaultHandler.OpCode,defaultHandler);
            
            RegisterHandler registerHandler = new RegisterHandler();
            HandlerDict.Add(registerHandler.OpCode,registerHandler);
        }

     (5)、完整代码

using System.Collections.Generic;
using System.IO;
using Common;
using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using log4net;
using log4net.Config;
using MyGameServer.Hander;
using MyGameServer.Manager;
using MyGameServer.Model;
using Photon.SocketServer;
using LogManager = ExitGames.Logging.LogManager;

namespace MyGameServer
{
    //继承 ApplicationBase
    public class MyGameServer : ApplicationBase
    {
        //日志打印
        public static readonly ILogger log = LogManager.GetCurrentClassLogger();

        public new static MyGameServer Instance { get; private set; }
        
        public Dictionary<OperationCode,BaseHandler> HandlerDict = new Dictionary<OperationCode, BaseHandler>();
                        
                                
        //客户端创建链接
        //使用一个peerbase表示一个客户端连接
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {
            log.Info("Client  Connect----------");
            //创建一个客户端返回给引擎,引擎自动管理
            return new ClientPeer(initRequest);
        }

        //服务器启动时调用
        protected override void Setup()
        {
            LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
            GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(ApplicationRootPath, "log");
            GlobalContext.Properties["LogFileName"] = "MySer_" + ApplicationName;
            XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(BinaryPath, "log4net.config")));
            
            log.Info("MyGameServer start----------");

            
            Instance = this;
            InitHandler();
        }

        private void InitHandler()
        {
            LoginHander loginHander = new LoginHander();
            HandlerDict.Add(loginHander.OpCode,loginHander);
            
            DefaultHandler defaultHandler = new DefaultHandler();
            HandlerDict.Add(defaultHandler.OpCode,defaultHandler);
            
            RegisterHandler registerHandler = new RegisterHandler();
            HandlerDict.Add(registerHandler.OpCode,registerHandler);
        }

        //服务器停止调用
        protected override void TearDown()
        {
            log.Info("MyGameServer down----------");
        }
    }
}
View Code

 

五、修改文件ClientPeer.cs,分发请求到每个Handler中取。根据客户端传来的code判断是哪个请求,找到相应的Handler,然后将上传的参数分发给该Handler进行逻辑处理。

using System.Collections.Generic;
using Common;
using Common.Toos;
using MyGameServer.Hander;
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;

namespace MyGameServer
{
    
    public class ClientPeer : Photon.SocketServer.ClientPeer
    {
        //创建客户端
        public ClientPeer(InitRequest initRequest) : base(initRequest)
        {
        }

        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            MyGameServer.log.Info("Client---请求了---" + operationRequest.OperationCode);

            //首先获取客户端传来的code (operationRequest.OperationCode)
            //然后根据code 去 MyGameServer中获取注册的Handler。
            //Handler我们注册到了主函数HandlerDict中。
            //DictTool工具类是我们自己定义的,方便传入key,就能从Dict中取值,这里取出的是code相对应的handler
            
            BaseHandler handler = DictTool.GetValue<OperationCode, BaseHandler>(MyGameServer.Instance.HandlerDict,
                (OperationCode) operationRequest.OperationCode);

            if (handler != null)
            {
                //找到相应的Handler,直接调用 OnOperationRequest 进行相应逻辑处理
                handler.OnOperationRequest(operationRequest,sendParameters,this);
            }
            else
            {
                //如果没有找到,返回我们自定义的 DefaultHandler.
                BaseHandler defHander = DictTool.GetValue<OperationCode, BaseHandler>(MyGameServer.Instance.HandlerDict,
                    OperationCode.Default);
                
                defHander.OnOperationRequest(operationRequest,sendParameters,this);
            }
            
        }

        //处理客户端断开连接
        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
        {
            MyGameServer.log.Info("Client------断开了");
        }
    }
}

 

到此,服务端代码整理完成。下面进行前端处理。

 

Guess you like

Origin www.cnblogs.com/cj8988/p/11690891.html