public void TestCase() { Message msg1 = new Message("blah"); msg1.SetBinField("t_bin", new byte[]{1,2,3,4}); msg1.SetIntField("t_int", 0xAB); msg1.SetStringField("t_string", "hallo"); Message msg3 = new Message("blub"); msg3.SetStringField("a", "bbbbbbbbbbbbbbbbbbbbbb"); msg3.SetIntField("blahahah", 1); msg1.SetMessageField("t_message", msg3); var list = new HtspListType<IHtspBaseType>(); list.Add(new HtspType<long>(0x99)); list.Add(new HtspType<long>(0x77)); msg1.SetListField("t_list", list); string sMsg1 = msg1.ToString(true); Message msg2 = new Message(msg1.ToBin()); string sMsg2 = msg2.ToString(true); Console.WriteLine(sMsg1); Console.WriteLine(BitConverter.ToString(msg1.ToBin())); Console.WriteLine(sMsg2); Console.WriteLine(BitConverter.ToString(msg2.ToBin())); StringAssert.AreEqualIgnoringCase(sMsg1, sMsg2); }
public void Send(Message message) { byte[] buf=message.ToBin(); stream.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(buf.Length)),0,4); stream.Write(buf,0,buf.Length); //Console.WriteLine("Send: {0}", BitConverter.ToString(buf)); }
public static void Main(string[] args) { var client = new HtspClient("holzi", 9982); int seq = 1; Message request; Message reply; request = new Message("hello"); request.SetStringField("clientname", "htsp-sharp"); request.SetIntField("htspversion", 5); request.SetIntField("seq", seq++); client.Send(request); Console.WriteLine("Send:\n" + request.ToString()); reply = client.Receive(); Console.WriteLine("Received:\n" + reply.ToString()); request = new Message("enableAsyncMetadata"); request.SetIntField("seq", seq++); client.Send(request); Console.WriteLine("Send:\n" + request.ToString()); reply = client.Receive(); Console.WriteLine("Received:\n" + reply.ToString()); int counter = 0; while (counter++ < 2000) { reply = client.Receive(); if (reply.GetStringField("method").Contains("channel")) Console.WriteLine("Received:\n" + reply.ToString()); if (reply.GetStringField("method") == "initialSyncCompleted") break; } //getEvents 0x973 /* request = new Message("getEvents"); request.SetIntField("eventId", 0x1adf); request.SetIntField("numFollowing", 3); request.SetIntField("seq", seq++); client.Send(request); Console.WriteLine("Send:\n" + request.ToString(true)); reply = client.Receive(); Console.WriteLine("Received:\n" + reply.ToString(true)); */ /* request = new Message("subscribe"); request.SetIntField("channelId", 0x15); request.SetIntField("subscriptionId", 0xabcdef); request.SetIntField("seq", seq++); client.Send(request); Console.WriteLine("Send:\n" + request.ToString()); reply = client.Receive(); Console.WriteLine("Received:\n" + reply.ToString()); counter = 0; while (counter++ < 2000) { Message msg = client.Receive(); if ((msg.Method == "subscriptionStart")) { Console.WriteLine("Received:\n" + msg.ToString(true)); //Console.WriteLine("Received: {0}", BitConverter.ToString(msg.ToBin())); break; } } request = new Message("unsubscribe"); request.SetIntField("subscriptionId", 0xabcdef); request.SetIntField("seq", seq++); client.Send(request); Console.WriteLine("Send:\n" + request.ToString()); reply = client.Receive(); Console.WriteLine("Received:\n" + reply.ToString()); */ #region stuff /* byte[] bin = request.ToBin(); FileStream hello = File.Create("hello.bin"); hello.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(bin.Length)),0,4); hello.Write(bin,0,bin.Length); */ /* long vduration = 0; long aduration = 0; FileStream audio = File.Create("test.mp2"); FileStream video = File.Create("test.mpeg"); long stream = msg.GetIntField("stream"); byte[] payload = msg.GetBinField("payload"); if (stream == 0x1) { video.Write(payload, 0, payload.Length); vduration += msg.GetIntField("duration"); } else if (stream == 0x2) { audio.Write(payload, 0, payload.Length); aduration += msg.GetIntField("duration"); } audio.Close(); video.Close(); Console.WriteLine("aduration: {0} msecs", aduration/1000); Console.WriteLine("vduration: {0} msecs", vduration/1000); */ /* request = new Message(); request.SetStringField("method", "getSysTime"); request.SetIntField("seq", seq++); client.Send(request); Console.WriteLine("Send:\n" + request.ToString()); reply = client.Receive(); Console.WriteLine("Received:\n" + reply.ToString()); long unixdate = reply.GetIntField("time") - 60 * reply.GetIntField("timezone"); Console.WriteLine(new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(unixdate)); */ /* request = new Message(); request.SetStringField("method", "epgQuery"); request.SetStringField("query", "sport"); request.SetIntField("seq", seq++); client.Send(request); Console.WriteLine("Send:\n" + request.ToString()); reply = client.Receive(); Console.WriteLine("Received:\n" + reply.ToString(true)); var field = reply.GetListField("eventIds"); var val = (HtspType<long>)field[0]; request = new Message(); request.SetStringField("method", "getEvents"); request.SetIntField("eventId", val.Value); request.SetIntField("numFollowing", 1); request.SetIntField("seq", seq++); client.Send(request); Console.WriteLine("Send:\n" + request.ToString()); reply = client.Receive(); Console.WriteLine("Received:\n" + reply.ToString(true)); foreach(IHtspType item in reply.GetListField("events")) { //Console.WriteLine("+++ SubMessage\n{0}--- SubMessage\n",((HtspType<Message>)item).Value.ToString()); } */ #endregion // Close everything. client.Dispose(); }
private static void MessageDispatch(Message msg) { string method = msg.Method; if (method.Contains("channel")) { long id = msg.GetIntField("channelId"); if (method.Contains("Delete")) { channels.Remove(id); } else { if (method.Contains("Add")) { if (!channels.ContainsKey(id)) channels[id] = new Channel(id); } Channel chan = channels[id]; // -> (method.Contains("Update")) try { chan.Name = msg.GetStringField("channelName"); } // was kommt da eigentlich argument out of range? catch (Exception ex) {} try { chan.Number = msg.GetIntField("channelNumber"); } // was kommt da eigentlich argument out of range? catch (Exception ex) {} try { chan.Icon = msg.GetStringField("channelIcon"); } // was kommt da eigentlich argument out of range? catch (Exception ex) {} try { chan.CurrentEvent = msg.GetIntField("eventId"); } // was kommt da eigentlich argument out of range? catch (Exception ex) {} } } else if (method.Contains("tag")) { long id = msg.GetIntField("tagId"); } else { Console.WriteLine("received: {0}", method); } }
private static IHtspType ParseField(TypeID fType, byte[] bin, int offset, int valLen) { switch(fType) { case TypeID.MAP: { byte[] bVal = new byte[valLen]; Buffer.BlockCopy(bin, offset, bVal, 0, valLen); Message msg = new Message(bVal, 0, valLen); return new HtspType<Message>(msg); } case TypeID.S64: { return new HtspType<long>(ParseIntField(bin, offset, valLen)); } case TypeID.STR: { return new HtspType<string>(Encoding.UTF8.GetString(bin,offset,valLen)); } case TypeID.BIN: { byte[] bVal = new byte[valLen]; Buffer.BlockCopy(bin, offset, bVal, 0, valLen); return new HtspType<byte[]>(bVal); } case TypeID.LIST: { HtspListType<IHtspBaseType> list = new HtspListType<IHtspBaseType>(16); int lOffset = 0; while (lOffset < valLen) { TypeID lfType = (TypeID)bin[offset + lOffset++]; int lNameLen = bin[offset + lOffset++]; int lValLen = ParseValueLength(bin, offset + lOffset); lOffset += 4; // listfields dont have a name! we should probaly check that. //string name = Encoding.UTF8.GetString(bin,offset + lOffset,nameLen); lOffset += lNameLen; list.Add((IHtspBaseType)ParseField(lfType, bin, offset + lOffset, lValLen)); lOffset += lValLen; } return list; } default: { Console.WriteLine("MessageType ({0}) not implented yet!", fType); /* byte[] bVal = new byte[valLen]; Buffer.BlockCopy(bin, offset, bVal, 0, valLen); Console.WriteLine(BitConverter.ToString(bVal)); */ return null; } } }
public void SetMessageField(string name, Message val) { if (fields.ContainsKey(name)) { // implement own exception types! throw new Exception("field does alread exist!"); } else if (this == val) { // implement own exception types! // this gets really ugly, because of all the recursive function calls throw new Exception("Message must not cotain itself!"); } else { fields[name] = new HtspType<Message>(val); } }