SendOperationResponse or EnqueueOperation

Options
Marek
Marek
edited April 2014 in Photon Server
Hi, when responding to operation should i use SendOperationResponse or EnqueueOperation?
What are the differences ? I assume that Enqueue will put that operation in some sort of queue and then dispatch all operations in order they was put in it. Does it really matter? Can using SendOperationResponse cause some sort of synchronization issues ?

Comments

  • Hey,

    a bit of background information:

    Photon makes use of "Fibers" to synchronize requests. You can think of a fiber as a queue - first in, first out, all actions on one fiber are executed "in order" and the execution of one action starts only when the execution of the previous action has finished. By this, we make sure that all actions on the fiber are thread-safe (because only one of them is executed at the same time).

    The work flow is like this:

    A client connects to Photon. Photon creates a "peer" object, this is the server-side representation of the connection. Each peer object has it's own fiber.
    A client sends an operation request to Photon. Photon puts this operation request in the peer's fiber. So we can be sure that all operations are executed by the peer in the same order as they arrive. When the operation is executed, PeerBase.OnOperationRequest() is called. Remember - this happens "on the fiber". If the operation affects only the peer (like an authenticate, for example), you can handle the operation inside the peer class and send a response right away (SendOperationResponse).

    Like this:
    Peer.Fiber.Enqueue(operation) -> Peer.Fiber.OnOperationRequest(operation) -> Peer.SendOperationResponse(response)

    If the client has joined a room, and he calls an operation that affects other players in the same room (like SetProperties, RaiseEvent etc.), we also need to synchronize the operations "per room". So we have another fiber "per room", so that we can make sure that only one operation is executed in the room at a time.
    In those cases, you call room.EnqueueOperation(). When the fiber starts execution of the operation, it calls the room's ExecuteOperation() method, where you actually have the code that is needed to handle the operation, and where you finally need to call peerBase.SendOperationResponse() to send a response to the client.

    Peer.Fiber.Enqueue(operation) -> Peer.Fiber.OnOperationRequest(operation) -> peer.room.EnqueueOperation -> room.ExecuteOperation() -> peer.SendOperationResponse(response)
  • Marek
    Options
    Thank you for informations, i will modify my code accordingly :)