Client Events sample (.NET)

Overview

This sample demonstrates how Message class events can be used to monitor and control message data serialization, deserialization and data transmission through the channel.
This sample works with the MegaDemo Sample (.NET) server. The server project is included in the sample solution.

Getting Started

  • Compile both projects in the solution.
  • Launch the MegaDemo server and activate any server channel you like.
  • Launch the client part of the sample.
  • Set up the message and client channel to be used to talk to the server. Unlike the MegaDemo client, this sample doesn't use ZeroConf to discover the server automatically.
  • Click the Start button and see how the relatively large amount of data is transferred to the server and back.
  • In real life, such an amount of data is transferred almost instantly via LAN. We placed some delays for you to see how the transfer is progressing. You can turn this delay off and see the full speed by checking the Remove transfer delay checkbox.

Examine the Code

If you look into the client main form code, you'll find a number of event handlers related to the message and client channel.
Events related to the channel:

// Fired after the channel receives an incoming data stream
private void AfterReceiveStream(Object sender, StreamEventArgs e)
{
    Log(sender, String.Format("AfterReceiveStream: stream is {0} bytes.", e.Stream.Length));
}

// Fired after the channel is about to send data to the server
private void BeforeSendStream(Object sender, StreamEventArgs e)
{
    Log(sender, String.Format("BeforeSendStream: stream is {0} bytes.", e.Stream.Length));
}

// Fired when the data transfer through the channel completed
private void OnTransferEnd(Object sender, TransferEndEventArgs e)
{
  // ...
}

// Fired when a portion of data is transmitted through the channel
private void OnTransferProgress(Object sender, TransferProgressEventArgs e)
{
  // ...
}

// Fired when the data transfer through the channel is about to start
private void OnTransferStart(Object sender, TransferStartEventArgs e)
{
  // ...
}

Events related to the message:

// Fired after each parameter in the message is deserialized
private void AfterReadMessageParam(Object sender, MessageParamEventArgs e)
{
    Log(sender, String.Format("AfterReadParam: {0}: {1}, StreamAs {2}", e.Name, e.Type.Name, e.StreamAs.ToString()));
}

// Fired before each parameter in the message is serialized
private void BeforeWriteMessageParam(Object sender, MessageParamEventArgs e)
{
    Log(sender, String.Format("BeforeWriteParam: {0}: {1}, StreamAs {2}", e.Name, e.Type.Name, e.StreamAs.ToString()));
}

// Fired after all parameters are serialized and the message is ready to be sent
private void OnFinalizeMessage(Object sender, EventArgs e)
{
    Log(sender, "OnFinalizeMessage");
}

// Fired to prepare a new message instance before any parameters are serialized
private void OnInitializeMessage(Object sender, InitMessageEventArgs e)
{
    Log(sender, String.Format("OnInitializeMessage: {0}.{1}", e.InterfaceName, e.MessageName));
}