Delay of message in the queue

Options
billTCP
edited June 2012 in Photon Server
I just begin using Photon, and built a simple server-client program, in which the client is a console and only send message from user input to the server, and the server send the exact content back to client. The client then display the message received from the server.
I used the queue to send the message:
this.fiber.Enqueue(() => this.peer.OpCustom((byte)OperationCode.LoginOperation, parameter, true));
and also the server:
this.fiber.Enqueue(() => this.PhotonPeer.SendEvent(eventData));
but seems there is a delay (my guess) in receiving the message?
Like below:
PeerStatusCallBack:Connect
:>1
:>2
:>3
Received(0):1
:>4
Received(0):2
Received(0):3
:>5
Received(0):4
:>
I only got "Received(0):1" after I have already sent the 3rd message "3". Is it because some message are temperately stored in the buffer queue?

Thanks!

Comments

  • Sergiy
    Options
    IMHO, it can be because of 2 separate reasons:
    1. Photon does not send requests/responses immediately. Instead, they are combined in a packet and sent afterwards as a whole. This helps saving bandwidth. So if you send requests quite fast, you will get response after a while. Take a look for following lines in PhotonServer.config file:
    EnablePerformanceCounters = "true"
    DataSendingDelayMilliseconds="50"
    AckSendingDelayMilliseconds="50"
    MinimumTimeout="5000"
    MaximumTimeout="10000"
    ProduceDumps="true"
    MaxMessageSize="512000"
    MaxQueuedDataPerPeer="512000"
    PerPeerMaxReliableDataInTransit="51200"
    PerPeerTransmitRateLimitKBSec="256"
    PerPeerTransmitRatePeriodMilliseconds="200"
    
    2. Network latency.
  • billTCP
    Options
    Sergiy wrote:
    IMHO, it can be because of 2 separate reasons:
    1. Photon does not send requests/responses immediately. Instead, they are combined in a packet and sent afterwards as a whole. This helps saving bandwidth. So if you send requests quite fast, you will get response after a while. Take a look for following lines in PhotonServer.config file:
    EnablePerformanceCounters = "true"
    DataSendingDelayMilliseconds="50"
    AckSendingDelayMilliseconds="50"
    MinimumTimeout="5000"
    MaximumTimeout="10000"
    ProduceDumps="true"
    MaxMessageSize="512000"
    MaxQueuedDataPerPeer="512000"
    PerPeerMaxReliableDataInTransit="51200"
    PerPeerTransmitRateLimitKBSec="256"
    PerPeerTransmitRatePeriodMilliseconds="200"
    
    2. Network latency.

    Thanks for your advice, but I think the 2rd reason may be unlikely, as I test it on the localhost, and what's more, the previous message would not be received and displayed in the console until I send a next message and press enter.

    I send the message using the interface
    this.fiber.Enqueue(() => this.peer.OpCustom((byte)OperationCode.ChatOperation, parameter, true));
    Console.WriteLine("Msg sent:i={0}",++i);
    
    And receive the message in the event call back function
    public void EventAction(byte eventCode, System.Collections.Hashtable photonEvent)
    {
           ......
           String msg = (string)photonEvent[(byte)1];
           Console.WriteLine("Received({0}):{1}", i, msg);
    }
    
    Where i is a global variable to trace the sequence of sending and receiving message. The console information is:
    :>1
    Msg sent:i=1
    :>2
    Msg sent:i=2
    :>3
    Msg sent:i=3
    Received(3):1
    Received(3):2
    :>4
    Msg sent:i=4
    Received(4):3
    :>5
    Msg sent:i=5
    Received(5):4
    :>
    
    Looks like the receive function "EventAction" is called immediately after the sending function, but it receives the old message instead of what I just input.

    Is it because of the queue that cause this asynchronism?

    Thanks!
  • Sergiy
    Options
    You should call Service() about 10 times/sec in order to allow photon to dispatch incoming/outgoing messages. Are you doing it?
  • billTCP
    Options
    Sergiy wrote:
    You should call Service() about 10 times/sec in order to allow photon to dispatch incoming/outgoing messages. Are you doing it?
    I used "peer.service()" in the loop of the run() function, so every time I send a message the service() will be called. How can I call it 10 times/sec if the client is waiting for the user's input at the Console.Readline() ? Do you suggest to put the peer.service() in another thread and run it endless?
  • Sergiy
    Options
    Exactly. Service() should be called constantly during whole lifetime of the application. A good solution would be to use system.timers.timer class with interval of 5-10 ms. That would probably solve your case
  • billTCP
    Options
    Sergiy wrote:
    Exactly. Service() should be called constantly during whole lifetime of the application. A good solution would be to use system.timers.timer class with interval of 5-10 ms. That would probably solve your case
    Awesome! Problem solved! I used System.timers.timer and run peer.service() every 100ms, then the message appears every time I send them. Thanks dude!
  • Tobias
    Options
    Make sure to avoid concurrent usage of the peer by multiple threads! Operations and Service calls should never done concurrently.
  • billTCP
    Options
    Tobias wrote:
    Make sure to avoid concurrent usage of the peer by multiple threads! Operations and Service calls should never done concurrently.
    Do you mean we should not use service() in sub thread, or just avoid running service() in more than one threads?
  • dreamora
    Options
    that means that any code interacting with Photon has to run on the same thread.