现在的位置: 首页 > 综合 > 正文

Photon分布式服务器架设-创建主服务器(一)

2019年07月22日 ⁄ 综合 ⁄ 共 5996字 ⁄ 字号 评论关闭

Exit Games' 推出Photon 3.0和特别是ServerToServer API,我决定看一看这是多么容易在一个服务器集群上开始工作。原来,这一点也不难,如果你花时间去了解。用少量的类和一个框架可以创建一个服务器集群在同一台机器上或在多个机器。设置我们将开始构建今天将很容易扩展到更大的系统,可以让我们构建一个健壮的功能集,以确保如果服务器崩溃,另一个可以代替它运行。然而,一切,都有一个开始。
简介
今天我想开始看主服务器。背后的想法是,它是主服务器第一个服务器上来,所有的子服务器将连接到它。这将允许客户端连接和请求状态以及通过消息从客户端到服务器并返回的子子服务器响应返回到客户机。基本上它需要尽快,因为它将路由数据包通过自身的许多球员和子服务器。
我们要怎么做呢?
在本文第一部分我们将只能构建块,关系到PhotonControl,Exit Games'的NodeResolverBase调用。 是一个类的NodeResolverBase Exit Games'设计的专门处理节点连接到服务器。这将是我们的发射点。它将检查连接建立子服务器或客户端和处理相应的。
目标
建立一个类继承自NodeResolverBase的主服务器
设置日志
添加日志语句
测试连接是否子服务器
修改PhotonControl.config

你需要有安装Photon但还不运行。
建立一个类继承自NodeResolverBase的主服务器

我们将首先创建一个新的解决方案,有一个类库项目。命名为Server2Server。 当解决方案完成创建,重命名文件MasterServer 的Class1和打开它。
让开始通过类继承NodeResolverBase并添加实现类的成员和移除NotImplementedExceptionThrow:

public class MasterServer : NodeResolverBase
    {
        #region Overrides of ApplicationBase
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {
        }
        protected override void Setup()
        {
        }
        protected override void TearDown()
        {
        }
        #endregion
        #region Overrides of NodeResolverBase
        protected override void OnNodeConnected(byte nodeId, int port)
        {
        }
        protected override void OnNodeDisconnected(byte nodeId, int port)
        {
        }
        #endregion
    }

设置日志
接下来,我们开始想用日志记录从一开始。我们这样做是为了记录正在发生的事情在我们的代码运行时,而不是启动服务器作为一个应用程序和调试。
我们开始通过添加ILogger实例:

using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using log4net;
using log4net.Config;
using LogManager = ExitGames.Logging.LogManager;
public class MasterServer : NodeResolverBase
{
    private static readonly ILogger Log = LogManager.GetCurrentClassLogger();
    protected override void Setup()
    {
        LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
        GlobalContext.Properties["LogFileName"] = "MS" + ApplicationName;
        XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(BinaryPath, "log4net.config")));
    }

找到你的Photon安装目录,然后srcserver/ LoadBalancing / LoadBalancing,拖动log4net.config到您的项目。记得打开属性并设置复制到输出目录复制。你也会想打开它并设置log4net isDebug为true,根日志级别设为DEBUG,和删除日志记录器名为 Photon.LoadBalancing.LoadShedding.FeedbackController
添加日志语句
接下来,我们想继续添加日志记录语句的功能OnNodeConnected和OnNodeDisconnected:
 

public class MasterServer : NodeResolverBase
{
    protected override void OnNodeConnected(byte nodeId, int port)
    {
        if(Log.IsDebugEnabled)
        {
            Log.DebugFormat("Node {0} connected from port {1}", nodeId, port);
        }
    }
    protected override void OnNodeDisconnected(byte nodeId, int port)
    {
        if(Log.IsDebugEnabled)
        {
            Log.DebugFormat("Node {0} disconnected from port {1}", nodeId, port);
        }
    }
}

测试连接是否子服务器
下一件事在我们的列表中添加代码来检查传入的连接子服务器。首先,我们会想要创建一个新的名为MasterServerSettings.settings设置文件。 当你打开这个文件,您将看到4列,一个名字、类型、范围和价值。我们想添加一行IncomingSubServerPort名称,类型的整数,范围的用户,和一个值4520。
一旦文件准备就绪MasterServer返回。 cs文件,我们将会添加一些代码到CreatePeer函数。
测试连接是否子服务器
下一件事在我们的列表中添加代码来检查传入的连接子服务器。首先,我们会想要创建一个新的名为MasterServerSettings.settings设置文件。 当你打开这个文件,您将看到4列,一个名字、类型、范围和价值。我们想添加一行IncomingSubServerPort,一个int 型的数,user,和一个value = 4520。
一旦文件准备就绪MasterServer.cs文件,我们将会添加一些代码到CreatePeer函数。

public class MasterServer : NodeResolverBase
{
    protected override PeerBase CreatePeer(InitRequest initRequest)
    {
        if(IsSubServerPeer(initRequest))
        {
            if(Log.IsDebugEnabled)
            {
                Log.DebugFormat("Received init request from sub server");
            }
        }
        return null;
    }
    protected virtual bool IsSubServerPeer(InitRequest initRequest)
    {
        return initRequest.LocalPort == MasterServerSettings.Default.IncomingSubServerPort;
    }
}

修改PhotonControl.config中的代码,最后我们需要做的是修改PhotonControl。配置文件。所以打开你使用哪个文件夹并打开PhotonControl。配置文件。
我们要想把下面的InstanceLoadBalancing:

<InstanceLoadBalancing
  EnablePerformanceCounters = "true"
  DataSendingDelayMilliseconds="50"
  AckSendingDelayMilliseconds="50"
  PerPeerMaxReliableDataInTransit="16384"
  PerPeerTransmitRateLimitKBSec="128"
  MaxQueuedDataPerPeer="65536"
  MinimumTimeout="5000"
  MaximumTimeout="10000">
  <IOPool/>
  <ThreadPool
   InitialThreads="4"
   MinThreads="4"
   MaxThreads="4">
  </ThreadPool>
  <!-- Using the same value for initial, min and max makes the pool fixed size, which allows optimizations. -->
  <ENetThreadPool
   InitialThreads="2"
   MinThreads="2"
   MaxThreads="2">
  </ENetThreadPool>
  <!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->
  <!-- Port 5055 is Photon's default for UDP connections. -->
  <UDPListeners>
   <UDPListener
    IPAddress="0.0.0.0"
    Port="5055"
    OverrideApplication="Master">
   </UDPListener>
  </UDPListeners>
  
  <!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->
  <!-- Port 5055 is Photon's default for TCP connecttions. -->
  <TCPListeners>
   <!-- TCP listener for Game clients on Master application -->
   <TCPListener
    IPAddress="0.0.0.0"
    Port="4530"
    OverrideApplication="Master"
    DisableNagle="true"
    InactivityTimeout="0">
   </TCPListener>
   
   <!-- DON'T EDIT THIS. TCP listener for GameServers on Master application -->
   <TCPListener
    IPAddress="0.0.0.0"
    Port="4520"
    DisableNagle="true"
    InactivityTimeout="0">
   </TCPListener>
  </TCPListeners>
  
  <!-- Policy request listener for Unity and Flash (port 843) and Silverlight (port 943)  -->
  <TCPPolicyListeners>
   <!-- multiple Listeners allowed for different ports -->
   <TCPPolicyListener
    IPAddress="0.0.0.0"
    Port="843"
    Application="Policy"
    InactivityTimeout="5000">
   </TCPPolicyListener>
  </TCPPolicyListeners>
  <!-- Defines the Photon Runtime Assembly to use. -->
  <Runtime
   Assembly="PhotonHostRuntime, Culture=neutral"
   Type="PhotonHostRuntime.PhotonDomainManager"
   UnhandledExceptionPolicy="Ignore">
  </Runtime>
  <!-- Defines which applications are loaded on start and which of them is used by default. Make sure the default application is defined. -->
  <!-- Application-folders must be located in the same folder as the bin_win32 folders. The BaseDirectory must include a "bin" folder. -->
  <Applications Default="Master">  
   <Application
    Name="Master"
    BaseDirectory="Server2Server"
    Assembly="Server2Server"
    Type="Server2Server.MasterServer">
   </Application>
   <Application
    Name="Policy"
    BaseDirectory="Policy"
    Assembly="Policy.Application"
    Type="Exitgames.Realtime.Policy.Application.Policy"
    EnableAutoRestart="true"
    WatchFiles="dll;config;xml"
    ExcludeFiles="log4net.config">
   </Application>
  </Applications>
 </InstanceLoadBalancing>

我们只要将主服务器上市。后来当我们有子服务器我们会扩大这个包含所有的子服务器和端口用来监听。导入xml是TcpListener在端口4520上,是什么使得NodeResolverBase特殊? 它有自己的港口它监听子服务器连接,与其他应用程序只有一个连接端口。

总结
所以这里我们已经创建了一个新的解决方案与一个类库,建立我们的主服务器类的过程,开始接受子服务器连接。我们实现了日志和设置PhotonControl.config文件。在这一点上你可以编译代码,把输出到 Photon/deploy/Server2Server/bin目录并启动服务器。它不做任何事情在这个时间,但是我们可以证实我们迄今启动和不关闭Photon InstanceLoadBalancing服务。

原文链接 :Photon Server2Server - Creating the Master Server

 

抱歉!评论已关闭.