internal static void Main(string[] args) { try { if (args.Length != 0) { Int32.TryParse(args[0], out _port); } Console.WriteLine("Listening for connections on port {0}.", _port); LmcpBinaryProcessor processor = new LmcpBinaryProcessor(); processor.ObjectReceived += OnLmcpObjectReceived; processor.Error += OnError; byte[] buffer = new byte[1024]; TcpListener socket = new TcpListener(IPAddress.Loopback, _port); socket.Start(); for ( ;;) { TcpClient s = socket.AcceptTcpClient(); Console.WriteLine("Connection established."); NetworkStream stream = s.GetStream(); for ( ;;) { try { ILmcpObject obj = LmcpFactory.GetObject(new BinaryReader(stream)); if (obj != null) { Console.WriteLine("Received " + obj.GetType()); } } catch (IOException) { Console.WriteLine("Connection closed."); s.Close(); break; } } } } catch (Exception ex) { Console.WriteLine("Unexpected error: {0}.", ex); } Console.WriteLine("Press any key..."); Console.ReadKey(); }
/// <summary> /// Reads an LMCP-object. /// </summary> public ILmcpObject ReadObject() { bool exists = ReadBoolean(); if (exists) { ILmcpObject o = LmcpFactory.CreateObject(ReadInt64(), ReadUInt32(), ReadUInt16()); if (o != null) { o.Unpack(this.BaseStream); return(o); } } return(null); }
/// <summary> /// Writes an LMCP object to this writer using LMCP rules /// </summary> public void Write(ILmcpObject obj) { if (obj == null) { Write(false); } else { Write(true); Write(obj.SeriesNameAsLong); Write(obj.LmcpType); Write(obj.Version); obj.Pack(this.BaseStream); } }
/// <summary> /// Returns an byte array containing an ILmcpObject packed as a message. /// </summary> /// <returns>The packed message.</returns> public static byte[] PackMessage(ILmcpObject rootObject, bool calculatechecksum) { if (rootObject == null) { return(null); } int size = rootObject.CalculateSize(); byte[] bytes = new byte[size + HEADER_SIZE + CHECKSUM_SIZE]; MemoryStream buf = new MemoryStream(bytes); LmcpBinaryWriter bw = new LmcpBinaryWriter(buf); bw.Write(LMCP_CONTROL_STR); bw.Write(size); bw.Write(rootObject); uint cs = calculatechecksum ? CalculateChecksum(bytes) : 0; bw.Write(cs); return(buf.ToArray()); }
/// <summary> /// Initializes a new instance of the <see cref="LmcpObjectReceivedEventArgs"/> class. /// </summary> /// <param name="lmcpObject">The LMCP object.</param> public LmcpObjectReceivedEventArgs(ILmcpObject lmcpObject) { LmcpObject = lmcpObject; }