iTransferable Objects

back to iNetwork

Introduction

The iNetwork Toolkit presents a number of features that allow for easy ways to send and receive messages between client(s) and server. As was discussed in the toolkit recipe, only certain object types are allowed to be added to a message being sent to or from a client or server.

So then, what if a message needs to be sent containing data which consists of objects that are not one of the accepted object types?

The iTransferable Interface is a feature provided with the toolkit. This interface allows other objects, such as Ellipses or Images, or even a class object you created, to be delivered between client(s) and server.

back to top

Using iTransferable

The iTransferable interface allows for object types, which are not of any of the types shown below, to be added to or retrieved from a message that is sent or received, respectively, to or from the client(s)/server.

  • Bool
  • Byte
  • Char
  • Double
  • Float
  • Int
  • SByte
  • Short
  • UInt
  • ULong
  • UShort

iTransferable Objects

In order to make use of this feature, the iTransferable interface needs to be inherited by the object class.

public class ClassName : ITransferable {...}

Within this class the GetStreamData method must be implemented. Then within this method, using the NetworkStreamInfo object, you are able to store any relevant data or information with the AddValue method. Here some-name will be replaced with a string name that would refer to the data, someValue, provided.

  1. public void GetStreamData(NetworkStreamInfo info)
  2. {
  3.     info.AddValue("some-name", someValue);
  4. }

Finally the following constructor needs to be included for the class as well. The GetStreamData method 'stores information using the NetworkStreamInfo object. This constructor will now retrieve the data that was stored using the name, some-name, that had been assigned to it. You would need to make sure to assign these values to an appropriate variable so that the information can be easily retrieved through get methods or properties if need be.

  1. public ClassName(NetworkStreamData info)
  2. {
  3.     someVariable = info.GetMethod("some-name");
  4. }

A NetStreamData object has a number of get methods, as listed below:

  • GetBinary
  • GetBool
  • GetByte
  • GetDouble
  • GetFloat
  • GetHashCode
  • GetInt
  • GetLong
  • GetShort
  • GetString
  • GetValue

The next section will discuss how to use this class as well as send and receive messages.

In the Client/Server

Sending Messages

In order for the client and server to communicate with each other, Message objects are still sent as they normally would be through the connection. The only major difference is that the field added to message will contain an iTransferable object.

  1. Create a new iTransferable object.
    ClassName someObject = new ClassName();

    Note: In creating this object, do not use the constructor that was implemented in the previous section.
    ClassName someObject = new ClassName(someNetworkStreamInfoObject);

    You would need to also instantiate a NetworkStreamInfo object in order for this to work. However that is not necessary because all of that happens internally.
  2. Create a new Message object.
    Message msg = new Message("Message-Name");
  3. Add the iTransferable object to the message.
    msg.AddField("some-name", someObject);
  4. Finally send the message.
    this._connection.SendMessage(msg);
    OR
    this._server.BroadcastMessage(msg);
Receiving Messages

Similarly, receiving messages and retrieving fields from them are still executed the same way.

  1. Retrieve the iTransferable object from the message. In this case you would have to use the GetField method since all the other get methods specify a particular object type.
    ClassName newObject = msg.GetField("some-name", typeof(ClassName)) as ClassName;

    This will trigger the constructor implemented previously. Using the NetworkStreamInfo object and its appropriate get methods data is easily reacquired. Once you have the information you need you can proceed with your program/application's code as intended.
back to top