summaryrefslogtreecommitdiff
path: root/CSharp
diff options
context:
space:
mode:
Diffstat (limited to 'CSharp')
-rw-r--r--CSharp/Ivy/IvyPPC/IvyUDPStreamV4.cs86
1 files changed, 46 insertions, 40 deletions
diff --git a/CSharp/Ivy/IvyPPC/IvyUDPStreamV4.cs b/CSharp/Ivy/IvyPPC/IvyUDPStreamV4.cs
index 572b74b..b8d329a 100644
--- a/CSharp/Ivy/IvyPPC/IvyUDPStreamV4.cs
+++ b/CSharp/Ivy/IvyPPC/IvyUDPStreamV4.cs
@@ -7,24 +7,17 @@ using System.IO;
namespace IvyBus
{
- class IvyUDPStreamV4
+ class IvyUDPStreamV4 : IvyUDPStream
{
- Socket socket;
+
BinaryReader input;
BinaryWriter output;
- byte[] buffer;
- MemoryStream out_stream;
- MemoryStream in_stream;
/// the protocol version number
- internal const int PROCOCOLVERSION = 4;
- public IvyUDPStreamV4(Socket _socket)
+ internal const ushort PROCOCOLVERSION = 4;
+ public IvyUDPStreamV4(Socket _socket) : base ( _socket, PROCOCOLVERSION)
{
- socket = _socket;
- buffer = new byte[4096];
- in_stream = new MemoryStream(buffer);
- input = new BinaryReader( in_stream,Encoding.ASCII);
- out_stream = new MemoryStream();
+ input = new BinaryReader( in_stream,Encoding.ASCII);
output = new BinaryWriter(out_stream, Encoding.ASCII);
}
/*
@@ -34,41 +27,54 @@ namespace IvyBus
* message Format:
protocol_version, TCP server port , lenAppId, appId, lenAppNameId, appName
*/
-
- internal void receiveMsg(out IPEndPoint remote, out int version, out int port, out string appId, out string appName)
+ private ushort DeserializeShort()
{
- int len;
- IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
- EndPoint tempRemoteEP = (EndPoint)remoteEP;
- remoteEP = null;
- len = socket.ReceiveFrom(buffer, ref tempRemoteEP);
- remote = (IPEndPoint)tempRemoteEP;
- in_stream.Position = 0;
- version = (ushort)IPAddress.NetworkToHostOrder((short)input.ReadUInt16());
- port = (ushort)IPAddress.NetworkToHostOrder((short)input.ReadUInt16());
- int lenAppId = (ushort)IPAddress.NetworkToHostOrder((short)input.ReadUInt16());
- appId = new String(input.ReadChars(lenAppId));
- int lenAppName = (ushort)IPAddress.NetworkToHostOrder((short)input.ReadUInt16());
- appName = new String(input.ReadChars(lenAppName));
-
+ return (ushort)IPAddress.NetworkToHostOrder((ushort)input.ReadUInt16());
+ }
+ private string DeserializeString()
+ {
+ string arg;
+ int val_len;
+ char[] data;
+ val_len = (ushort)IPAddress.NetworkToHostOrder((ushort)input.ReadUInt16());
+ if (val_len != 0)
+ {
+ data = input.ReadChars(val_len);
+ arg = new String(data);
+ }
+ else
+ arg = "";
+ return arg;
}
- internal void sendMsg(IPEndPoint EPhost, int port, string appId, string appName)
+ internal override void Deserialize(out ushort version, out ushort port, out string appId, out string appName)
{
- output.Write((ushort)IPAddress.HostToNetworkOrder((short)(PROCOCOLVERSION)));
- output.Write((ushort)IPAddress.HostToNetworkOrder((short)port));
- output.Write((ushort)IPAddress.HostToNetworkOrder((short)appId.Length));
- output.Write(appId.ToCharArray());
- output.Write((ushort)IPAddress.HostToNetworkOrder((short)appName.Length));
- output.Write(appName.ToCharArray());
- output.Flush();
+ version = DeserializeShort();
+ port = DeserializeShort();
+ appId = DeserializeString();
+ appName = DeserializeString();
- byte[] hellob = out_stream.GetBuffer();
- socket.SendTo(hellob, (int)out_stream.Length, 0, EPhost);
}
- internal void Close()
+ private void Serialize(ushort arg)
{
- socket.Close();
+ output.Write((ushort)IPAddress.HostToNetworkOrder(arg));
}
+ private void Serialize(string arg)
+ {
+ ushort length = arg != null ? (ushort)arg.Length : (ushort)0;
+ Serialize(length);
+ if (length != 0)
+ output.Write(arg.ToCharArray());
+ }
+
+ internal override void Serialize(ushort port, string appId, string appName)
+ {
+ Serialize(PROCOCOLVERSION );
+ Serialize(port);
+ Serialize(appId);
+ Serialize(appName);
+ output.Flush();
+ }
+
}
}