📄 csocketmaster.cls
字号:
Exit Sub
End If
Else 'If we use UDP we create a socket if there isn't one yet
If Not SocketExists Then Exit Sub
If Not BindInternal Then Exit Sub
m_enmState = sckOpen: If ShowDebug Then Debug.Print "STATE: sckOpen"
End If
'We need to convert data variant into a byte array
Select Case varType(data)
Case vbString
Dim strdata As String
strdata = CStr(data)
If Len(strdata) = 0 Then Exit Sub
ReDim arrData(Len(strdata) - 1)
arrData() = StrConv(strdata, vbFromUnicode)
Case vbArray + vbByte
Dim strArray As String
strArray = StrConv(data, vbUnicode)
If Len(strArray) = 0 Then Exit Sub
arrData() = StrConv(strArray, vbFromUnicode)
Case vbBoolean
Dim blnData As Boolean
blnData = CBool(data)
ReDim arrData(LenB(blnData) - 1)
api_CopyMemory arrData(0), blnData, LenB(blnData)
Case vbByte
Dim bytData As Byte
bytData = CByte(data)
ReDim arrData(LenB(bytData) - 1)
api_CopyMemory arrData(0), bytData, LenB(bytData)
Case vbCurrency
Dim curData As Currency
curData = CCur(data)
ReDim arrData(LenB(curData) - 1)
api_CopyMemory arrData(0), curData, LenB(curData)
Case vbDate
Dim datData As Date
datData = CDate(data)
ReDim arrData(LenB(datData) - 1)
api_CopyMemory arrData(0), datData, LenB(datData)
Case vbDouble
Dim dblData As Double
dblData = CDbl(data)
ReDim arrData(LenB(dblData) - 1)
api_CopyMemory arrData(0), dblData, LenB(dblData)
Case vbInteger
Dim intData As Integer
intData = CInt(data)
ReDim arrData(LenB(intData) - 1)
api_CopyMemory arrData(0), intData, LenB(intData)
Case vbLong
Dim lngData As Long
lngData = CLng(data)
ReDim arrData(LenB(lngData) - 1)
api_CopyMemory arrData(0), lngData, LenB(lngData)
Case vbSingle
Dim sngData As Single
sngData = CSng(data)
ReDim arrData(LenB(sngData) - 1)
api_CopyMemory arrData(0), sngData, LenB(sngData)
Case Else
Err.Raise sckUnsupported, "CSocketMaster.SendData", "Unsupported variant type."
End Select
'if there's already something in the buffer that means we are
'already sending data, so we put the new data in the buffer
'and exit silently
If Len(m_strSendBuffer) > 0 Then
m_strSendBuffer = m_strSendBuffer + StrConv(arrData(), vbUnicode)
Exit Sub
Else
m_strSendBuffer = m_strSendBuffer + StrConv(arrData(), vbUnicode)
End If
'send the data
SendBufferedData
End Sub
'Check which protocol we are using to decide which
'function should handle the data sending.
Private Sub SendBufferedData()
If m_enmProtocol = sckTCPProtocol Then
SendBufferedDataTCP
Else
SendBufferedDataUDP
End If
End Sub
'Send buffered data if we are using UDP protocol.
Private Sub SendBufferedDataUDP()
Dim lngAddress As Long
Dim udtSockAddr As sockaddr_in
Dim arrData() As Byte
Dim lngBufferLength As Long
Dim lngResult As Long
Dim lngErrorCode As Long
Dim strTemp As String
lngAddress = ResolveIfHostnameSync(m_strRemoteHost, strTemp, lngErrorCode)
If lngErrorCode <> 0 Then
m_strSendBuffer = ""
If lngErrorCode = WSAEAFNOSUPPORT Then
Err.Raise lngErrorCode, "CSocketMaster.SendBufferedDataUDP", GetErrorDescription(lngErrorCode)
Else
Err.Raise sckInvalidArg, "CSocketMaster.SendBufferedDataUDP", "Invalid argument"
End If
End If
With udtSockAddr
.sin_addr = lngAddress
.sin_family = AF_INET
.sin_port = api_htons(modSocketMaster.UnsignedToInteger(m_lngRemotePort))
End With
lngBufferLength = Len(m_strSendBuffer)
arrData() = StrConv(m_strSendBuffer, vbFromUnicode)
m_strSendBuffer = ""
lngResult = api_sendto(m_lngSocketHandle, arrData(0), lngBufferLength, 0&, udtSockAddr, LenB(udtSockAddr))
If lngResult = SOCKET_ERROR Then
lngErrorCode = Err.LastDllError
m_enmState = sckError: If ShowDebug Then Debug.Print "STATE: sckError"
Dim blnCancelDisplay As Boolean
blnCancelDisplay = True
RaiseEvent Error(lngErrorCode, GetErrorDescription(lngErrorCode), 0, "CSocketMaster.SendBufferedDataUDP", "", 0, blnCancelDisplay)
If blnCancelDisplay = False Then MsgBox GetErrorDescription(lngErrorCode), vbOKOnly, "CSocketMaster.SendBufferedDataUDP"
End If
End Sub
'Send buffered data if we are using TCP protocol.
Private Sub SendBufferedDataTCP()
Dim arrData() As Byte
Dim lngBufferLength As Long
Dim lngResult As Long
Dim lngTotalSent As Long
Do Until lngResult = SOCKET_ERROR Or Len(m_strSendBuffer) = 0
lngBufferLength = Len(m_strSendBuffer)
If lngBufferLength > m_lngSendBufferLen Then
lngBufferLength = m_lngSendBufferLen
arrData() = StrConv(Left$(m_strSendBuffer, m_lngSendBufferLen), vbFromUnicode)
Else
arrData() = StrConv(m_strSendBuffer, vbFromUnicode)
End If
lngResult = api_send(m_lngSocketHandle, arrData(0), lngBufferLength, 0&)
If lngResult = SOCKET_ERROR Then
Dim lngErrorCode As Long
lngErrorCode = Err.LastDllError
If lngErrorCode = WSAEWOULDBLOCK Then
If ShowDebug Then Debug.Print "WARNING: Send buffer full, waiting..."
If lngTotalSent > 0 Then RaiseEvent SendProgress(lngTotalSent, Len(m_strSendBuffer))
Else
m_enmState = sckError: If ShowDebug Then Debug.Print "STATE: sckError"
Dim blnCancelDisplay As Boolean
blnCancelDisplay = True
RaiseEvent Error(lngErrorCode, GetErrorDescription(lngErrorCode), 0, "CSocketMaster.SendBufferedData", "", 0, blnCancelDisplay)
If blnCancelDisplay = False Then MsgBox GetErrorDescription(lngErrorCode), vbOKOnly, "CSocketMaster.SendBufferedData"
End If
Else
If ShowDebug Then Debug.Print "OK Bytes sent: " & lngResult
lngTotalSent = lngTotalSent + lngResult
If Len(m_strSendBuffer) > lngResult Then
m_strSendBuffer = Mid$(m_strSendBuffer, lngResult + 1)
Else
If ShowDebug Then Debug.Print "OK Finished SENDING"
m_strSendBuffer = ""
Dim lngTemp As Long
lngTemp = lngTotalSent
lngTotalSent = 0
RaiseEvent SendProgress(lngTemp, 0)
RaiseEvent SendComplete
End If
End If
Loop
End Sub
'This function retrieves data from the Winsock buffer
'into the class local buffer. The function returns number
'of bytes retrieved (received).
Private Function RecvDataToBuffer() As Long
Dim arrBuffer() As Byte
Dim lngBytesReceived As Long
Dim strBuffTemporal As String
ReDim arrBuffer(m_lngRecvBufferLen - 1)
lngBytesReceived = api_recv(m_lngSocketHandle, arrBuffer(0), m_lngRecvBufferLen, 0&)
If lngBytesReceived = SOCKET_ERROR Then
m_enmState = sckError: If ShowDebug Then Debug.Print "STATE: sckError"
Dim lngErrorCode As Long
lngErrorCode = Err.LastDllError
Err.Raise lngErrorCode, "CSocketMaster.RecvDataToBuffer", GetErrorDescription(lngErrorCode)
ElseIf lngBytesReceived > 0 Then
strBuffTemporal = StrConv(arrBuffer(), vbUnicode)
m_strRecvBuffer = m_strRecvBuffer & Left$(strBuffTemporal, lngBytesReceived)
RecvDataToBuffer = lngBytesReceived
End If
End Function
'Retrieves some socket options.
'If it is an UDP socket also sets SO_BROADCAST option.
Private Sub ProcessOptions()
Dim lngResult As Long
Dim lngBuffer As Long
Dim lngErrorCode As Long
If m_enmProtocol = sckTCPProtocol Then
lngResult = api_getsockopt(m_lngSocketHandle, SOL_SOCKET, SO_RCVBUF, lngBuffer, LenB(lngBuffer))
If lngResult = SOCKET_ERROR Then
lngErrorCode = Err.LastDllError
Err.Raise lngErrorCode, "CSocketMaster.ProcessOptions", GetErrorDescription(lngErrorCode)
Else
m_lngRecvBufferLen = lngBuffer
End If
lngResult = api_getsockopt(m_lngSocketHandle, SOL_SOCKET, SO_SNDBUF, lngBuffer, LenB(lngBuffer))
If lngResult = SOCKET_ERROR Then
lngErrorCode = Err.LastDllError
Err.Raise lngErrorCode, "CSocketMaster.ProcessOptions", GetErrorDescription(lngErrorCode)
Else
m_lngSendBufferLen = lngBuffer
End If
Else
lngBuffer = 1
lngResult = api_setsockopt(m_lngSocketHandle, SOL_SOCKET, SO_BROADCAST, lngBuffer, LenB(lngBuffer))
lngResult = api_getsockopt(m_lngSocketHandle, SOL_SOCKET, SO_MAX_MSG_SIZE, lngBuffer, LenB(lngBuffer))
If lngResult = SOCKET_ERROR Then
lngErrorCode = Err.LastDllError
Err.Raise lngErrorCode, "CSocketMaster.ProcessOptions", GetErrorDescription(lngErrorCode)
Else
m_lngRecvBufferLen = lngBuffer
m_lngSendBufferLen = lngBuffer
End If
End If
If ShowDebug Then Debug.Print "Winsock buffer size for sends: " & m_lngRecvBufferLen
If ShowDebug Then Debug.Print "Winsock buffer size for receives: " & m_lngSendBufferLen
End Sub
Public Sub GetData(ByRef data As Variant, Optional varType As Variant, Optional maxLen As Variant)
If m_enmProtocol = sckTCPProtocol Then
If m_enmState <> sckConnected And Not m_blnAcceptClass Then
Err.Raise sckBadState, "CSocketMaster.GetData", "Wrong protocol or connection state for the requested transaction or request"
Exit Sub
End If
Else
If m_enmState <> sckOpen Then
Err.Raise sckBadState, "CSocketMaster.GetData", "Wrong protocol or connection state for the requested transaction or request"
Exit Sub
End If
If GetBufferLenUDP = 0 Then Exit Sub
End If
If Not IsMissing(maxLen) Then
If IsNumeric(maxLen) Then
If CLng(maxLen) < 0 Then
Err.Raise sckInvalidArg, "CSocketMaster.GetData", "The argument passed to a function was not in the correct format or in the specified range."
End If
Else
If m_enmProtocol = sckTCPProtocol Then
maxLen = Len(m_strRecvBuffer)
Else
maxLen = GetBufferLenUDP
End If
End If
End If
Dim lngBytesRecibidos As Long
lngBytesRecibidos = RecvData(data, False, varType, maxLen)
If ShowDebug Then Debug.Print "OK Bytes obtained from buffer: " & lngBytesRecibidos
End Sub
Public Sub PeekData(ByRef data As Variant, Optional varType As Variant, Optional maxLen As Variant)
If m_enmProtocol = sckTCPProtocol Then
If m_enmState <> sckConnected Then
Err.Raise sckBadState, "CSocketMaster.PeekData", "Wrong protocol or connection state for the requested transaction or request"
Exit Sub
End If
Else
If m_enmState <> sckOpen Then
Err.Raise sckBadState, "CSocketMaster.PeekData", "Wrong protocol or connection state for the requested transaction or request"
Exit Sub
End If
If GetBufferLenUDP = 0 Then Exit Sub
End If
If Not IsMissing(maxLen) Then
If IsNumeric(maxLen) Then
If CLng(maxLen) < 0 Then
Err.Raise sckInvalidArg, "CSocketMaster.PeekData", "The argument passed to a function was not in the correct format or in the specified range."
End If
Else
If m_enmProtocol = sckTCPProtocol Then
maxLen = Len(m_strRecvBuffer)
Else
maxLen = GetBufferLenUDP
End If
End If
End If
Dim lngBytesRecibidos As Long
lngBytesRecibidos = RecvData(data, True, varType, maxLen)
If ShowDebug Then Debug.Print "OK Bytes obtained from buffer: " & lngBytesRecibidos
End Sub
'This function is to retrieve data from the buffer. If we are using TCP
'then the data is retrieved from a local buffer (m_strRecvBuffer). If we
'are using UDP the data is retrieved from winsock buffer.
'It can be called by two public methods of the class - GetData and PeekData.
'Behavior of the function is defined by the blnPeek argument. If a value of
'that argument is TRUE, the function returns number of bytes in the
'buffer, and copy data from that buffer into the data argument.
'If a value of the blnPeek is FALSE, then this function returns number of
'bytes received, and move data from the buffer into the data
'argument. MOVE means that data will be removed from the buffer.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -