Photon 分布式服务器架设-创建数据传入子服务器(二)
转自:http://blog.csdn.net/huihuilaopo/article/details/8740394
欢迎回到第2部分的Server2Server
在第1部分中我们做NodeResolverBase,我们称之为主服务器。现在我们需要一种方法来与MasterServer通信, 所以我们要创建一个新类称为IncomingSubServerPeer。 所以创建这个类并实现成员,但是我们会有一个稍微不同的构造函数。我们还将创建一个链接到MasterServer和我们的日志能够添加日志当我们需要的时候。
- class IncomingSubServerPeer : ServerPeerBase
- {
- private MasterServer _server;
- private static readonly ILogger Log = LogManager.GetCurrentClassLogger();
- public IncomingSubServerPeer(InitRequest request, MasterServer server) : base(request.Protocol, request.PhotonPeer)
- {
- _server = server;
- if(Log.IsDebugEnabled)
- {
- Log.DebugFormat("SubServer connected from {0}:{1} - connection id: {2}", RemoteIP, RemotePort, ConnectionId);
- }
- }
- #region Overrides of PeerBase
- protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
- {
- throw new NotImplementedException();
- }
- protected override void OnDisconnect()
- {
- throw new NotImplementedException();
- }
- #endregion
- #region Overrides of ServerPeerBase
- protected override void OnEvent(IEventData eventData, SendParameters sendParameters)
- {
- throw new NotImplementedException();
- }
- protected override void OnOperationResponse(OperationResponse operationResponse, SendParameters sendParameters)
- {
- throw new NotImplementedException();
- }
- #endregion
- }
在这样的情况下,当我们收到一个OperationRequest的时候我们要写一个Log
- protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
- {
- if(Log.IsDebugEnabled)
- {
- Log.DebugFormat("Received operation request from {0} - op code {1}", ConnectionId, operationRequest.OperationCode);
- }
- }
当服务器断开连接也写一个日志。
- protected override void OnDisconnect()
- {
- if(Log.IsDebugEnabled)
- {
- Log.DebugFormat("Connection from Sub Server {0} terminated", ConnectionId);
- }
- }
当服务器连接上的时候,我们有MasterServer.CreatePeer()
- protected override PeerBase CreatePeer(InitRequest initRequest)
- {
- if(IsSubServerPeer(initRequest))
- {
- if(Log.IsDebugEnabled)
- {
- Log.DebugFormat("Received init request from sub server");
- }
- return new IncomingSubServerPeer(initRequest, this);
- }
- return null;
- }