iTransferable Objects
back to iNetwork
Contents
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.
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.
- public void GetStreamData(NetworkStreamInfo info)
- {
- info.AddValue("some-name", someValue);
- }
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.
- public ClassName(NetworkStreamData info)
- {
- someVariable = info.GetMethod("some-name");
- }
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.
- Create a new iTransferable object.
Note: In creating this object, do not use the constructor that was implemented in the previous section.
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. - Create a new Message object.
- Add the iTransferable object to the message. msg.AddField("some-name", someObject);
- 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.
- 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.
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.