From a1f7872de160eb2c42797ed7280f649dcacea24b Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 10:02:46 +0000 Subject: Utilisateur : Fcolin Date : 19/12/05 Heure : 16:54 Archivé dans $/CSharp/Ivy Commentaire: (vss 3) --- CSharp/Ivy/IvyPPC/IvyUDPStreamV3.cs | 95 ++++++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 39 deletions(-) diff --git a/CSharp/Ivy/IvyPPC/IvyUDPStreamV3.cs b/CSharp/Ivy/IvyPPC/IvyUDPStreamV3.cs index d09d85b..556c4a0 100644 --- a/CSharp/Ivy/IvyPPC/IvyUDPStreamV3.cs +++ b/CSharp/Ivy/IvyPPC/IvyUDPStreamV3.cs @@ -7,25 +7,18 @@ using System.IO; namespace IvyBus { - class IvyUDPStreamV3 + class IvyUDPStreamV3 : IvyUDPStream { Socket socket; StreamReader input; StreamWriter output; - byte[] buffer; - MemoryStream out_stream; - MemoryStream in_stream; /// the protocol version number internal const int PROCOCOLVERSION = 3; - public IvyUDPStreamV3(Socket _socket) + public IvyUDPStreamV3(Socket _socket) : base( _socket , PROCOCOLVERSION ) { - socket = _socket; - buffer = new byte[4096]; - in_stream = new MemoryStream(buffer); input = new StreamReader(in_stream, Encoding.ASCII); - out_stream = new MemoryStream(); output = new StreamWriter(out_stream, Encoding.ASCII); } /* @@ -33,43 +26,67 @@ namespace IvyBus * this is a binary formated message use of network representation * * message Format: - protocol_version, TCP server port , lenAppId, appId, lenAppNameId, appName + protocol_version, TCP server port , appId, 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 = 0; //TODO input.ReadUInt16(); - port = 0; //TODO input.ReadUInt16(); - int lenAppId = 0; //TODO input.ReadUInt16(); - appId = null; //TODO input.ReadChars(lenAppId); - int lenAppName = 0; //TODO input.ReadUInt16(); - appName = null; //TODO new String(input.ReadChars(lenAppName)); - + int read; + ushort ret = 0; + char digit; + // this will eat next non digit car ie space + do + { + read = input.Read(); + if (read < 0) break; + digit = (char) read; + if ( Char.IsDigit(digit) ) + ret = (ushort)(ret * 10 + (digit-0x30)); + } while (Char.IsDigit(digit)); + return ret; } - - internal void sendMsg(IPEndPoint EPhost, int port, string appId, string appName) + private string DeserializeString(char sep) { - 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(); + int read; + char car; + StringBuilder str = new StringBuilder(); + // this will eat next non digit car ie space + do + { + read = input.Read(); + if (read < 0) break; + car = (char)read; + if (car != sep) + str.Append(car); + } while (car != sep); + return str.ToString(); + } - byte[] hellob = out_stream.GetBuffer(); - socket.SendTo(hellob, (int)out_stream.Length, 0, EPhost); + internal override void Deserialize(out ushort version, out ushort port, out string appId, out string appName) + { + version = DeserializeShort(); + port = DeserializeShort(); + //appId = DeserializeString(); //No AppId in V3 + //appName = DeserializeString('\n'); + appId = ""; + appName = ""; } - internal void Close() + private void Serialize(ushort arg, char sep) { - socket.Close(); + output.Write(arg); + output.Write(sep); + } + private void Serialize(string arg, char sep) + { + output.Write(arg); + output.Write(sep); + } + internal override void Serialize(ushort port, string appId, string appName) + { + Serialize(PROCOCOLVERSION, ' '); + Serialize(port,'\n'); + //Serialize(appId); //No AppId in V3 + //Serialize(appName, '\n'); //No Appname in V3 + output.Flush(); } } } -- cgit v1.1