frmClient.vb 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. Public Class frmClient
  2. Public IsClosing As Boolean = False
  3. Public client As New TcpComm.Client(AddressOf UpdateUI, True, 30)
  4. Public lat As TcpComm.Utilities.LargeArrayTransferHelper
  5. Public Sub UpdateUI(ByVal bytes() As Byte, ByVal dataChannel As Byte)
  6. ' Use TcpComm.Utilities.LargeArrayTransferHelper to make it easier to send and receive
  7. ' large arrays sent via lat.SendArray()
  8. ' The LargeArrayTransferHelperb will assemble an incoming large array
  9. ' on any channel we choose to evaluate, and pass it back to this callback
  10. ' when it is complete. Returns True if it has handled this incomming packet,
  11. ' so we exit the callback when it returns true.
  12. If Not lat Is Nothing AndAlso lat.HandleIncomingBytes(bytes, dataChannel, , {100, 100}) Then Return
  13. If Me.InvokeRequired() Then
  14. ' InvokeRequired: We're running on the background thread. Invoke the delegate.
  15. Me.Invoke(client.ClientCallbackObject, bytes, dataChannel)
  16. Else
  17. ' We're on the main UI thread now.
  18. Dim dontReport As Boolean = False
  19. If dataChannel < 251 Then
  20. If bytes.Length < 65535 then
  21. Me.ListBox1.Items.Add(TcpComm.Utilities.BytesToString(bytes))
  22. Else
  23. ' This is a large array delivered by LAT. Display it in the
  24. ' large transfer viewer form.
  25. Me.ListBox1.Items.Add("Received LAT packet containing " & bytes.Length.ToString & " bytes.")
  26. Dim viewer As frmLatViewer = New frmLatViewer
  27. viewer.PassBytes(bytes)
  28. viewer.Show()
  29. End If
  30. ElseIf dataChannel = 255 Then
  31. Dim msg As String = TcpComm.Utilities.BytesToString(bytes)
  32. Dim tmp As String = ""
  33. ' Display info about the incoming file:
  34. If msg.Length > 15 Then tmp = msg.Substring(0, 15)
  35. If tmp = "Receiving file:" Then
  36. If btGetFile.Text = "Get File" then btGetFile.Text = "Cancel"
  37. gbGetFilePregress.Text = "Receiving: " & client.GetIncomingFileName
  38. dontReport = True
  39. End If
  40. ' Display info about the outgoing file:
  41. If msg.Length > 13 Then tmp = msg.Substring(0, 13)
  42. If tmp = "Sending file:" Then
  43. gbSendFileProgress.Text = "Sending: " & client.GetOutgoingFileName
  44. dontReport = True
  45. End If
  46. ' The file being sent to the client is complete.
  47. If msg = "->Done" Then
  48. gbGetFilePregress.Text = "File->Client: Transfer complete."
  49. btGetFile.Text = "Get File"
  50. dontReport = True
  51. End If
  52. ' The file being sent to the server is complete.
  53. If msg = "<-Done" Then
  54. gbSendFileProgress.Text = "File->Server: Transfer complete."
  55. btSendFile.Text = "Send File"
  56. dontReport = True
  57. End If
  58. ' The file transfer to the client has been aborted.
  59. If msg = "->Aborted." Then
  60. gbGetFilePregress.Text = "File->Client: Transfer aborted."
  61. dontReport = True
  62. End If
  63. ' The file transfer to the server has been aborted.
  64. If msg = "<-Aborted." Then
  65. gbSendFileProgress.Text = "File->Server: Transfer aborted."
  66. dontReport = True
  67. End If
  68. ' _Client as finished sending the bytes you put into sendBytes()
  69. If msg.Length > 4 Then tmp = msg.Substring(0, 4)
  70. If tmp = "UBS:" Then ' User Bytes Sent on channel:???.
  71. btSendText.Enabled = True
  72. dontReport = True
  73. End If
  74. ' We have an error message. Could be local, or from the server.
  75. If msg.Length > 4 Then tmp = msg.Substring(0, 5)
  76. If tmp = "ERR: " Then
  77. Dim msgParts() As String
  78. msgParts = Split(msg, ": ")
  79. MsgBox("" & msgParts(1), MsgBoxStyle.Critical, "Test Tcp Communications App")
  80. dontReport = True
  81. End If
  82. If msg.Equals("Disconnected.") then Me.Button2.Text = "Connect"
  83. ' Display all other messages in the status strip.
  84. If Not dontReport Then Me.ToolStripStatusLabel1.Text = TcpComm.Utilities.BytesToString(bytes)
  85. End If
  86. End If
  87. End Sub
  88. Private Sub frmClient_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  89. tmrPoll.Enabled = False
  90. IsClosing = True
  91. If client IsNot Nothing AndAlso client.isClientRunning Then
  92. client.Close(True)
  93. While client.isClientRunning
  94. Application.DoEvents()
  95. End While
  96. End If
  97. End Sub
  98. Private Sub frmClient_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  99. 'tmrPoll.Enabled = False
  100. 'IsClosing = True
  101. 'If client IsNot Nothing AndAlso client.isClientRunning Then
  102. ' client.Close(True)
  103. ' While client.isClientRunning
  104. ' Application.DoEvents()
  105. ' End While
  106. 'End If
  107. client.Close(True)
  108. End Sub
  109. Private Sub frmClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  110. TextBox2.Text = client.GetLocalIpAddress.ToString
  111. client.SetReceivedFilesFolder(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\ClientReceivedFiles")
  112. tmrPoll.Start()
  113. End Sub
  114. 'Private Sub bgQuickConnects()
  115. ' Dim s As String = Me.TextBox3.Text.Trim
  116. ' Dim errMsg As String = ""
  117. ' Dim count As Int32 = 0
  118. ' While Not IsClosing
  119. ' If Not client.Connect(Me.TextBox2.Text.Trim, Convert.ToInt32(s), Me.tbMachineID.Text, errMsg) Then
  120. ' If errMsg.Trim <> "" Then MsgBox(errMsg, MsgBoxStyle.Critical, "Test Tcp Communications App")
  121. ' Exit While
  122. ' End If
  123. ' client.Close()
  124. ' End While
  125. 'End Sub
  126. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  127. 'Dim t As New Threading.Thread(AddressOf bgQuickConnects)
  128. 't.Start()
  129. If Me.Button2.Text = "Connect" Then
  130. Dim s As String
  131. Dim errMsg As String = ""
  132. s = Me.TextBox3.Text.Trim
  133. Me.Button2.Enabled = False
  134. Me.Button2.Text = "Disconnect"
  135. client = New TcpComm.Client(AddressOf UpdateUI, cbReconnect.Checked, 30)
  136. If Not client.Connect(Me.TextBox2.Text.Trim, Convert.ToInt32(s), Me.tbMachineID.Text, errMsg) Then
  137. Me.Button2.Text = "Connect"
  138. If errMsg.Trim <> "" Then MsgBox(errMsg, MsgBoxStyle.Critical, "Test Tcp Communications App")
  139. End If
  140. lat = New TcpComm.Utilities.LargeArrayTransferHelper(client)
  141. Me.Button2.Enabled = True
  142. Else
  143. client.Close()
  144. Me.Button2.Text = "Connect"
  145. End If
  146. End Sub
  147. Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btGetFileBrowse.Click
  148. Dim ofd As New OpenFileDialog
  149. Dim _path As String
  150. ofd.ShowDialog()
  151. _path = ofd.FileName
  152. tbGetFileReq.Text = _path
  153. End Sub
  154. Private Sub btGetFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btGetFile.Click
  155. If btGetFile.Text = "Get File" Then
  156. If tbGetFileReq.Text.Trim <> "" Then
  157. If client.isClientRunning Then client.GetFile(tbGetFileReq.Text.Trim)
  158. btGetFile.Text = "Cancel"
  159. End If
  160. Else
  161. client.CancelIncomingFileTransfer()
  162. btGetFile.Text = "Get File"
  163. End If
  164. End Sub
  165. Private Sub tmrPoll_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrPoll.Tick
  166. ' Update progress bars
  167. pbIncomingFile.Value = client.GetPercentOfFileReceived
  168. pbOutgoingFile.Value = client.GetPercentOfFileSent
  169. ' Update Mbps
  170. Me.Text = "Test Client (" & client.GetMbps & ")"
  171. End Sub
  172. Private Sub btSendFileBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSendFileBrowse.Click
  173. Dim ofd As New OpenFileDialog
  174. ofd.ShowDialog()
  175. tbSendFile.Text = ofd.FileName
  176. End Sub
  177. Private Sub btSendFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSendFile.Click
  178. If btSendFile.Text = "Send File" Then
  179. If tbSendFile.Text.Trim <> "" Then
  180. If client.isClientRunning Then client.SendFile(tbSendFile.Text.Trim)
  181. btSendFile.Text = "Cancel"
  182. End If
  183. Else
  184. client.CancelOutgoingFileTransfer()
  185. btSendFile.Text = "Send File"
  186. End If
  187. End Sub
  188. Private Sub btSendText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSendText.Click
  189. If Me.tbSendText.Text.Trim.Length > 0 Then
  190. Dim errorMessage As String = ""
  191. If Not client.SendText(Me.tbSendText.Text.Trim, , errorMessage) Then MsgBox(errorMessage, MsgBoxStyle.Critical)
  192. End If
  193. End Sub
  194. Public Function SetMachineIDNumber(ByVal idNumber As Int32) As Boolean
  195. Me.tbMachineID.Text = "Unique Machine ID" & idNumber.ToString()
  196. Return True
  197. End Function
  198. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  199. client.SetMachineID(Me.tbMachineID.Text)
  200. End Sub
  201. End Class