How can I derive from PhotonPeer to create my own implementation of a Peer?

rafael_at_jags
edited December 2016 in Photon Server
I would like to do so to add new operations.

I have made a simple Peer class:
public class MyPeer : PhotonPeer
    {
        public MyPeer(ConnectionProtocol protocolType) : base(protocolType) {}
        public MyPeer(IPhotonPeerListener listener, ConnectionProtocol protocolType) : base(listener, protocolType) {}

        public bool OpExecuteAction(string param)
        {
            var dic = new Dictionary<byte, object>
            {
                {1, param},
            };
            return OpCustom(100, dic, true);
        }
    }
And this is how I am using it:
 _myPeer = new MyPeer(this, PhotonNetwork.PhotonServerSettings.Protocol);
_myPeer.Connect("myPublicIp:Port#", null);
But my Peer is always in Connecting state so I am unable to call the OpExecuteAction method.

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    edited December 2016
    Hi @rafael_at_jags,

    First of all ask yourself if you really need a new operation or you can make use of Photon events instead.

    If you need to add operations then you should:
    - Implement them server side, either start your own Photon Server application from scratch or extend/modify LoadBalancing applications.
    - If you choose to work on LoadBalancing application server side, you can modify/extend LoadBalancingClient to add those operations on client side and no need to extend PhotonPeer and re invent the wheel.
    - You may not need PUN at all. If you do, you should also update PhotonNetwork accordingly. Otherwise, you should use Photon Unity SDK if you need Unity or .Net SDK.

    EDIT: I moved the discussion to Photon Server category.
    I guess you followed a documentation page which is quite old referring to Lite and LiteLobby applications that were replaced with LoadBalancing. I will make sure to update that doc.

    To answer your specific question I need to know if there were some logs or an error or an exception. If nothing happens, then probably you did not call Service() regularly meaning the client did not communicate w/ the Server at all as the update loop where the messages are received and dispatched was not called.
  • rafael_at_jags
    edited December 2016
    Hi @JohnTube

    I really need to add a new operation but adding operations is not the problem.
    I already added the new operation on my server and managed to get it working but I got it by modifying PUN by adding the operation in the NetworkingPeer and PhotonNetwork classes so I can access it just like all the other operations. I think it's a bad idea because when PUN gets updated, I will lose my changes, so I'd like to create my own Peer implementation with my new operations.

    I'm not getting any errors when calling Connect(). I do, however, get an error when calling my Peer's custom "Op" method that makes use of PeerBase OpCustom. The error is that my Peer is connecting so the operation cannot be sent:
    DebugReturn Cannot send op: 100 Not connected. PeerState: Connecting
    UnityEngine.Debug:Log(Object)
    Test:DebugReturn(DebugLevel, String) (at Assets/Test.cs:37)
    ExitGames.Client.Photon.EnetPeer:EnqueueOperation(Dictionary`2, Byte, Boolean, Byte, Boolean, EgMessageType)
    ExitGames.Client.Photon.PeerBase:EnqueueOperation(Dictionary`2, Byte, Boolean, Byte, Boolean)
    ExitGames.Client.Photon.PhotonPeer:OpCustom(Byte, Dictionary`2, Boolean, Byte)
    ExitGames.Client.Photon.PhotonPeer:OpCustom(Byte, Dictionary`2, Boolean)
    MyPeer:OpExecuteAction(String) (at Assets/Test.cs:219)
    Test:Update() (at Assets/Test.cs:26)
    The problem is my Peer is always connecting, no matter how long I wait.
  • Still trying to figure out why my Peer implementation isn't connecting properly.
  • it looks like you create client side peer, not server side.

    for client side peers you are bound to call method Service(). if you do not do this, nothing will work. you should call it often enought but not oftent then 50 times per second. you may call even 100 times pers second, but IMO this does not make any sense
  • Thank you. I ended up calling Service() in the Update loop and that seems like it fixed my problem. :)
  • I noticed a new problem: it doesn't seem like the GameClientPeer class from the LoadBalancing project is working with my custom peer implementation. Basically the OnOperationRequest method doesn't seem to be called when I call OpCustom from my custom peer.
  • well, it should work. it is not clear what you managed to break
  • On the client side, I create a new peer that inherits from PhotonPeer.
    Sending operation works fine, but on the server's GameClientPeer class, the method OnOperationRequest does not seem to be invoked when an operation is sent from my custom peer; only when an operation is sent from PhotonNetwork.networkingPeer.
  • I'm late to the party :)

    I would not extend PhotonPeer. It's not the class that provides operations anyways.

    The LoadBalancingPeer is providing operations, so you could extend that or do what that peer does: Use PhotonPeer.OpCustom() to pass in your own operation-parameters. The latter can be done in any class.

    If you don't mess with the inheritance, you can keep the states which PhotonNetwork and the NetworkingPeer provide. We can't provide a lot of help for custom network/game states.
  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @rafael_at_jags,

    As @Tobias mentioned you could call PhotonPeer.OpCustom().
    Please take a look at "Adding Operations" documentation page.
  • Sorry, I didn't explain myself properly. That's not the problem. The problem is that my custom peer implementation that inherits from LoadbalancingPeer does not cause the OnOperationRequest method to be invoked on the server's GameClientPeer. So yes, I can call MyCustomPeer.OpCustom and it works perfectly fine; the operations are sent and received correctly, but the OnOperationRequest of server's GameClientPeer is not invoked. It's only invoked when I call PhotonNetwork.networkingPeer.OpCustom.
  • I fear, that's up to you to debug. Your custom peer class is unknown to us...
    You can check if it's connected, to which server and if you call SendOutgoingCommands on it or not, etc.