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

转自:http://blog.csdn.net/huihuilaopo/article/details/8740261

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

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

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

[csharp] view plaincopy
 
  1. public class MasterServer : NodeResolverBase  
  2.     {  
  3.         #region Overrides of ApplicationBase  
  4.         protected override PeerBase CreatePeer(InitRequest initRequest)  
  5.         {  
  6.         }  
  7.         protected override void Setup()  
  8.         {  
  9.         }  
  10.         protected override void TearDown()  
  11.         {  
  12.         }  
  13.         #endregion  
  14.         #region Overrides of NodeResolverBase  
  15.         protected override void OnNodeConnected(byte nodeId, int port)  
  16.         {  
  17.         }  
  18.         protected override void OnNodeDisconnected(byte nodeId, int port)  
  19.         {  
  20.         }  
  21.         #endregion  
  22.     }  


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

[csharp] view plaincopy
 
  1. using ExitGames.Logging;  
  2. using ExitGames.Logging.Log4Net;  
  3. using log4net;  
  4. using log4net.Config;  
  5. using LogManager = ExitGames.Logging.LogManager;  
  6. public class MasterServer : NodeResolverBase  
  7. {  
  8.     private static readonly ILogger Log = LogManager.GetCurrentClassLogger();  
  9.     protected override void Setup()  
  10.     {  
  11.         LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);  
  12.         GlobalContext.Properties["LogFileName"] = "MS" + ApplicationName;  
  13.         XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(BinaryPath, "log4net.config")));  
  14.     }  


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

[csharp] view plaincopy
 
  1. public class MasterServer : NodeResolverBase  
  2. {  
  3.     protected override void OnNodeConnected(byte nodeId, int port)  
  4.     {  
  5.         if(Log.IsDebugEnabled)  
  6.         {  
  7.             Log.DebugFormat("Node {0} connected from port {1}", nodeId, port);  
  8.         }  
  9.     }  
  10.     protected override void OnNodeDisconnected(byte nodeId, int port)  
  11.     {  
  12.         if(Log.IsDebugEnabled)  
  13.         {  
  14.             Log.DebugFormat("Node {0} disconnected from port {1}", nodeId, port);  
  15.         }  
  16.     }  
  17. }  


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

[csharp] view plaincopy
 
  1. public class MasterServer : NodeResolverBase  
  2. {  
  3.     protected override PeerBase CreatePeer(InitRequest initRequest)  
  4.     {  
  5.         if(IsSubServerPeer(initRequest))  
  6.         {  
  7.             if(Log.IsDebugEnabled)  
  8.             {  
  9.                 Log.DebugFormat("Received init request from sub server");  
  10.             }  
  11.         }  
  12.         return null;  
  13.     }  
  14.     protected virtual bool IsSubServerPeer(InitRequest initRequest)  
  15.     {  
  16.         return initRequest.LocalPort == MasterServerSettings.Default.IncomingSubServerPort;  
  17.     }  
  18. }  


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

[html] view plaincopy
 
  1. <InstanceLoadBalancing  
  2.   EnablePerformanceCounters = "true"  
  3.   DataSendingDelayMilliseconds="50"  
  4.   AckSendingDelayMilliseconds="50"  
  5.   PerPeerMaxReliableDataInTransit="16384"  
  6.   PerPeerTransmitRateLimitKBSec="128"  
  7.   MaxQueuedDataPerPeer="65536"  
  8.   MinimumTimeout="5000"  
  9.   MaximumTimeout="10000">  
  10.   <IOPool/>  
  11.   <ThreadPool  
  12.    InitialThreads="4"  
  13.    MinThreads="4"  
  14.    MaxThreads="4">  
  15.   </ThreadPool>  
  16.   <!-- Using the same value for initial, min and max makes the pool fixed size, which allows optimizations. -->  
  17.   <ENetThreadPool  
  18.    InitialThreads="2"  
  19.    MinThreads="2"  
  20.    MaxThreads="2">  
  21.   </ENetThreadPool>  
  22.   <!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->  
  23.   <!-- Port 5055 is Photon's default for UDP connections. -->  
  24.   <UDPListeners>  
  25.    <UDPListener  
  26.     IPAddress="0.0.0.0"  
  27.     Port="5055"  
  28.     OverrideApplication="Master">  
  29.    </UDPListener>  
  30.   </UDPListeners>  
  31.     
  32.   <!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->  
  33.   <!-- Port 5055 is Photon's default for TCP connecttions. -->  
  34.   <TCPListeners>  
  35.    <!-- TCP listener for Game clients on Master application -->  
  36.    <TCPListener  
  37.     IPAddress="0.0.0.0"  
  38.     Port="4530"  
  39.     OverrideApplication="Master"  
  40.     DisableNagle="true"  
  41.     InactivityTimeout="0">  
  42.    </TCPListener>  
  43.      
  44.    <!-- DON'T EDIT THIS. TCP listener for GameServers on Master application -->  
  45.    <TCPListener  
  46.     IPAddress="0.0.0.0"  
  47.     Port="4520"  
  48.     DisableNagle="true"  
  49.     InactivityTimeout="0">  
  50.    </TCPListener>  
  51.   </TCPListeners>  
  52.     
  53.   <!-- Policy request listener for Unity and Flash (port 843) and Silverlight (port 943)  -->  
  54.   <TCPPolicyListeners>  
  55.    <!-- multiple Listeners allowed for different ports -->  
  56.    <TCPPolicyListener  
  57.     IPAddress="0.0.0.0"  
  58.     Port="843"  
  59.     Application="Policy"  
  60.     InactivityTimeout="5000">  
  61.    </TCPPolicyListener>  
  62.   </TCPPolicyListeners>  
  63.   <!-- Defines the Photon Runtime Assembly to use. -->  
  64.   <Runtime  
  65.    Assembly="PhotonHostRuntime, Culture=neutral"  
  66.    Type="PhotonHostRuntime.PhotonDomainManager"  
  67.    UnhandledExceptionPolicy="Ignore">  
  68.   </Runtime>  
  69.   <!-- Defines which applications are loaded on start and which of them is used by default. Make sure the default application is defined. -->  
  70.   <!-- Application-folders must be located in the same folder as the bin_win32 folders. The BaseDirectory must include a "bin" folder. -->  
  71.   <Applications Default="Master">    
  72.    <Application  
  73.     Name="Master"  
  74.     BaseDirectory="Server2Server"  
  75.     Assembly="Server2Server"  
  76.     Type="Server2Server.MasterServer">  
  77.    </Application>  
  78.    <Application  
  79.     Name="Policy"  
  80.     BaseDirectory="Policy"  
  81.     Assembly="Policy.Application"  
  82.     Type="Exitgames.Realtime.Policy.Application.Policy"  
  83.     EnableAutoRestart="true"  
  84.     WatchFiles="dll;config;xml"  
  85.     ExcludeFiles="log4net.config">  
  86.    </Application>  
  87.   </Applications>  
  88.  </InstanceLoadBalancing>  


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

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


分享到: 微信 更多