RPC.Peer odd behaviour

Options
Sergiy
Sergiy
edited June 2012 in Photon Server
hi

Consider following code:
public class TestPeer : Peer, IOperationHandler
    {
        
        private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();

        public TestPeer (IRpcProtocol rpcProtocol, IPhotonPeer photonPeer)
            : base(rpcProtocol, photonPeer)
        {
            SetCurrentOperationHandler(this);
            Logger.Debug(string.Format("ConnectionID: {0} Set operation handler to this", ConnectionId));
        }

        public void OnDisconnect(PeerBase peer)
        {
            //throw new NotImplementedException();
        }

        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
        {
            Logger.Debug(string.Format("ConnectionID: {0} disconnected from lobby", ConnectionId));
            base.OnDisconnect(reasonCode, reasonDetail);
        }        

       public OperationResponse OnOperationRequest(PeerBase peer, OperationRequest operationRequest, SendParameters sendParameters)
        {
            switch ((OperationCodes) operationRequest.OperationCode)
            {
                case OperationCodes.JoinLobby:
                    switch (LocalPort)
                    {
                        case 21311:
                            TestJoinLobby testJoinLobby = new TestJoinLobby(Protocol, operationRequest);
                            if (testJoinLobby.IsValid)
                            {
                                Logger.Debug(string.Format("{0}, ConnectionID: {1}", "JoinLobby", ConnectionId));
                                SetCurrentOperationHandler(new TestOperationHandler());
                                return testJoinLobby.GetOperationResponse(ErrorCodes.NoError);
                            }
                            Logger.Error(string.Format("Error: {0}, ConnectionID: {1}", "Invalid Session", ConnectionId));
                            return testJoinLobby.GetOperationResponse(ErrorCodes.InvalidSession);
                    }
                    break;
            }
            return new OperationResponse
                       {
                           OperationCode = operationRequest.OperationCode,
                           ReturnCode = (short) ErrorCodes.InvalidOperation
                       };
        }

and this one:
class TestOperationHandler: IOperationHandler
    {
        private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();

        public void OnDisconnect(PeerBase peer)
        {
            Logger.Debug(string.Format("ConnectionID: {0} disconnected from GameOperationHandler", peer.ConnectionId));
        }
}

So, TestClient calls Connect(), then JoinLobby and lastly, Disconnect

Output in log is as follows:
2012-06-01 22:34:58,185 [24] DEBUG Photon.SocketServer.ApplicationBase [(null)] - OnInit - ConnID=11, IP 127.0.0.1 on port 21311
2012-06-01 22:34:58,186 [24] DEBUG Photon.SocketServer.Protocol [(null)] - Parsed init message for application Test.Server, client version 3.0.1, protocol GpBinaryV2 version 1.6
2012-06-01 22:34:58,187 [24] DEBUG Photon.SocketServer.Rpc.Peer [(null)] - set operation handler to Photon.SocketServer.Rpc.OperationHandlerDisabled, was {null} - peer id 11
2012-06-01 22:34:58,187 [24] DEBUG Photon.SocketServer.Rpc.Peer [(null)] - set operation handler to Test.Server.TestPeer, was Photon.SocketServer.Rpc.OperationHandlerDisabled - peer id 11
2012-06-01 22:34:58,187 [24] DEBUG Test.Server.TestPeer [(null)] - ConnectionID: 11 Set operation handler to this
2012-06-01 22:34:58,187 [24] DEBUG Photon.SocketServer.ApplicationBase [(null)] - OnInit - response sent to ConnId 11 with SendResult Ok
2012-06-01 22:35:04,763 [21] DEBUG PhotonHostRuntime.PhotonDomainManager [(null)] - AssemblyLoadEvent: Test.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
2012-06-01 22:35:04,785 [21] DEBUG Test.Server.TestPeer [(null)] - JoinLobby, ConnectionID: 11
2012-06-01 22:35:04,785 [21] DEBUG Photon.SocketServer.Rpc.Peer [(null)] - set operation handler to Test.Server.OperationHandlers.TestOperationHandler, was Test.Server.TestPeer - peer id 11
2012-06-01 22:35:04,794 [21] DEBUG Photon.SocketServer.PeerBase [(null)] - SentOpResponse: ConnID=11, opCode=1, return=0, ChannelId=0 result=Ok size=8 bytes
2012-06-01 22:35:26,002 [24] DEBUG Photon.SocketServer.ApplicationBase [(null)] - OnDisconnect - ConnID=11
2012-06-01 22:35:26,004 [18] DEBUG Photon.SocketServer.PeerBase [(null)] - Peer 11 changed state from Connected to Disconnected
2012-06-01 22:35:26,004 [18] DEBUG Test.Server.TestPeer [(null)] - ConnectionID: 11 disconnected from lobby
2012-06-01 22:35:26,005 [18] DEBUG Test.Server.OperationHandlers.TestOperationHandler [(null)] - ConnectionID: 11 disconnected from GameOperationHandler
2012-06-01 22:35:26,005 [18] DEBUG Photon.SocketServer.PeerBase [(null)] - Peer 11 changed state from Disconnected to Disposed

Questions:
1. why did TestPeer.OnDisconnect() fired before TestOperationHandler.OnDisconnect() ?
2. why did TestPeer.OnDisconnect() fired at all? OperationHandler was TestOperationHandler.

Sorry for long post, but it's easier to show code than to explain :)
Thanks

Comments

  • Philip
    Options
    As I explained in viewtopic.php?f=5&t=1702 the code for OnDisconnect(...) of the RPC.Peer is:

    protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
    {
    this.CurrentOperationHandler.OnDisconnect(this);
    }

    Answers:
    1. because "your" code is calling base.OnDisconnect(reasonCode, reasonDetail) and "our" code calls the current OperationHandler.
    2.
    - the framework calls the peer OnDisconnect(reasonCode, reasonDetail)
    - the RPC.Peer is an implementation of peer that implements OnDisconnect(reasonCode, reasonDetail) which calls the current OperationHandler.OnDisconnect
  • Sergiy
    Options
    So, no matter who is current operation handler, framework always calls RPC.Peer.OnDisconnect() first and only after that - current operation handler's OnDisconnect(). Is it correct?
  • Philip
    Options
    Sergiy wrote:
    So, no matter who is current operation handler, framework always calls RPC.Peer.OnDisconnect() first and only after that - current operation handler's OnDisconnect(). Is it correct?

    well yes - when inheriting from RPC.Peer. The framework calls "Peer".Ondisconnect first where "Peer" is any peer that inherits from Photon.SocketServer.PeerBase, which is the case with RPC.Peer. Then RPC.Peer.OnDisconnect calls the current operationhandler. Photon.SocketServer.PeerBase doesn't.