* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
-
- Change History (most recent first):
-
-$Log: SimpleChat.cs,v $
-Revision 1.7 2009/06/04 20:21:19 herscher
-<rdar://problem/3948252> Update code to work with DNSSD COM component
-
-Revision 1.6 2006/08/14 23:24:21 cheshire
-Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
-
-Revision 1.5 2004/09/13 19:37:42 shersche
-Change code to reflect namespace and type changes to dnssd.NET library
-
-Revision 1.4 2004/09/11 05:42:56 shersche
-don't reset SelectedIndex in OnRemove
-
-Revision 1.3 2004/09/11 00:38:58 shersche
-DNSService APIs now expect port in host format
-
-Revision 1.2 2004/07/19 22:08:53 shersche
-Fixed rdata->int conversion problem in QueryRecordReply
-
-Revision 1.1 2004/07/19 07:57:08 shersche
-Initial revision
-
-
-
-*/
+ */
using System;
using System.Drawing;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
- private System.Windows.Forms.Label label1;\r
- private Bonjour.DNSSDEventManager m_eventManager = null;\r
- private Bonjour.DNSSDService m_service = null;\r
- private Bonjour.DNSSDService m_registrar = null;\r
- private Bonjour.DNSSDService m_browser = null;\r
+ private System.Windows.Forms.Label label1;
+
+ private Bonjour.DNSSDEventManager m_eventManager = null;
+ private Bonjour.DNSSDService m_service = null;
+ private Bonjour.DNSSDService m_registrar = null;
+ private Bonjour.DNSSDService m_browser = null;
private Bonjour.DNSSDService m_resolver = null;
- private String m_name;\r
- private Socket m_socket = null;\r
- private const int BUFFER_SIZE = 1024;\r
- public byte[] m_buffer = new byte[BUFFER_SIZE];\r
- public bool m_complete = false;\r
- public StringBuilder m_sb = new StringBuilder();\r
- delegate void ReadMessageCallback(String data);\r
+ private String m_name;
+ private Socket m_socket = null;
+ private const int BUFFER_SIZE = 1024;
+ public byte[] m_buffer = new byte[BUFFER_SIZE];
+ public bool m_complete = false;
+ public StringBuilder m_sb = new StringBuilder();
+
+ delegate void ReadMessageCallback(String data);
+
ReadMessageCallback m_readMessageCallback;
/// <summary>
/// Required designer variable.
//
// Called by DNSServices core as a result of Register()
// call
- //\r
-\r
- public void\r
- ServiceRegistered\r
- (\r
- DNSSDService service,\r
- DNSSDFlags flags,\r
- String name,\r
- String regType,\r
- String domain\r
- )\r
- {\r
- m_name = name;\r
-\r
- try\r
- {\r
- m_browser = m_service.Browse(0, 0, "_p2pchat._udp", null, m_eventManager);\r
- }\r
- catch\r
- {\r
- MessageBox.Show("Browse Failed", "Error");\r
- Application.Exit();\r
- }\r
+ //
+ public void
+ ServiceRegistered
+ (
+ DNSSDService service,
+ DNSSDFlags flags,
+ String name,
+ String regType,
+ String domain
+ )
+ {
+ m_name = name;
+
+ //
+ // Try to start browsing for other instances of this service
+ //
+ try
+ {
+ m_browser = m_service.Browse(0, 0, "_p2pchat._udp", null, m_eventManager);
+ }
+ catch
+ {
+ MessageBox.Show("Browse Failed", "Error");
+ Application.Exit();
+ }
}
//
//
// Called by DNSServices core as a result of a Browse call
//
-
public void
ServiceFound
(
String regType,
String domain
)
- {\r
- if (serviceName != m_name)\r
- {\r
- PeerData peer = new PeerData();\r
-\r
- peer.InterfaceIndex = ifIndex;\r
- peer.Name = serviceName;\r
- peer.Type = regType;\r
- peer.Domain = domain;\r
- peer.Address = null;\r
-\r
- comboBox1.Items.Add(peer);\r
-\r
- if (comboBox1.Items.Count == 1)\r
- {\r
- comboBox1.SelectedIndex = 0;\r
- }\r
+ {
+ if (serviceName != m_name)
+ {
+ PeerData peer = new PeerData();
+
+ peer.InterfaceIndex = ifIndex;
+ peer.Name = serviceName;
+ peer.Type = regType;
+ peer.Domain = domain;
+ peer.Address = null;
+
+ comboBox1.Items.Add(peer);
+
+ if (comboBox1.Items.Count == 1)
+ {
+ comboBox1.SelectedIndex = 0;
+ }
}
- }\r
-\r
- //\r
- // ServiceLost\r
- //\r
- // Called by DNSServices core as a result of a Browse call\r
- //\r
-\r
- public void\r
- ServiceLost\r
- (\r
- DNSSDService sref,\r
- DNSSDFlags flags,\r
- uint ifIndex,\r
- String serviceName,\r
- String regType,\r
- String domain\r
- )\r
- {\r
- PeerData peer = new PeerData();\r
-\r
- peer.InterfaceIndex = ifIndex;\r
- peer.Name = serviceName;\r
- peer.Type = regType;\r
- peer.Domain = domain;\r
- peer.Address = null;\r
-\r
- comboBox1.Items.Remove(peer);\r
+ }
+
+ //
+ // ServiceLost
+ //
+ // Called by DNSServices core as a result of a Browse call
+ //
+ public void
+ ServiceLost
+ (
+ DNSSDService sref,
+ DNSSDFlags flags,
+ uint ifIndex,
+ String serviceName,
+ String regType,
+ String domain
+ )
+ {
+ PeerData peer = new PeerData();
+
+ peer.InterfaceIndex = ifIndex;
+ peer.Name = serviceName;
+ peer.Type = regType;
+ peer.Domain = domain;
+ peer.Address = null;
+
+ comboBox1.Items.Remove(peer);
}
//
// Called by DNSServices core as a result of DNSService.Resolve()
// call
//
-\r
- public void\r
- ServiceResolved\r
- (\r
- DNSSDService sref,\r
- DNSSDFlags flags,\r
- uint ifIndex,\r
- String fullName,\r
- String hostName,\r
- ushort port,\r
- TXTRecord txtRecord\r
+ public void
+ ServiceResolved
+ (
+ DNSSDService sref,
+ DNSSDFlags flags,
+ uint ifIndex,
+ String fullName,
+ String hostName,
+ ushort port,
+ TXTRecord txtRecord
)
- {\r
- m_resolver.Stop();\r
- m_resolver = null;\r
-\r
- PeerData peer = (PeerData)comboBox1.SelectedItem;\r
-\r
- peer.Port = port;\r
-\r
- try\r
- {\r
- m_resolver = m_service.QueryRecord(0, ifIndex, hostName, DNSSDRRType.kDNSSDType_A, DNSSDRRClass.kDNSSDClass_IN, m_eventManager );\r
- }\r
- catch\r
- {\r
- MessageBox.Show("QueryRecord Failed", "Error");\r
- Application.Exit();\r
+ {
+ m_resolver.Stop();
+ m_resolver = null;
+
+ PeerData peer = (PeerData)comboBox1.SelectedItem;
+
+ peer.Port = port;
+
+ //
+ // Query for the IP address associated with "hostName"
+ //
+ try
+ {
+ m_resolver = m_service.QueryRecord(0, ifIndex, hostName, DNSSDRRType.kDNSSDType_A, DNSSDRRClass.kDNSSDClass_IN, m_eventManager );
+ }
+ catch
+ {
+ MessageBox.Show("QueryRecord Failed", "Error");
+ Application.Exit();
}
}
// Called by DNSServices core as a result of DNSService.QueryRecord()
// call
//
-
public void
QueryAnswered
(
Object rdata,
uint ttl
)
- {\r
- m_resolver.Stop();\r
- m_resolver = null;\r
-\r
- PeerData peer = (PeerData) comboBox1.SelectedItem;\r
+ {
+ //
+ // Stop the resolve to reduce the burden on the network
+ //
+ m_resolver.Stop();
+ m_resolver = null;
+ PeerData peer = (PeerData) comboBox1.SelectedItem;
uint bits = BitConverter.ToUInt32( (Byte[])rdata, 0);
System.Net.IPAddress address = new System.Net.IPAddress(bits);
peer.Address = address;
- }\r
-\r
- public void\r
- OperationFailed\r
- (\r
- DNSSDService service,\r
- DNSSDError error\r
- )\r
- {\r
- MessageBox.Show("Operation returned an error code " + error, "Error");\r
- }\r
-\r
- //\r
- // OnReadMessage\r
- //\r
- // Called when there is data to be read on a socket\r
- //\r
- // This is called (indirectly) from OnReadSocket()\r
- //\r
- private void\r
- OnReadMessage\r
- (\r
- String msg\r
- )\r
- {\r
- int rgb = 0;\r
-\r
- for (int i = 0; i < msg.Length && msg[i] != ':'; i++)\r
- {\r
- rgb = rgb ^ ((int)msg[i] << (i % 3 + 2) * 8);\r
- }\r
-\r
- Color color = Color.FromArgb(rgb & 0x007F7FFF);\r
- richTextBox1.SelectionColor = color;\r
- richTextBox1.AppendText(msg + Environment.NewLine);\r
+ }
+
+ public void
+ OperationFailed
+ (
+ DNSSDService service,
+ DNSSDError error
+ )
+ {
+ MessageBox.Show("Operation returned an error code " + error, "Error");
+ }
+
+ //
+ // OnReadMessage
+ //
+ // Called when there is data to be read on a socket
+ //
+ // This is called (indirectly) from OnReadSocket()
+ //
+ private void
+ OnReadMessage
+ (
+ String msg
+ )
+ {
+ int rgb = 0;
+
+ for (int i = 0; i < msg.Length && msg[i] != ':'; i++)
+ {
+ rgb = rgb ^ ((int)msg[i] << (i % 3 + 2) * 8);
+
+ }
+
+ Color color = Color.FromArgb(rgb & 0x007F7FFF);
+
+ richTextBox1.SelectionColor = color;
+ richTextBox1.AppendText(msg + Environment.NewLine);
}
//
//
// Required for Windows Form Designer support
//
- InitializeComponent();\r
-\r
- try\r
- {\r
- m_service = new DNSSDService();\r
- }\r
- catch\r
- {\r
- MessageBox.Show("Bonjour Service is not available", "Error");\r
- Application.Exit();\r
- }\r
-\r
- m_eventManager = new DNSSDEventManager();\r
- m_eventManager.ServiceRegistered += new _IDNSSDEvents_ServiceRegisteredEventHandler(this.ServiceRegistered);\r
- m_eventManager.ServiceFound += new _IDNSSDEvents_ServiceFoundEventHandler(this.ServiceFound);\r
- m_eventManager.ServiceLost += new _IDNSSDEvents_ServiceLostEventHandler(this.ServiceLost);\r
- m_eventManager.ServiceResolved += new _IDNSSDEvents_ServiceResolvedEventHandler(this.ServiceResolved);\r
- m_eventManager.QueryRecordAnswered += new _IDNSSDEvents_QueryRecordAnsweredEventHandler(this.QueryAnswered);\r
+ InitializeComponent();
+
+ try
+ {
+ m_service = new DNSSDService();
+ }
+ catch
+ {
+ MessageBox.Show("Bonjour Service is not available", "Error");
+ Application.Exit();
+ }
+
+ //
+ // Associate event handlers with all the Bonjour events that the app is interested in.
+ //
+ m_eventManager = new DNSSDEventManager();
+ m_eventManager.ServiceRegistered += new _IDNSSDEvents_ServiceRegisteredEventHandler(this.ServiceRegistered);
+ m_eventManager.ServiceFound += new _IDNSSDEvents_ServiceFoundEventHandler(this.ServiceFound);
+ m_eventManager.ServiceLost += new _IDNSSDEvents_ServiceLostEventHandler(this.ServiceLost);
+ m_eventManager.ServiceResolved += new _IDNSSDEvents_ServiceResolvedEventHandler(this.ServiceResolved);
+ m_eventManager.QueryRecordAnswered += new _IDNSSDEvents_QueryRecordAnsweredEventHandler(this.QueryAnswered);
m_eventManager.OperationFailed += new _IDNSSDEvents_OperationFailedEventHandler(this.OperationFailed);
+ //
+ // Socket read handler
+ //
m_readMessageCallback = new ReadMessageCallback(OnReadMessage);
this.Load += new System.EventHandler(this.Form1_Load);
if (m_browser != null)
{
m_browser.Stop();
- }\r
-\r
- if (m_resolver != null)\r
- {\r
- m_resolver.Stop();\r
- }\r
-\r
- m_eventManager.ServiceFound -= new _IDNSSDEvents_ServiceFoundEventHandler(this.ServiceFound);\r
- m_eventManager.ServiceLost -= new _IDNSSDEvents_ServiceLostEventHandler(this.ServiceLost);\r
- m_eventManager.ServiceResolved -= new _IDNSSDEvents_ServiceResolvedEventHandler(this.ServiceResolved);\r
- m_eventManager.QueryRecordAnswered -= new _IDNSSDEvents_QueryRecordAnsweredEventHandler(this.QueryAnswered);\r
+ }
+
+ if (m_resolver != null)
+ {
+ m_resolver.Stop();
+ }
+
+ m_eventManager.ServiceFound -= new _IDNSSDEvents_ServiceFoundEventHandler(this.ServiceFound);
+ m_eventManager.ServiceLost -= new _IDNSSDEvents_ServiceLostEventHandler(this.ServiceLost);
+ m_eventManager.ServiceResolved -= new _IDNSSDEvents_ServiceResolvedEventHandler(this.ServiceResolved);
+ m_eventManager.QueryRecordAnswered -= new _IDNSSDEvents_QueryRecordAnsweredEventHandler(this.QueryAnswered);
m_eventManager.OperationFailed -= new _IDNSSDEvents_OperationFailedEventHandler(this.OperationFailed);
}
base.Dispose( disposing );
Byte[] bytes = Encoding.UTF8.GetBytes(message);
- IPEndPoint endPoint = new IPEndPoint( peer.Address, peer.Port );\r
-\r
+ IPEndPoint endPoint = new IPEndPoint( peer.Address, peer.Port );
+
+
+
m_socket.SendTo(bytes, endPoint);
richTextBox1.SelectionColor = Color.Black;
Application.Exit();
}
}
- }\r
-\r
- //\r
- // PeerData\r
- //\r
- // Holds onto the information associated with a peer on the network\r
- //\r
- public class PeerData\r
- {\r
- public uint InterfaceIndex;\r
- public String Name;\r
- public String Type;\r
- public String Domain;\r
- public IPAddress Address;\r
- public int Port;\r
-\r
- public override String\r
- ToString()\r
- {\r
- return Name;\r
- }\r
-\r
- public override bool\r
- Equals(object other)\r
- {\r
- bool result = false;\r
-\r
- if (other != null)\r
- {\r
- if ((object)this == other)\r
- {\r
- result = true;\r
- }\r
- else if (other is PeerData)\r
- {\r
- PeerData otherPeerData = (PeerData)other;\r
-\r
- result = (this.Name == otherPeerData.Name);\r
- }\r
- }\r
-\r
- return result;\r
- }\r
-\r
- public override int\r
- GetHashCode()\r
- {\r
- return Name.GetHashCode();\r
- }\r
+ }
+
+ //
+ // PeerData
+ //
+ // Holds onto the information associated with a peer on the network
+ //
+ public class PeerData
+ {
+ public uint InterfaceIndex;
+ public String Name;
+ public String Type;
+ public String Domain;
+ public IPAddress Address;
+ public int Port;
+
+ public override String
+ ToString()
+ {
+ return Name;
+ }
+
+ public override bool
+ Equals(object other)
+ {
+ bool result = false;
+
+ if (other != null)
+ {
+ if ((object)this == other)
+ {
+ result = true;
+ }
+ else if (other is PeerData)
+ {
+ PeerData otherPeerData = (PeerData)other;
+
+ result = (this.Name == otherPeerData.Name);
+ }
+ }
+
+ return result;
+ }
+
+
+ public override int
+ GetHashCode()
+ {
+ return Name.GetHashCode();
+ }
};
}