.NET.VBSource

Top  Previous  Next

Imports AstaSipCSharp

Imports Microsoft.VisualBasic

Imports System.Collections.Specialized

 

Public Class frmMain

Inherits System.Windows.Forms.Form

 

#Region " Windows Form Designer generated code "

 

Public Sub New()

   MyBase.New()

 

   'This call is required by the Windows Form Designer.

   InitializeComponent()

 

   'Add any initialization after the InitializeComponent() call

 

End Sub

 

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

   If disposing Then

     If Not (components Is Nothing) Then

       components.Dispose()

     End If

   End If

   MyBase.Dispose(disposing)

End Sub

 

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

 

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer. 

'Do not modify it using the code editor.

Friend WithEvents tbURI As System.Windows.Forms.TextBox

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents btDial As System.Windows.Forms.Button

Friend WithEvents btHangup As System.Windows.Forms.Button

Friend WithEvents lbLog As System.Windows.Forms.ListBox

Friend WithEvents btOptions As System.Windows.Forms.Button

Friend WithEvents btRegister As System.Windows.Forms.Button

Friend WithEvents btUnregister As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

   Me.tbURI = New System.Windows.Forms.TextBox

   Me.Label1 = New System.Windows.Forms.Label

   Me.btDial = New System.Windows.Forms.Button

   Me.btHangup = New System.Windows.Forms.Button

   Me.lbLog = New System.Windows.Forms.ListBox

   Me.btOptions = New System.Windows.Forms.Button

   Me.btRegister = New System.Windows.Forms.Button

   Me.btUnregister = New System.Windows.Forms.Button

   Me.SuspendLayout()

   '

   'tbURI

   '

   Me.tbURI.Location = New System.Drawing.Point(96, 8)

   Me.tbURI.Name = "tbURI"

   Me.tbURI.Size = New System.Drawing.Size(248, 20)

   Me.tbURI.TabIndex = 0

   Me.tbURI.Text = "sip:3000@192.168.0.1:5070"

   '

   'Label1

   '

   Me.Label1.Location = New System.Drawing.Point(8, 8)

   Me.Label1.Name = "Label1"

   Me.Label1.Size = New System.Drawing.Size(80, 16)

   Me.Label1.TabIndex = 1

   Me.Label1.Text = "Remote party:"

   '

   'btDial

   '

   Me.btDial.Location = New System.Drawing.Point(200, 32)

   Me.btDial.Name = "btDial"

   Me.btDial.Size = New System.Drawing.Size(72, 24)

   Me.btDial.TabIndex = 2

   Me.btDial.Text = "Dial"

   '

   'btHangup

   '

   Me.btHangup.Location = New System.Drawing.Point(280, 32)

   Me.btHangup.Name = "btHangup"

   Me.btHangup.Size = New System.Drawing.Size(64, 24)

   Me.btHangup.TabIndex = 3

   Me.btHangup.Text = "Hangup"

   '

   'lbLog

   '

   Me.lbLog.Dock = System.Windows.Forms.DockStyle.Bottom

   Me.lbLog.Location = New System.Drawing.Point(0, 112)

   Me.lbLog.Name = "lbLog"

   Me.lbLog.Size = New System.Drawing.Size(352, 69)

   Me.lbLog.TabIndex = 4

   '

   'btOptions

   '

   Me.btOptions.Location = New System.Drawing.Point(96, 32)

   Me.btOptions.Name = "btOptions"

   Me.btOptions.Size = New System.Drawing.Size(72, 24)

   Me.btOptions.TabIndex = 5

   Me.btOptions.Text = "Options..."

   '

   'btRegister

   '

   Me.btRegister.Location = New System.Drawing.Point(200, 64)

   Me.btRegister.Name = "btRegister"

   Me.btRegister.Size = New System.Drawing.Size(72, 24)

   Me.btRegister.TabIndex = 6

   Me.btRegister.Text = "Register"

   '

   'btUnregister

   '

   Me.btUnregister.Location = New System.Drawing.Point(280, 64)

   Me.btUnregister.Name = "btUnregister"

   Me.btUnregister.Size = New System.Drawing.Size(64, 24)

   Me.btUnregister.TabIndex = 7

   Me.btUnregister.Text = "Unregister"

   '

   'frmMain

   '

   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

   Me.ClientSize = New System.Drawing.Size(352, 181)

   Me.Controls.Add(Me.btUnregister)

   Me.Controls.Add(Me.btRegister)

   Me.Controls.Add(Me.btOptions)

   Me.Controls.Add(Me.lbLog)

   Me.Controls.Add(Me.btHangup)

   Me.Controls.Add(Me.btDial)

   Me.Controls.Add(Me.Label1)

   Me.Controls.Add(Me.tbURI)

   Me.Name = "frmMain"

   Me.Text = "SIP phone"

   Me.ResumeLayout(False)

 

End Sub

 

#End Region

Private sipEP As SIPEndPoint = New SIPEndPoint

 

 

Private Sub btDial_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDial.Click

   sipEP.MakeCall(tbURI.Text)

   StoreSetting("LastURI", tbURI.Text)

End Sub

 

Private Sub btHangup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btHangup.Click

   sipEP.Hangup()

End Sub

 

 

Private Function LoadSetting(ByRef Name As String, ByRef DefaultValue As String) As String

   Return GetSetting("AstaTech SIP.NET demo beta", "Options", Name, DefaultValue)

End Function

 

Private Function StoreSetting(ByRef Name As String, ByRef Value As String)

   SaveSetting("AstaTech SIP.NET demo beta", "Options", Name, Value)

End Function

 

Private Function GetPortFromCommandLine() As Integer

   Dim cmdArgs() As String = Environment.GetCommandLineArgs()

   If cmdArgs.Length > 1 Then

     Return Integer.Parse(cmdArgs(1))

   Else

     Return -1

   End If

End Function

 

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

   sipEP.Contact = LoadSetting("Contact", "sip:user@127.0.0.1:5060")

   sipEP.Realm = LoadSetting("Realm", "127.0.0.1")

   sipEP.UseProxy = Boolean.Parse(LoadSetting("UseProxy", Boolean.FalseString))

   sipEP.ProxyHost = LoadSetting("ProxyHost", "127.0.0.1")

   sipEP.ProxyPort = Integer.Parse(LoadSetting("ProxyPort", "5060"))

   sipEP.UseSTUN = Boolean.Parse(LoadSetting("UseStun", Boolean.FalseString))

   sipEP.STUNHost = LoadSetting("StunHost", "127.0.0.1")

   sipEP.STUNPort = Integer.Parse(LoadSetting("StunPort", "5060"))

   sipEP.User = LoadSetting("UserName", "")

   sipEP.Password = LoadSetting("UserPassword", "")

 

   'check binding interface and port

   Dim ifName As String = LoadSetting("BindingIf", "0.0.0.0")

 

   'check if preloaded interface exists

   Dim ifList As StringCollection = New StringCollection

   SIPEndPoint.GetInterfaceList(ifList)

   If ifList.IndexOf(ifName) = -1 Then

     ifName = "0.0.0.0" 'do not bind - let the AstaSIP choose right interface

   End If

   sipEP.BindingIf = ifName

 

   'check if binding port is specified in command line

   Dim ifPort As String = LoadSetting("BindingPort", 5060)

   Dim port As Integer = GetPortFromCommandLine()

   If port = -1 Then

     port = Integer.Parse(ifPort)

   End If

   sipEP.BindingPort = port

   sipEP.UseRXChannel = Boolean.Parse(LoadSetting("UseRXChannel", Boolean.TrueString))

   sipEP.UseTXChannel = Boolean.Parse(LoadSetting("UseTXChannel", Boolean.TrueString))

 

 

   'example how to get speaker and microphone lists

   Dim speakerList As StringCollection = New StringCollection

   Dim micList As StringCollection = New StringCollection

   sipEP.GetSpeakerList(speakerList)

   sipEP.GetMicrophoneList(micList)

 

   'Default values for audio

   sipEP.MicrophoneId = -1

   sipEP.SpeakerId = -1

   'sipEP.SpeakerVolume = 100

 

 

   'Set the last invited URI

   Me.tbURI.Text = LoadSetting("LastURI", "")

 

   sipEP.OnInvite = New SIPEndPoint.OnInviteEvent(AddressOf frmMain_OnInviteEvent)

   sipEP.OnConnected = New SIPEndPoint.OnConnectedEvent(AddressOf frmMain_OnConnected)

   sipEP.OnTerminated = New SIPEndPoint.OnTerminatedEvent(AddressOf frmMain_OnTerminated)

   sipEP.OnRegisterFailed = New SIPEndPoint.OnRegisterFailedEvent(AddressOf frmMain_OnRegisterFailed)

   sipEP.OnRegisterSuccess = New SIPEndPoint.OnRegisterSuccessEvent(AddressOf frmMain_OnRegisterSuccess)

   sipEP.Start()

End Sub

 

 

Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

   sipEP.Stop()

   sipEP = Nothing

End Sub

 

Public Function frmMain_OnInviteEvent(ByVal remoteParty As String) As Boolean

   lbLog.Items.Add(remoteParty + " sent invite. Accepting.")

   Return True

End Function

 

Public Sub frmMain_OnConnected(ByVal remoteParty As String)

   lbLog.Items.Add(remoteParty + " connected.")

End Sub

 

Public Sub frmMain_OnTerminated(ByVal remoteParty As String, ByVal reason As String)

   lbLog.Items.Add(remoteParty + " disconnected.")

End Sub

 

Public Sub frmMain_OnRegisterSuccess()

   lbLog.Items.Add("Registering is successfull.")

End Sub

 

Public Sub frmMain_OnRegisterFailed(ByVal responseCode As Integer)

   lbLog.Items.Add("Registering is failed. Error code is " + responseCode.ToString())

End Sub

 

Private Sub btOptions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btOptions.Click

   Dim options As FormOptions = New FormOptions

   Dim localPort As Integer = 0, port As Integer = 0, selectedIf As Integer = -1

   Dim userName As String, password As String, ifName As String, contact As String, host As String, useProxy As String

 

   REM preconfigure options dialog

   With options

     .tbContact.Text = sipEP.Contact

     .tbUserName.Text = sipEP.User

     .tbUserPassword.Text = sipEP.User

     .tbLocalPort.Text = sipEP.BindingPort.ToString()

     .cbUseProxy.Checked = sipEP.UseProxy

     .tbProxyHost.Text = sipEP.ProxyHost

     .tbProxyPort.Text = sipEP.ProxyPort.ToString()

     .cbUseStun.Checked = sipEP.UseSTUN

     .tbStunHost.Text = sipEP.STUNHost

     .tbStunPort.Text = sipEP.STUNPort

     .tbRealm.Text = sipEP.Realm

     .cbUseRXChannel.Checked = sipEP.UseRXChannel

     .cbUseTXChannel.Checked = sipEP.UseTXChannel

 

     'update interfaces combo list

     '.cbInterfaces.Items.Add('0.0.0.0')

 

     SIPEndPoint.GetInterfaceList(.cbInterfaces.Items)

     selectedIf = .cbInterfaces.FindStringExact(sipEP.BindingIf)

     If selectedIf = -1 Then

       .cbInterfaces.SelectedIndex = 0

     Else

       .cbInterfaces.SelectedIndex = selectedIf

     End If

 

     REM use AstaTech.com IP To found the most reliable interface

     'Try

     '.tbRecommendedIf.Text = SIPEndPoint.GetBestInterface('204.228.239.20')

     'Catch ex As Exception

     '.tbRecommendedIf.Text = 'None'

     'End Try

 

   End With

 

 

   If options.ShowDialog(Me) = DialogResult.OK Then

     If SIPEndPoint.ParseSIPContact(options.tbContact.Text, userName, host, port) = False Then

       MsgBox("Cannot parse contact name")

       Return

     End If

 

     'Update stack settings

 

     With options

       sipEP.Contact = .tbContact.Text

       sipEP.User = .tbUserName.Text

       sipEP.Password = .tbUserPassword.Text

       sipEP.BindingIf = .cbInterfaces.Text

       sipEP.BindingPort = Integer.Parse(.tbLocalPort.Text())

       sipEP.UseProxy = .cbUseProxy.Checked

       sipEP.ProxyHost = .tbProxyHost.Text

       sipEP.ProxyPort = Integer.Parse(.tbProxyPort.Text)

       sipEP.Realm = .tbRealm.Text

       sipEP.UseSTUN = .cbUseStun.Checked

       sipEP.STUNHost = .tbStunHost.Text

       sipEP.STUNPort = Integer.Parse(.tbStunPort.Text)

       sipEP.UseRXChannel = .cbUseRXChannel.Checked

       sipEP.UseTXChannel = .cbUseTXChannel.Checked

     End With

     sipEP.Stop()

     sipEP.Start()

 

     'Store configuration

     StoreSetting("Contact", sipEP.Contact)

     StoreSetting("Realm", sipEP.Realm)

     StoreSetting("BindingIf", sipEP.BindingIf)

     StoreSetting("BindingPort", sipEP.BindingPort.ToString())

     StoreSetting("UseProxy", sipEP.UseProxy.ToString())

     StoreSetting("ProxyHost", sipEP.ProxyHost)

     StoreSetting("ProxyPort", sipEP.ProxyPort.ToString())

     StoreSetting("UseStun", sipEP.UseSTUN.ToString())

     StoreSetting("StunHost", sipEP.STUNHost)

     StoreSetting("StunPort", sipEP.STUNPort.ToString())

     StoreSetting("UserName", sipEP.User)

     StoreSetting("UserPassword", sipEP.Password)

     StoreSetting("UseRXChannel", sipEP.UseRXChannel.ToString())

     StoreSetting("UseTXChannel", sipEP.UseTXChannel.ToString())

   End If

 

   options = Nothing

End Sub

 

Private Sub btRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btRegister.Click

   sipEP.Register()

End Sub

 

 

Private Sub btUnregister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btUnregister.Click

   sipEP.Unregister()

End Sub

End Class