]> git.saurik.com Git - apple/mdnsresponder.git/blob - Clients/SimpleChat.VB/SimpleChat.vb
mDNSResponder-1096.100.3.tar.gz
[apple/mdnsresponder.git] / Clients / SimpleChat.VB / SimpleChat.vb
1 '
2 ' Copyright (c) 2010 Apple Inc. All rights reserved.
3 '
4 ' Licensed under the Apache License, Version 2.0 (the "License");
5 ' you may not use this file except in compliance with the License.
6 ' You may obtain a copy of the License at
7 '
8 ' http://www.apache.org/licenses/LICENSE-2.0
9 '
10 ' Unless required by applicable law or agreed to in writing, software
11 ' distributed under the License is distributed on an "AS IS" BASIS,
12 ' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ' See the License for the specific language governing permissions and
14 ' limitations under the License.
15 '
16
17 Imports System.Net
18 Imports System.Net.Sockets
19 Imports System.Data
20 Imports System.Text
21
22 Public Class SimpleChat
23 '
24 ' Associate Bonjour events with event handlers
25 '
26 Public WithEvents MyEventManager As New Bonjour.DNSSDEventManager
27 Private m_service As New Bonjour.DNSSDService
28 Private m_registrar As Bonjour.DNSSDService
29 Private m_browser As Bonjour.DNSSDService
30 Private m_resolver As Bonjour.DNSSDService
31 Private m_socket As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
32 Private m_port As Integer
33 Private m_buffer(1024 * 32) As Byte
34 Private m_async As IAsyncResult
35 Public Delegate Sub SocketDelegate(ByVal msg As String)
36 Private m_socketDelegate As SocketDelegate
37 Private m_name As String
38
39 Public Sub New()
40 MyBase.New()
41
42 'This call is required by the Windows Form Designer.
43 InitializeComponent()
44
45 Button1.Enabled = False
46
47 m_socketDelegate = New SocketDelegate(AddressOf MessageReceived)
48
49 Dim endPoint As New IPEndPoint(IPAddress.Any, 0)
50 m_socket.Bind(endPoint)
51 endPoint = m_socket.LocalEndPoint
52 m_port = endPoint.Port
53
54 Dim txtRecord As Bonjour.TXTRecord
55 m_async = m_socket.BeginReceive(m_buffer, 0, m_buffer.Length, SocketFlags.Partial, New AsyncCallback(AddressOf OnReceive), Me)
56 m_registrar = m_service.Register(0, 0, Environment.UserName, "_p2pchat._udp", vbNullString, vbNullString, m_port, txtRecord, MyEventManager)
57 End Sub
58
59 '
60 ' Called when Bonjour core finished registering a service successfully
61 '
62 Public Sub MyEventManager_ServiceRegistered(ByVal registrar As Bonjour.DNSSDService, ByVal flags As Bonjour.DNSSDFlags, ByVal name As String, ByVal regType As String, ByVal domain As String) Handles MyEventManager.ServiceRegistered
63 m_name = name
64 m_browser = m_service.Browse(0, 0, regType, vbNullString, MyEventManager)
65 End Sub
66
67 '
68 ' Called when a service is found
69 '
70 Public Sub MyEventManager_ServiceFound(ByVal browser As Bonjour.DNSSDService, ByVal flags As Bonjour.DNSSDFlags, ByVal ifIndex As UInteger, ByVal serviceName As String, ByVal regtype As String, ByVal domain As String) Handles MyEventManager.ServiceFound
71 If (serviceName <> m_name) Then
72 Dim peer As PeerData = New PeerData
73 peer.InterfaceIndex = ifIndex
74 peer.Name = serviceName
75 peer.Type = regtype
76 peer.Domain = domain
77 ComboBox1.Items.Add(peer)
78 ComboBox1.SelectedIndex = 0
79 End If
80 End Sub
81
82 '
83 ' Called when a service is lost
84 '
85 Public Sub MyEventManager_ServiceLost(ByVal browser As Bonjour.DNSSDService, ByVal flags As Bonjour.DNSSDFlags, ByVal ifIndex As UInteger, ByVal serviceName As String, ByVal regtype As String, ByVal domain As String) Handles MyEventManager.ServiceLost
86 ComboBox1.Items.Remove(serviceName)
87 End Sub
88
89 '
90 ' Called when a service is resolved
91 '
92 Public Sub MyEventManager_ServiceResolved(ByVal resolver As Bonjour.DNSSDService, ByVal flags As Bonjour.DNSSDFlags, ByVal ifIndex As UInteger, ByVal fullname As String, ByVal hostname As String, ByVal port As UShort, ByVal record As Bonjour.TXTRecord) Handles MyEventManager.ServiceResolved
93 m_resolver.Stop()
94 Dim peer As PeerData = ComboBox1.SelectedItem
95 peer.Port = port
96 m_resolver = m_service.QueryRecord(0, ifIndex, hostname, Bonjour.DNSSDRRType.kDNSSDType_A, Bonjour.DNSSDRRClass.kDNSSDClass_IN, MyEventManager)
97 End Sub
98
99 Public Sub MyEventManager_QueryAnswered(ByVal resolver As Bonjour.DNSSDService, ByVal flags As Bonjour.DNSSDFlags, ByVal ifIndex As UInteger, ByVal fullName As String, ByVal rrtype As Bonjour.DNSSDRRType, ByVal rrclass As Bonjour.DNSSDRRClass, ByVal rdata As Object, ByVal ttl As UInteger) Handles MyEventManager.QueryRecordAnswered
100 m_resolver.Stop()
101 Dim peer As PeerData = ComboBox1.SelectedItem
102 Dim bits As UInteger = BitConverter.ToUInt32(rdata, 0)
103 Dim address As IPAddress = New System.Net.IPAddress(bits)
104 peer.Address = address
105 End Sub
106
107 Public Sub MyEventManager_OperationFailed(ByVal registrar As Bonjour.DNSSDService, ByVal errorCode As Bonjour.DNSSDError) Handles MyEventManager.OperationFailed
108 MessageBox.Show("Operation failed error code: " + errorCode)
109 End Sub
110
111 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
112 Dim peer As PeerData = ComboBox1.SelectedItem
113 Dim message As String = m_name + ": " + TextBox2.Text
114 Dim bytes As Byte() = Encoding.UTF8.GetBytes(message)
115 Dim endPoint As IPEndPoint = New IPEndPoint(peer.Address, peer.Port)
116 m_socket.SendTo(bytes, 0, bytes.Length, 0, endPoint)
117 TextBox1.AppendText(TextBox2.Text + Environment.NewLine)
118 TextBox2.Text = ""
119 End Sub
120
121 Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
122 Dim peer As PeerData = ComboBox1.SelectedItem
123 m_resolver = m_service.Resolve(0, peer.InterfaceIndex, peer.Name, peer.Type, peer.Domain, MyEventManager)
124 End Sub
125 Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
126 Dim peer As PeerData = ComboBox1.SelectedItem
127 If ((peer.Address IsNot Nothing) And TextBox2.Text.Length > 0) Then
128 Button1.Enabled = True
129 Else
130 Button1.Enabled = False
131 End If
132 End Sub
133 Public Sub MessageReceived(ByVal msg As System.String)
134 TextBox1.AppendText(msg)
135 End Sub
136 Private Sub OnReceive(ByVal ar As IAsyncResult)
137 Dim bytesReceived As Integer = m_socket.EndReceive(ar)
138 If (bytesReceived > 0) Then
139 Dim msg As String = Encoding.UTF8.GetString(m_buffer, 0, bytesReceived)
140 Me.Invoke(m_socketDelegate, msg)
141 End If
142 m_async = m_socket.BeginReceive(m_buffer, 0, m_buffer.Length, SocketFlags.Partial, New AsyncCallback(AddressOf OnReceive), Me)
143 End Sub
144 End Class
145
146 Public Class PeerData
147 Public InterfaceIndex As UInteger
148 Public Name As String
149 Public Type As String
150 Public Domain As String
151 Public Address As IPAddress
152 Public Port As UShort
153
154 Overrides Function ToString() As String
155 Return Name
156 End Function
157 End Class