📄 command.vb
字号:
Dim data As Byte() = Send(packet.ToByte)
Dim rsp As New PacketDecoder.PacketStructure
rsp = PacketDecoder.Decode(data)
If rsp.ResponseCode = PacketDecoder.PacketStructure.EnumResponseCode.Success Then
Return True
Else
mLastError = rsp.ResponseCode
Return False
End If
End Function
''' <summary>
''' Set current folder path. Absoluate path required.
''' </summary>
''' <param name="Path">Set to string.empty will guide you to root. For example "\test1\test2"</param>
''' <returns>True if success;False if failed</returns>
''' <remarks></remarks>
Public Overridable Function SetPath(ByVal path As String) As Boolean
path = path.Replace("/", "\")
Dim p() As String = path.Split("\")
'Back to root first
If BackToRoot() = False Then
Return False
End If
For i As Integer = 1 To p.Length - 1
Dim packet As New PacketEncoder.SetPathPacket()
packet.CreateFolderIfNotExist = False
packet.BackupALevelBeforeApplyingName = False
If mConnectionID >= 0 Then
packet.AddHeader(New ConnectionIDHeader(mConnectionID))
End If
packet.AddHeader(New NameHeader(p(i)))
Dim data As Byte() = Send(packet.ToByte)
Dim rsp As New PacketDecoder.PacketStructure
rsp = PacketDecoder.Decode(data)
If rsp.ResponseCode <> PacketDecoder.PacketStructure.EnumResponseCode.Success Then
mLastError = rsp.ResponseCode
Return False
End If
Next
Return True
End Function
''' <summary>
''' Create a folder in current directory.
''' </summary>
''' <param name="name">Folder name.</param>
''' <returns>True if success;False if failed</returns>
''' <remarks></remarks>
Public Overridable Function CreateFolder(ByVal name As String) As Boolean
Dim packet As New PacketEncoder.SetPathPacket()
packet.CreateFolderIfNotExist = True
packet.BackupALevelBeforeApplyingName = False
If mConnectionID >= 0 Then
packet.AddHeader(New ConnectionIDHeader(mConnectionID))
End If
packet.AddHeader(New NameHeader(name))
Dim data As Byte() = Send(packet.ToByte)
Dim rsp As New PacketDecoder.PacketStructure
rsp = PacketDecoder.Decode(data)
If rsp.ResponseCode <> PacketDecoder.PacketStructure.EnumResponseCode.Success Then
mLastError = rsp.ResponseCode
Return False
End If
'Backup to a level
packet = New PacketEncoder.SetPathPacket()
packet.CreateFolderIfNotExist = False
packet.BackupALevelBeforeApplyingName = True
data = Send(packet.ToByte)
rsp = PacketDecoder.Decode(data)
If rsp.ResponseCode <> PacketDecoder.PacketStructure.EnumResponseCode.Success Then
mLastError = rsp.ResponseCode
Return False
End If
Return True
End Function
''' <summary>
''' Del a folder
''' </summary>
''' <param name="name">
''' Where the EMPTY folder or file you want to del. For exmaple:
''' If you want to del "test2" under "\test1" folder, you can set name to "test2"
''' If you want to file "testFile" under \test1" folder, you can set name to "testFile"
''' </param>
''' <returns>True if success;False if failed</returns>
''' <remarks></remarks>
Public Overridable Function Del(ByVal name As String) As Boolean
Dim E As New PacketEncoder
E.OperateCode = PacketEncoder.OpCode.PUT
If mConnectionID >= 0 Then
E.AddHeader(New ConnectionIDHeader(mConnectionID))
End If
E.AddHeader(New NameHeader(name))
Dim d As New PacketDecoder
Dim rsp As PacketDecoder.PacketStructure = PacketDecoder.Decode(Send(E.ToByteData))
If rsp.ResponseCode = PacketDecoder.PacketStructure.EnumResponseCode.Success Then
Return True
Else
mLastError = rsp.ResponseCode
Return False
End If
End Function
''' <summary>
''' Send bytes from stream to destination path.
''' </summary>
''' <param name="source">Source of file</param>
''' <param name="path">Destination path.</param>
''' <param name="fileName">Desctination name</param>
''' <returns>True if success;False if failed</returns>
''' <remarks></remarks>
Public Overridable Function SendFile(ByVal source As IO.Stream, ByVal byteToSend As Integer, ByVal path As String, ByVal fileName As String) As Boolean
Return SendFile(source, byteToSend, Nothing, path, fileName)
End Function
''' <summary>
''' Send a file.
''' </summary>
''' <param name="source">Source of the data</param>
''' <param name="byteToSend">Bytes should send.</param>
''' <param name="customHeaderData">Custom Header Data.</param>
''' <param name="path">Destination folder of file. For example, "\Data\Folder"</param>
''' <param name="fileName">Destination file name.</param>
''' <returns></returns>
''' <remarks></remarks>
Public Overridable Function SendFile(ByVal source As IO.Stream, ByVal byteToSend As Integer, ByVal customHeaderData As Byte(), ByVal path As String, ByVal fileName As String) As Boolean
mAbort = False
If SetPath(path) = False Then
Return False
End If
TotalByteSend = 0
Dim packets() As PacketEncoder.SendFilePacket.Packet
Dim sendFilePacketsEncoder As New PacketEncoder.SendFilePacket
With sendFilePacketsEncoder
.ConnectionID = mConnectionID
.DataLength = byteToSend
.FileName = fileName
.MaxServerPacketSize = mMaxServerPacketSize
.Source = source
.CustomData = customHeaderData
packets = .ToPackets
End With
For i As Integer = 0 To packets.Length - 1
Dim data As Byte() = Send(packets(i).Data)
Dim rsp As PacketDecoder.PacketStructure = PacketDecoder.Decode(data)
If rsp.ResponseCode = PacketDecoder.PacketStructure.EnumResponseCode.Success Or _
rsp.ResponseCode = PacketDecoder.PacketStructure.EnumResponseCode.Continue Then
TotalByteSend += packets(i).ByteSend
Else
mLastError = rsp.ResponseCode
Return False
End If
Next
TotalByteSend = CInt(source.Length)
Return True
End Function
''' <summary>
''' Get bytes from destnation and write to a stream.
''' </summary>
''' <param name="sourcePath">Path of the file</param>
''' <param name="sourceName">File you want to get</param>
''' <param name="destination">IO stream to hold the data</param>
''' <returns>Total bytes received.
''' -1 for error</returns>
''' <remarks></remarks>
Public Overridable Function GetFile(ByVal sourcePath As String, ByVal sourceName As String, ByVal destination As IO.Stream) As Integer
mAbort = False
TotalByteReceived = 0
If SetPath(sourcePath) = False Then
Return -1
End If
Dim encoder As New PacketEncoder
encoder.OperateCode = PacketEncoder.OpCode.GET
encoder.FinalBitSet = True
If mConnectionID >= 0 Then
encoder.AddHeader(New ConnectionIDHeader(mConnectionID))
End If
encoder.AddHeader(New NameHeader(sourceName))
Dim data As Byte() = Send(encoder.ToByteData)
Dim rsp As New PacketDecoder.PacketStructure
rsp = PacketDecoder.Decode(data)
Dim detail As New PacketDecoder.PacketDetail
detail = PacketDecoder.GetDetail(rsp)
'Get first Packet
If rsp.ResponseCode = PacketDecoder.PacketStructure.EnumResponseCode.Success Then
If detail.BodyHeader Is Nothing = False Then
destination.Write(detail.BodyHeader, 0, detail.BodyHeader.Length())
destination.Flush()
Return detail.BodyHeader.Length
Else
Return 0
End If
End If
If rsp.ResponseCode = PacketDecoder.PacketStructure.EnumResponseCode.Continue Then
destination.Write(detail.BodyHeader, 0, detail.BodyHeader.Length())
destination.Flush()
TotalByteReceived += detail.BodyHeader.Length()
Else
Return -1
End If
'Get remaining packets
Do
If mAbort = True Then Exit Do
encoder = New PacketEncoder
encoder.OperateCode = PacketEncoder.OpCode.GET
encoder.FinalBitSet = True
If mConnectionID > 0 Then
encoder.AddHeader(New ConnectionIDHeader(mConnectionID))
End If
data = Send(encoder.ToByteData)
rsp = New PacketDecoder.PacketStructure
rsp = PacketDecoder.Decode(data)
detail = New PacketDecoder.PacketDetail
If rsp.ResponseCode = PacketDecoder.PacketStructure.EnumResponseCode.Continue Then
encoder = New PacketEncoder
encoder.FinalBitSet = True
encoder.OperateCode = PacketEncoder.OpCode.GET
If mConnectionID >= 0 Then
encoder.AddHeader(New ConnectionIDHeader(mConnectionID))
End If
End If
detail = PacketDecoder.GetDetail(rsp)
destination.Write(detail.BodyHeader, 0, detail.BodyHeader.Length())
destination.Flush()
TotalByteReceived += detail.BodyHeader.Length()
If detail.ResponseCode = PacketDecoder.PacketStructure.EnumResponseCode.Success Then Exit Do
Loop
Return TotalByteReceived
End Function
''' <summary>
''' Get file content and convert to string using encoder
''' </summary>
''' <param name="sourcePath"></param>
''' <param name="sourceName"></param>
''' <returns>String contains read data. If error or no data available. It returns String.Empty</returns>
''' <remarks></remarks>
Public Overridable Function GetFile(ByVal sourcePath As String, ByVal sourceName As String, ByVal encoder As Text.Encoding) As String
Dim mem As New IO.MemoryStream
Dim len As Integer = GetFile(sourcePath, sourceName, mem)
If len <= 0 Then Return String.Empty
Dim data(len - 1) As Byte
mem.Position = 0
mem.Read(data, 0, len)
Dim result As String = encoder.GetString(data)
Return result
End Function
''' <summary>
''' Get file content using ASCII encoding
''' </summary>
''' <param name="sourcePath"></param>
''' <param name="sourceName"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Overridable Function GetFile(ByVal sourcePath As String, ByVal sourceName As String) As String
Return GetFile(sourcePath, sourceName, Text.Encoding.ASCII)
End Function
''' <summary>
''' Send a OBEX command.
''' </summary>
''' <param name="cmdToSend">Command to send.</param>
''' <returns>Response</returns>
''' <remarks></remarks>
Public Function Send(ByVal cmdToSend As Byte()) As Byte()
mPort.ReadExisting()
mPort.Write(cmdToSend, 0, cmdToSend.Length)
Dim t1 As Date = Now
Dim buffer(1023) As Byte
Dim readCount As Integer
Dim byteReceived As New Collections.ArrayList
Dim len As Integer = 0
Do
If mPort.BytesToRead > 0 Then
readCount = mPort.Read(buffer, 0, buffer.Length)
Else
readCount = 0
End If
For i As Integer = 0 To readCount - 1
byteReceived.Add(buffer(i))
Next
If len = 0 AndAlso byteReceived.Count >= 3 Then
len = CByte(byteReceived(1)) * 256 + CByte(byteReceived(2))
End If
If len > 0 And byteReceived.Count = len Then Exit Do
If Now.Subtract(t1).TotalMilliseconds > mOBEXTimeOut Then
Throw New TimeoutException("Send OBEX timeout")
End If
Loop
Dim data(byteReceived.Count - 1) As Byte
byteReceived.CopyTo(data)
Return data
End Function
''' <summary>
''' Abort current process. Only used when sending or receving file.
''' </summary>
''' <remarks></remarks>
Public Sub Abort()
mAbort = True
End Sub
End Class
End Namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -