using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.IO; namespace IvyBus { class IvyUDPStreamV3 : IvyUDPStream { Socket socket; StreamReader input; StreamWriter output; /// the protocol version number internal const int PROCOCOLVERSION = 3; public IvyUDPStreamV3(Socket _socket) : base( _socket , PROCOCOLVERSION ) { input = new StreamReader(in_stream, Encoding.ASCII); output = new StreamWriter(out_stream, Encoding.ASCII); } /* * message Syntax: * this is a binary formated message use of network representation * * message Format: protocol_version, TCP server port , appId, appName */ private ushort DeserializeShort() { 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; } private string DeserializeString(char sep) { 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(); } 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 = ""; } private void Serialize(ushort arg, char sep) { 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(); } } }