public RegisterService (string name, string regtype, string replyDomain, int @interface, Protocol aprotocol) : base (name, regtype, replyDomain, @interface, aprotocol) { }
/* * Queue<Message> Outbound = new Queue<Message> (); * * public void Flush () * { * //should just iterate the enumerator here * while (Outbound.Count != 0) { * Message msg = Outbound.Dequeue (); * WriteMessage (msg); * } * } * * public bool ReadWrite (int timeout_milliseconds) * { * //TODO * * return true; * } * * public bool ReadWrite () * { * return ReadWrite (-1); * } * * public bool Dispatch () * { * //TODO * Message msg = Inbound.Dequeue (); * //HandleMessage (msg); * * return true; * } * * public bool ReadWriteDispatch (int timeout_milliseconds) * { * //TODO * return Dispatch (); * } * * public bool ReadWriteDispatch () * { * return ReadWriteDispatch (-1); * } */ internal Message ReadMessage() { byte[] header; byte[] body = null; int read; //16 bytes is the size of the fixed part of the header byte[] hbuf = new byte[16]; read = ns.Read(hbuf, 0, 16); if (read == 0) { return(null); } if (read != 16) { throw new Exception("Header read length mismatch: " + read + " of expected " + "16"); } EndianFlag endianness = (EndianFlag)hbuf[0]; MessageReader reader = new MessageReader(endianness, hbuf); //discard the endian byte as we've already read it reader.ReadByte(); //discard message type and flags, which we don't care about here reader.ReadByte(); reader.ReadByte(); byte version = reader.ReadByte(); if (version < Protocol.MinVersion || version > Protocol.MaxVersion) { throw new NotSupportedException("Protocol version '" + version.ToString() + "' is not supported"); } if (Protocol.Verbose) { if (version != Protocol.Version) { Console.Error.WriteLine("Warning: Protocol version '" + version.ToString() + "' is not explicitly supported but may be compatible"); } } uint bodyLength = reader.ReadUInt32(); //discard serial reader.ReadUInt32(); uint headerLength = reader.ReadUInt32(); //this check may become relevant if a future version of the protocol allows larger messages /* * if (bodyLength > Int32.MaxValue || headerLength > Int32.MaxValue) * throw new NotImplementedException ("Long messages are not yet supported"); */ int bodyLen = (int)bodyLength; int toRead = (int)headerLength; //we fixup to include the padding following the header toRead = Protocol.Padded(toRead, 8); long msgLength = toRead + bodyLen; if (msgLength > Protocol.MaxMessageLength) { throw new Exception("Message length " + msgLength + " exceeds maximum allowed " + Protocol.MaxMessageLength + " bytes"); } header = new byte[16 + toRead]; Array.Copy(hbuf, header, 16); read = ns.Read(header, 16, toRead); if (read != toRead) { throw new Exception("Message header length mismatch: " + read + " of expected " + toRead); } //read the body if (bodyLen != 0) { body = new byte[bodyLen]; read = ns.Read(body, 0, bodyLen); if (read != bodyLen) { throw new Exception("Message body length mismatch: " + read + " of expected " + bodyLen); } } Message msg = new Message(); msg.Connection = this; msg.Body = body; msg.SetHeaderData(header); return(msg); }
private void OnItemRemove(int @interface, Protocol protocol, string name, string type, string domain, LookupResultFlags flags) { lock (this) { BrowseService service = new BrowseService (name, type, domain, @interface, protocol); if (services.ContainsKey (name)) { services[name].Dispose (); services.Remove (name); } OnServiceRemoved (service); service.Dispose (); } }
private void OnResolveFound(int @interface, Protocol protocol, string name, string type, string domain, string host, Protocol aprotocol, string address, ushort port, byte [][] txt, LookupResultFlags flags) { Name = name; RegType = type; AvahiInterface = @interface; AvahiProtocol = protocol; ReplyDomain = domain; TxtRecord = new TxtRecord (txt); this.full_name = String.Format ("{0}.{1}.{2}", name.Replace (" ", "\\032"), type, domain); this.port = (short)port; this.host_target = host; host_entry = new IPHostEntry (); host_entry.AddressList = new IPAddress[1]; if (IPAddress.TryParse (address, out host_entry.AddressList[0]) && protocol == Protocol.IPv6) { host_entry.AddressList[0].ScopeId = @interface; } host_entry.HostName = host; OnResolved (); }