扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:白猫黑爪子 来源:天极开发 2007年11月9日
关键字:
IrDAClient | 提供连接信息,并创建客户端连接对象,用以打开和关闭与服务器的连接。 |
IrDADeviceInfo | 提供客户端在发现查询过程中获取的有关可用服务器和端口的信息。 |
IrDAListener | 将套接字置于侦听状态,以监视来自指定服务或网络地址的连接。 |
Dim taobjIrDADeviceInfo() As Sockets.IrDADeviceInfo Dim tobjIrDAClient As New Sockets.IrDAClient '开始检索附近的红外设备并加入到列表中,最多只搜索7个设备 taobjIrDADeviceInfo = tobjIrDAClient.DiscoverDevices(7) |
Dim tobjIrDAClient As New Sockets.IrDAClient 'ni_objIrDADeviceInfo是上文介绍的IrDADeviceInfo对象,以下语句的含义就是连接到'DeviceID指定的红外设备上,这个设备提供OBEX服务。 Dim tobjIrDAEndPoint As New IrDAEndPoint(ni_objIrDADeviceInfo.DeviceID, "OBEX") Try tobjIrDAClient.Connect(tobjIrDAEndPoint) Catch ex As Exception MessageBox.Show("IrDA Socket Connect 状态失败") End Try |
Imports System.Net Imports System.Net.Sockets Public Class clsIrDAOBEXOperator 'OBEX常数 Public Const cOBEX_VERSION As Byte = &H10 Public Const cOBEX_CONNECTFLAGS As Byte = &H0 Public Const cOBEX_CONNECT As Byte = &H80 Public Const cOBEX_DISCONNECT As Byte = &H81 Public Const cOBEX_PUT As Byte = &H2 Public Const cOBEX_NAME As Byte = &H1 Public Const cOBEX_PUT_FINAL As Byte = &H82 Public Const cOBEX_LENGTH As Byte = &HC3 Public Const cOBEX_BODY As Byte = &H48 Public Const cOBEX_END_OF_BODY As Byte = &H49 Public Const cOBEX_CONTINUE As Byte = &H90 Public Const cOBEX_SUCCESS As Byte = &HA0 '向外界公布可直接修改的变量 Public m_i32OBEXMAXSendBufferLen As Int32 = 255 Public m_i32OBEXMAXReceiveBufferLen As Int32 = 1024 Public m_i32OBEXMAXPacketSize As Int16 = &H808 '将一个16位整数转为2个字节的数组,并存入参数中指定索引开始的字节数组处(低位字在低字节,高位字在高字节) Private Function fnInt16ToByteArray(ByVal ni_i16Convert As Int16, ByRef ni_abytDest() As Byte, ByVal ni_i32StartIndex As Int32) As Boolean ni_abytDest(ni_i32StartIndex) = ni_i16Convert And &HFF ni_abytDest(ni_i32StartIndex + 1) = ni_i16Convert >> 8 End Function '将一个16位整数转为2个字节的数组,并存入参数中指定索引开始的字节数组处 Private Function fnInt16ToByteArrayLowByteToHightPosition(ByVal ni_i16Convert As Int16, ByRef ni_abytDest() As Byte, ByVal ni_i32StartIndex As Int32) As Boolean ni_abytDest(ni_i32StartIndex + 1) = ni_i16Convert And &HFF ni_abytDest(ni_i32StartIndex) = ni_i16Convert >> 8 End Function '将一个32位整数转为4个字节的数组,并存入参数中指定索引开始的字节数组处 Private Function fnInt32ToByteArrayLowByteToHightPosition(ByVal ni_i32Convert As Int16, ByRef ni_abytDest() As Byte, ByVal ni_i32StartIndex As Int32) As Boolean Dim ti32Tempa As Int32 = ni_i32Convert ni_abytDest(ni_i32StartIndex + 3) = ni_i32Convert And &HFF ni_abytDest(ni_i32StartIndex + 2) = (ni_i32Convert And &HFF00) >> 8 ni_abytDest(ni_i32StartIndex + 1) = (ni_i32Convert And &HFF0000) >> 16 ni_abytDest(ni_i32StartIndex) = (ti32Tempa And &HFF000000) >> 24 End Function '将本设备中指定的文件传到指定的设备中去 Public Function fnSendFileToIrDADevice(ByVal ni_objIrDADeviceInfo As IrDADeviceInfo, _ ByVal ni_strFilePath As String) As Boolean 'ni_objIrDADeviceInfo:指定红外设备的IrDAInfo结构 'ni_strFilePath:要传输的文件路径 '返回值:成功返回true Dim tobjIrDAClient As New Sockets.IrDAClient Dim tobjIrDAEndPoint As New IrDAEndPoint(ni_objIrDADeviceInfo.DeviceID, "OBEX") Dim tabytSendBuffer(m_i32OBEXMAXSendBufferLen * 2) As Byte Dim tabytReceiveBuffer(m_i32OBEXMAXReceiveBufferLen) As Byte Dim tblnTemp As Boolean Dim ti32Tempa As Int32, ti32TempB As Int32, tieTempC As Int32 Dim tbytTempa As Byte Dim ti32LoopA As Int32 Dim tobjBinaryReader As IO.BinaryReader Dim tobjFileStream As IO.FileStream Dim tabytTempa() As Byte, tabytTempb() As Byte, tabytTempC() As Byte, tabytTempD() As Byte Dim tabytTempE() As Byte Dim tobjOBEX_HEADERS As New clsOBEXHeaders Dim ti32SendPackageSpare As Int32 '保存最大发送包数组-当前数据后的剩余空间 Dim tobjFileInfo As IO.FileInfo tobjFileInfo = New IO.FileInfo(ni_strFilePath) Try Try tobjIrDAClient.Connect(tobjIrDAEndPoint) Catch ex As Exception MessageBox.Show("IrDA Socket Connect 状态失败") Return False End Try '创建一个最简单的连接信息 tabytTempa = tobjOBEX_HEADERS.fnCreateConnectHeaderRequest(, , , m_i32OBEXMAXPacketSize) Try tobjIrDAClient.GetStream.Write(tabytTempa, 0, tabytTempa.Length) tobjIrDAClient.GetStream.Flush() Catch ex As Exception MessageBox.Show("OBEX_CONNECT信息发送失败") Application.DoEvents() Return False End Try If tobjIrDAClient.GetStream.CanRead = True Then Try tobjIrDAClient.GetStream.Read(tabytReceiveBuffer, 0, 7) Catch ex As Exception MessageBox.Show("接收OBEX_CONNECT响应消息失败") Return False End Try If tabytReceiveBuffer(0) <> cOBEX_SUCCESS Then MessageBox.Show("OBEX_CONNECT没接收到相应的响应") Return False End If End If '发送put包 'Name包 tabytTempa = tobjOBEX_HEADERS.fnCreateNameHeader(IO.Path.GetFileName(ni_strFilePath)) ti32Tempa = tabytTempa.Length 'lenght包 tabytTempb = tobjOBEX_HEADERS.fnCreateLengthHeader(tobjFileInfo.Length) ti32TempB = tabytTempb.Length '时间 tabytTempC = tobjOBEX_HEADERS.fnCreateTimeHeaderISO(Now) ReDim Preserve tabytTempa(tabytTempa.Length + tabytTempb.Length + tabytTempC.Length) tabytTempb.CopyTo(tabytTempa, ti32Tempa) tabytTempC.CopyTo(tabytTempa, ti32Tempa + ti32TempB) tabytTempa = tobjOBEX_HEADERS.fnCreatePutHeader(tabytTempa) '将名称、时间、文件长度等基本信息先发出去 Try tobjIrDAClient.GetStream.Write(tabytTempa, 0, tabytTempa.Length) Catch ex As Exception Return False End Try Try tobjIrDAClient.GetStream.Read(tabytReceiveBuffer, 0, tabytReceiveBuffer.Length) Catch ex As Exception '接收服务器端响应时失败 tobjIrDAClient.GetStream.Close() tobjIrDAClient.Close() Return False End Try If tabytReceiveBuffer(0) <> cOBEX_SUCCESS And tabytReceiveBuffer(0) <> cOBEX_CONTINUE Then '没有收到服务器的对应的响应信息 tobjIrDAClient.GetStream.Close() tobjIrDAClient.Close() Return False End If '开始正式发送文件的数据 Try tobjFileStream = IO.File.Open(ni_strFilePath, IO.FileMode.Open, IO.FileAccess.Read) tobjBinaryReader = New IO.BinaryReader(tobjFileStream) Catch ex As Exception MessageBox.Show("打开文件时出现错误") tobjIrDAClient.GetStream.Close() tobjIrDAClient.Close() Return False End Try '读出文件的数据放到body中,总长度必须少于最大发送长度-put标志的3字节-body标志的3字节 '最多可发送的文件信息长度 ti32SendPackageSpare = m_i32OBEXMAXSendBufferLen - 6 '循环读取文件的内容,并发送到服务方 For ti32LoopA = 1 To Math.Ceiling(tobjFileInfo.Length / ti32SendPackageSpare) Try tabytTempa = tobjBinaryReader.ReadBytes(ti32SendPackageSpare) Catch ex As Exception tobjIrDAClient.GetStream.Close() tobjIrDAClient.Close() Return False End Try '构建合适的put包并发送 tabytTempb = tobjOBEX_HEADERS.fnCreateBodyHeader(tabytTempa) 'body标志 tabytTempa = tobjOBEX_HEADERS.fnCreatePutHeader(tabytTempb) '开始发送 Try tobjIrDAClient.GetStream.Write(tabytTempa, 0, tabytTempa.Length) tobjIrDAClient.GetStream.Flush() Catch ex As Exception tobjIrDAClient.GetStream.Close() tobjIrDAClient.Close() Return False End Try Try tobjIrDAClient.GetStream.Read(tabytReceiveBuffer, 0, tabytReceiveBuffer.Length) Catch ex As Exception '接收服务器端响应时失败 tobjIrDAClient.GetStream.Close() tobjIrDAClient.Close() Return False End Try If tabytReceiveBuffer(0) <> cOBEX_SUCCESS And tabytReceiveBuffer(0) <> cOBEX_CONTINUE Then '没有收到服务器的对应的响应信息 tobjIrDAClient.GetStream.Close() tobjIrDAClient.Close() Return False End If Next '关闭文件 tobjBinaryReader.Close() tobjFileStream.Close() If ti32Tempa Then End If '文件数据已全部发送完毕,发送结束的final_put 包 'endbody包 tabytTempa = tobjOBEX_HEADERS.fnCreateEndOfBodyHeader(Nothing) 'putfinal tabytTempa = tobjOBEX_HEADERS.fnCreatePut_FinalHeaderResponse(tabytTempa) Try tobjIrDAClient.GetStream.Write(tabytTempa, 0, tabytTempa.Length) tobjIrDAClient.GetStream.Flush() Catch ex As Exception tobjIrDAClient.GetStream.Close() tobjIrDAClient.Close() Return False End Try Try tobjIrDAClient.GetStream.Read(tabytReceiveBuffer, 0, tabytReceiveBuffer.Length) Catch ex As Exception tobjIrDAClient.GetStream.Close() tobjIrDAClient.Close() Return False End Try If tabytReceiveBuffer(0) <> cOBEX_SUCCESS Then '没有收到服务器的对应的响应信息 tobjIrDAClient.GetStream.Close() tobjIrDAClient.Close() Return False End If '发送disconnect包 tabytTempa = tobjOBEX_HEADERS.fnCreateDisConnectHeaderRequest() Try tobjIrDAClient.GetStream.Write(tabytTempa, 0, tabytTempa.Length) tobjIrDAClient.GetStream.Flush() Catch ex As Exception tobjIrDAClient.GetStream.Close() tobjIrDAClient.Close() Return False End Try tobjIrDAClient.Close() Catch ex As Exception MessageBox.Show("过程发生不明错误") Return False End Try '代码能来到这儿就是正常完成任务了 MsgBox("红外操作成功") Return True End Function End Class clsOBEXHeaders.vb文件的代码: Public Class clsOBEXHeaders #Region "OBEX常数" Public Const cOBEX_VERSION As Byte = &H10 Public Const cOBEX_CONNECTFLAGS As Byte = &H0 Public Const cOBEX_CONNECT As Byte = &H80 Public Const cOBEX_DISCONNECT As Byte = &H81 Public Const cOBEX_PUT As Byte = &H2 Public Const cOBEX_NAME As Byte = &H1 Public Const cOBEX_PUT_FINAL As Byte = &H82 Public Const cOBEX_LENGTH As Byte = &HC3 Public Const cOBEX_BODY As Byte = &H48 Public Const cOBEX_END_OF_BODY As Byte = &H49 Public Const cOBEX_TIME_ISO As Byte = &H44 Public Const cOBEX_CONTINUE As Byte = &H90 Public Const cOBEX_SUCCESS As Byte = &HA0 #End Region #Region "类内私有函数" '将一个16位整数转为2个字节的数组,并存入参数中指定索引开始的字节数组处 Private Sub sbInt16ToByteArrayLowByteToHightPosition(ByVal ni_i16Convert As Int16, ByRef ni_abytDest() As Byte, ByVal ni_i32StartIndex As Int32) ni_abytDest(ni_i32StartIndex + 1) = ni_i16Convert And &HFF ni_abytDest(ni_i32StartIndex) = ni_i16Convert >> 8 End Sub ''将一个16位整数转为2个字节的数组,高位字放入高字节,并存入参数中指定索引开始的字节数组处 'Private Sub sbInt16ToByteArrayHightByteToHightPosition(ByVal ni_i16Convert As Int16, ByRef ni_abytDest() As Byte, ByVal ni_i32StartIndex As Int32) ' ni_abytDest(ni_i32StartIndex) = ni_i16Convert And &HFF ' ni_abytDest(ni_i32StartIndex + 1) = ni_i16Convert >> 8 'End Sub '将一个32位整数转为4个字节的数组,并存入参数中指定索引开始的字节数组处 Private Sub sbInt32ToByteArrayLowByteToHightPosition(ByVal ni_i32Convert As Int16, ByRef ni_abytDest() As Byte, ByVal ni_i32StartIndex As Int32) Dim ti32Tempa As Int32 = ni_i32Convert ni_abytDest(ni_i32StartIndex + 3) = ni_i32Convert And &HFF ni_abytDest(ni_i32StartIndex + 2) = (ni_i32Convert And &HFF00) >> 8 ni_abytDest(ni_i32StartIndex + 1) = (ni_i32Convert And &HFF0000) >> 16 ni_abytDest(ni_i32StartIndex) = (ti32Tempa And &HFF000000) >> 24 End Sub #End Region '创建一个新的ConnectHeader客户端请求信息 Public Function fnCreateConnectHeaderRequest(Optional ByVal ni_abytOtherHeaders As Byte() = Nothing, _ Optional ByVal ni_bytOBEX_Version As Byte = cOBEX_VERSION, _ Optional ByVal ni_bytFlags As Byte = cOBEX_CONNECTFLAGS, _ Optional ByVal ni_i16MaxPacketLength As Int16 = 255) As Byte() Dim tabytFuncReturn() As Byte If Not (ni_abytOtherHeaders Is Nothing) Then ReDim tabytFuncReturn(7 + ni_abytOtherHeaders.Length - 1) Else ReDim tabytFuncReturn(6) End If tabytFuncReturn(0) = cOBEX_CONNECT tabytFuncReturn(3) = ni_bytOBEX_Version tabytFuncReturn(4) = ni_bytFlags sbInt16ToByteArrayLowByteToHightPosition(ni_i16MaxPacketLength, tabytFuncReturn, 5) If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了 '加入connect包的长度 Call sbInt16ToByteArrayLowByteToHightPosition(7, tabytFuncReturn, 1) Else '还有其它头信息 ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 7) Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 7, tabytFuncReturn, 1) End If Return tabytFuncReturn End Function '创建一个新的ConnectHeader的服务器端响应信息数据 Public Function fnCreateConnectHeaderResponse(Optional ByVal ni_abytOtherHeaders As Byte() = Nothing, _ Optional ByVal ni_bytOBEX_Version As Byte = cOBEX_VERSION, _ Optional ByVal ni_bytFlags As Byte = cOBEX_CONNECTFLAGS, _ Optional ByVal ni_i16MaxPacketLength As Int16 = 255) As Byte() Dim tabytFuncReturn() As Byte If Not (ni_abytOtherHeaders Is Nothing) Then ReDim tabytFuncReturn(7 + ni_abytOtherHeaders.Length - 1) Else ReDim tabytFuncReturn(6) End If tabytFuncReturn(0) = cOBEX_SUCCESS tabytFuncReturn(3) = ni_bytOBEX_Version tabytFuncReturn(4) = ni_bytFlags sbInt16ToByteArrayLowByteToHightPosition(ni_i16MaxPacketLength, tabytFuncReturn, 5) If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了 '加入connect包的长度 Call sbInt16ToByteArrayLowByteToHightPosition(7, tabytFuncReturn, 1) Else '还有其它头信息 ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 7) Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 7, tabytFuncReturn, 1) End If Return tabytFuncReturn End Function '创建一个新的DisConnectHeader客户请求信息 Public Function fnCreateDisConnectHeaderRequest(Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte() Dim tabytFuncReturn() As Byte If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面 ReDim tabytFuncReturn(3 + ni_abytOtherHeaders.Length - 1) Else ReDim tabytFuncReturn(2) End If tabytFuncReturn(0) = cOBEX_DISCONNECT If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了 '加入包的长度 Call sbInt16ToByteArrayLowByteToHightPosition(3, tabytFuncReturn, 1) Else '还有其它头信息 ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3) Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 3, tabytFuncReturn, 1) End If Return tabytFuncReturn End Function '创建一个新的ContinueHeader信息 Public Function fnCreateContinueHeaderResponse(Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte() Dim tabytFuncReturn() As Byte If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面 ReDim tabytFuncReturn(3 + ni_abytOtherHeaders.Length - 1) Else ReDim tabytFuncReturn(2) End If tabytFuncReturn(0) = cOBEX_CONTINUE If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了 '加入包的长度 Call sbInt16ToByteArrayLowByteToHightPosition(3, tabytFuncReturn, 1) Else '还有其它头信息 ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3) Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 3, tabytFuncReturn, 1) End If Return tabytFuncReturn End Function '创建一个新的SuccessHeader信息 Public Function fnCreateSuccessHeaderResponse(Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte() Dim tabytFuncReturn() As Byte If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面 ReDim tabytFuncReturn(3 + ni_abytOtherHeaders.Length - 1) Else ReDim tabytFuncReturn(2) End If tabytFuncReturn(0) = cOBEX_SUCCESS If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了 '加入包的长度 Call sbInt16ToByteArrayLowByteToHightPosition(3, tabytFuncReturn, 1) Else '还有其它头信息 ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3) Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 3, tabytFuncReturn, 1) End If Return tabytFuncReturn End Function '创建一个新的TimeHeader信息 Public Function fnCreateTimeHeaderISO(ByVal ni_dtmDate As Date, Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte() Dim tabytFuncReturn() As Byte Dim tstrDate As String Dim tabytTempa() As Byte 'tstrDate = ni_dtmDate.Year & "." & ni_dtmDate.Month & "." & ni_dtmDate.Day & "T" & _ ' ni_dtmDate.Hour & ":" & ni_dtmDate.Minute & ":" & ni_dtmDate.Second & "Z" & ControlChars.NullChar tstrDate = String.Format("{0:yyyy/MM/dd\THH:mm:ss\Z}", ni_dtmDate) & ControlChars.NullChar tabytTempa = Text.Encoding.BigEndianUnicode.GetBytes(tstrDate) If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面 ReDim tabytFuncReturn(3 + tabytTempa.Length + ni_abytOtherHeaders.Length - 1) Else ReDim tabytFuncReturn(2 + tabytTempa.Length) End If tabytFuncReturn(0) = cOBEX_TIME_ISO tabytTempa.CopyTo(tabytFuncReturn, 3) '将时间字节串放入返回数组中 If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了 '加入包的长度 Call sbInt16ToByteArrayLowByteToHightPosition(3 + tabytTempa.Length, tabytFuncReturn, 1) Else '还有其它头信息 ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3) Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 3 + tabytTempa.Length, tabytFuncReturn, 1) End If Return tabytFuncReturn End Function '创建一个新的BodyHeader信息 Public Function fnCreateBodyHeader(ByVal ni_abytBodyContent() As Byte, Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte() Dim tabytFuncReturn() As Byte Dim tabytTempa As Byte Dim ti32BodyContentLength As Int32 = 0 '定义合适大小的数组以容纳bodyheader的内容 If Not (ni_abytBodyContent Is Nothing) Then '有body的内容 If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面 ReDim tabytFuncReturn(3 + ni_abytBodyContent.Length + ni_abytOtherHeaders.Length - 1) ni_abytBodyContent.CopyTo(tabytFuncReturn, 3) ni_abytOtherHeaders.CopyTo(tabytFuncReturn, ni_abytBodyContent.Length + 3) Else ReDim tabytFuncReturn(2 + ni_abytBodyContent.Length) ni_abytBodyContent.CopyTo(tabytFuncReturn, 3) End If Else '没有body的内容 If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面 ReDim tabytFuncReturn(3 + ni_abytOtherHeaders.Length - 1) ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3) Else ReDim tabytFuncReturn(2) End If End If tabytFuncReturn(0) = cOBEX_BODY '加入包的长度 Call sbInt16ToByteArrayLowByteToHightPosition(tabytFuncReturn.Length, tabytFuncReturn, 1) Return tabytFuncReturn End Function '创建一个新的EndOfBodyHeader信息 Public Function fnCreateEndOfBodyHeader(ByVal ni_abytBodyContent() As Byte, Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte() Dim tabytFuncReturn() As Byte Dim tabytTempa As Byte Dim ti32BodyContentLength As Int32 = 0 If Not (ni_abytBodyContent Is Nothing) Then If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面 ReDim tabytFuncReturn(3 + ni_abytBodyContent.Length + ni_abytOtherHeaders.Length - 1) ni_abytBodyContent.CopyTo(tabytFuncReturn, 3) ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3 + ni_abytBodyContent.Length) Else ReDim tabytFuncReturn(2 + ni_abytBodyContent.Length) ni_abytBodyContent.CopyTo(tabytFuncReturn, 3) End If Else '没有body内容 If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面 ReDim tabytFuncReturn(3 + ni_abytOtherHeaders.Length - 1) ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3) Else ReDim tabytFuncReturn(2) End If End If '加入包的长度 Call sbInt16ToByteArrayLowByteToHightPosition(tabytFuncReturn.Length, tabytFuncReturn, 1) tabytFuncReturn(0) = cOBEX_END_OF_BODY Return tabytFuncReturn End Function '创建一个新的LengthHeader信息 Public Function fnCreateLengthHeader(ByVal ni_i32Length As Int32) As Byte() Dim tabytFuncReturn() As Byte ReDim tabytFuncReturn(4) tabytFuncReturn(0) = cOBEX_LENGTH '加入包的内容 Call sbInt32ToByteArrayLowByteToHightPosition(ni_i32Length, tabytFuncReturn, 1) Return tabytFuncReturn End Function '创建一个新的NameHeader信息 Public Function fnCreateNameHeader(ByVal ni_strName As String, Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte() Dim tabytFuncReturn() As Byte Dim tabytTempa() As Byte Dim ti32BodyContentLength As Int32 = 0 tabytTempa = Text.Encoding.BigEndianUnicode.GetBytes(ni_strName & ControlChars.NullChar) If ni_strName <> "" Then If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面 ReDim tabytFuncReturn(3 + tabytTempa.Length + ni_abytOtherHeaders.Length - 1) Else '没有其它数据包附后 ReDim tabytFuncReturn(2 + tabytTempa.Length) End If Else If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面 ReDim tabytFuncReturn(3 + ni_abytOtherHeaders.Length - 1) Else ReDim tabytFuncReturn(2) End If End If tabytFuncReturn(0) = cOBEX_NAME If ni_strName <> "" Then If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了 '加入包的长度 Call sbInt16ToByteArrayLowByteToHightPosition(3 + tabytTempa.Length, tabytFuncReturn, 1) Else '还有其它头信息 ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3) Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + tabytTempa.Length + 3, tabytFuncReturn, 1) End If tabytTempa.CopyTo(tabytFuncReturn, 3) Else '没有实际的body数据 If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了 '加入包的长度 Call sbInt16ToByteArrayLowByteToHightPosition(3, tabytFuncReturn, 1) Else '还有其它头信息 ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3) Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 3, tabytFuncReturn, 1) End If End If Return tabytFuncReturn End Function '创建一个新的PutHeader信息 Public Function fnCreatePutHeader(Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte() Dim tabytFuncReturn() As Byte If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面 ReDim tabytFuncReturn(3 + ni_abytOtherHeaders.Length - 1) Else ReDim tabytFuncReturn(2) End If tabytFuncReturn(0) = cOBEX_PUT If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了 '加入包的长度 Call sbInt16ToByteArrayLowByteToHightPosition(3, tabytFuncReturn, 1) Else '还有其它头信息 ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3) Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 3, tabytFuncReturn, 1) End If Return tabytFuncReturn End Function '创建一个新的PutHeader信息 Public Function fnCreatePut_FinalHeaderResponse(Optional ByVal ni_abytOtherHeaders As Byte() = Nothing) As Byte() Dim tabytFuncReturn() As Byte If Not (ni_abytOtherHeaders Is Nothing) Then '有其它的数据包要附在后面 ReDim tabytFuncReturn(3 + ni_abytOtherHeaders.Length - 1) Else ReDim tabytFuncReturn(2) End If tabytFuncReturn(0) = cOBEX_PUT_FINAL If ni_abytOtherHeaders Is Nothing Then '没有其它的头信息了 '加入包的长度 Call sbInt16ToByteArrayLowByteToHightPosition(3, tabytFuncReturn, 1) Else '还有其它头信息 ni_abytOtherHeaders.CopyTo(tabytFuncReturn, 3) Call sbInt16ToByteArrayLowByteToHightPosition(ni_abytOtherHeaders.Length + 3, tabytFuncReturn, 1) End If Return tabytFuncReturn End Function End Class |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者