Photon 分布式服务器架设-创建数据传入子服务器(二)

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

欢迎回到第2部分的Server2Server
在第1部分中我们做NodeResolverBase,我们称之为主服务器。现在我们需要一种方法来与MasterServer通信, 所以我们要创建一个新类称为IncomingSubServerPeer。 所以创建这个类并实现成员,但是我们会有一个稍微不同的构造函数。我们还将创建一个链接到MasterServer和我们的日志能够添加日志当我们需要的时候。

[csharp] view plaincopy
 
  1. class IncomingSubServerPeer : ServerPeerBase  
  2.     {  
  3.         private MasterServer _server;  
  4.         private static readonly ILogger Log = LogManager.GetCurrentClassLogger();  
  5.    
  6.         public IncomingSubServerPeer(InitRequest request, MasterServer server) : base(request.Protocol, request.PhotonPeer)  
  7.         {  
  8.             _server = server;  
  9.             if(Log.IsDebugEnabled)  
  10.             {  
  11.                 Log.DebugFormat("SubServer connected from {0}:{1} - connection id: {2}", RemoteIP, RemotePort, ConnectionId);  
  12.             }  
  13.         }  
  14.   
  15.         #region Overrides of PeerBase  
  16.    
  17.         protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)  
  18.         {  
  19.             throw new NotImplementedException();  
  20.         }  
  21.    
  22.         protected override void OnDisconnect()  
  23.         {  
  24.             throw new NotImplementedException();  
  25.         }  
  26.   
  27.         #endregion  
  28.   
  29.         #region Overrides of ServerPeerBase  
  30.    
  31.         protected override void OnEvent(IEventData eventData, SendParameters sendParameters)  
  32.         {  
  33.             throw new NotImplementedException();  
  34.         }  
  35.    
  36.         protected override void OnOperationResponse(OperationResponse operationResponse, SendParameters sendParameters)  
  37.         {  
  38.             throw new NotImplementedException();  
  39.         }  
  40.   
  41.         #endregion  
  42.     }  


在这样的情况下,当我们收到一个OperationRequest的时候我们要写一个Log

[csharp] view plaincopy
 
  1. protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)  
  2.         {  
  3.             if(Log.IsDebugEnabled)  
  4.             {  
  5.                 Log.DebugFormat("Received operation request from {0} - op code {1}", ConnectionId, operationRequest.OperationCode);  
  6.             }  
  7.         }  


当服务器断开连接也写一个日志。

[csharp] view plaincopy
 
  1. protected override void OnDisconnect()  
  2.         {  
  3.             if(Log.IsDebugEnabled)  
  4.             {  
  5.                 Log.DebugFormat("Connection from Sub Server {0} terminated", ConnectionId);  
  6.             }  
  7.         }  


当服务器连接上的时候,我们有MasterServer.CreatePeer()

[csharp] view plaincopy
 
  1. protected override PeerBase CreatePeer(InitRequest initRequest)  
  2.         {  
  3.             if(IsSubServerPeer(initRequest))  
  4.             {  
  5.                 if(Log.IsDebugEnabled)  
  6.                 {  
  7.                     Log.DebugFormat("Received init request from sub server");  
  8.                 }  
  9.                 return new IncomingSubServerPeer(initRequest, this);  
  10.             }  
  11.             return null;  
  12.         }  

分享到: 微信 更多