home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD112761132000.psc / Public_Functions.bas next >
Encoding:
BASIC Source File  |  2000-11-04  |  6.5 KB  |  233 lines

  1. Attribute VB_Name = "Public_Functions"
  2. Option Explicit
  3.  
  4. 'These two functions (with the accompanying constant and variable) are
  5. 'used to call the WindowProc function below. These declarations
  6. 'can be used to tell the OS to call the function, when certain
  7. 'Winsock Events occur.
  8. Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  9. Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal wndrpcPrev As Long, ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  10. Public Const GWL_WNDPROC = (-4)
  11. Public OldWndProc As Long
  12.  
  13. 'These two variables are used to raise events in the user control
  14. Private UC As New Collection
  15. Private MaxUCCount As Integer
  16.  
  17. 'These four variables are used to allow us to use encryption on send and
  18. 'receive
  19. Public CryptionObject As Object
  20. Public ICanUseCryptionObject As Boolean
  21. Public IShouldUseCryptionObject() As Boolean
  22. Public CryptionKey() As String
  23.  
  24. 'These two variables are used for tracking current states
  25. Public WinsockStates(9) As String
  26. Public CurrentState() As Integer
  27.  
  28. 'These three variables are used to track sockets
  29. Public m_lngSocks() As Long
  30. Public m_intSocketAsync() As Integer
  31. Public m_intMaxSockCount As Integer
  32. Public m_intConnectionsAlert As Integer
  33. '
  34.  
  35. 'Returns the socket stack index for the specified socketID.
  36. 'If the socket ID does not exist in the stack, -1 is returned.
  37. Public Function GetIndexFromsID(SocketID As Long) As Integer
  38.  
  39.   Dim x As Integer
  40.   
  41.   For x = 1 To m_intMaxSockCount
  42.     If m_lngSocks(x) = SocketID Then
  43.       GetIndexFromsID = x
  44.       Exit Function
  45.     End If
  46.   Next x
  47.   
  48.   GetIndexFromsID = -1
  49.  
  50. End Function
  51.  
  52. 'An internal function for delays. The WaitTime should be specified in
  53. 'seconds. If waittime is not passed, a value of 1 second is used.
  54. Public Function WaitJustOneSecond(Optional WaitTime As Single = 1) As Boolean
  55.  
  56.   Dim sTimer As Variant
  57.   
  58.   sTimer = Timer
  59.   
  60.   Do Until Timer > sTimer + WaitTime
  61.     DoEvents
  62.   Loop
  63.   
  64.   WaitJustOneSecond = True
  65.  
  66. End Function
  67.  
  68. 'This sub is used at start up, to reference the user control
  69. 'from within this function.
  70. Public Function SetControlHost(ByVal ControlInstance As TTOSock) As String
  71.   
  72.   Dim objTTOSock As TTOSock
  73.   Dim NewKey As String
  74.   
  75.   'This will ensure a unique key
  76.   NewKey = "a" & UC.Count + 1
  77.   
  78.   Set objTTOSock = ControlInstance
  79.   UC.Add objTTOSock, NewKey
  80.   
  81.   'If the count is larger than the Maximum Count, we need to
  82.   'increase the maximum count so that we are sure that we will
  83.   'be able to raise events to each instance.
  84.   If UC.Count > MaxUCCount Then MaxUCCount = UC.Count
  85.   
  86.   Set objTTOSock = Nothing
  87.   Set ControlInstance = Nothing
  88.   
  89.   SetControlHost = NewKey
  90.       
  91. End Function
  92.  
  93. 'This function is called by the OS, when Winsock events are
  94. 'raised. lParam contains the event or error code, wParam contains
  95. 'the Socket ID.
  96. Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  97.     
  98.   'Under Windows NT Server 4.0 with SP6 I found that when we were trying
  99.   'to close the control, a value continued to loop through WindowProc until
  100.   'a stack over flow occured. After testing, I found that in all instances,
  101.   'no matter the the value of uMsg, lParam was 0. I am assuming that
  102.   'this is a garbage message that is floating around looking to be destroyed
  103.   'but I can't prove it. Everything still appears to be working fine, but
  104.   'you should watch out for error caused because of this.
  105.   If lParam = 0 Then Exit Function
  106.   
  107.   'We only want to look at messages that are addressed to us,
  108.   'so we check to see if the message number matches our designated
  109.   'number. We designated the number 4025 when we set up the
  110.   'messaging system.
  111.     
  112.   If uMsg > 4025 And uMsg < 4026 + MaxUCCount Then
  113.       
  114.     Dim WSAEvent As Long
  115.     Dim WSAError As Long
  116.     Dim TempUC As TTOSock
  117.         
  118.     'We need to create a useable instance of TTOSock
  119.     Set TempUC = UC.Item("a" & uMsg - 4025)
  120.     
  121.     'Checks for errors and events
  122.     WSAEvent = WSAGetSelectEvent(lParam)
  123.     WSAError = WSAGetAsyncError(lParam)
  124.       
  125.     'Deals with each event
  126.     Select Case WSAEvent
  127.       Case FD_ACCEPT
  128.                 
  129.         TempUC.RaiseConnectionRequest wParam
  130.               
  131.       Case FD_READ
  132.           
  133.         ReceiveDataNew wParam, "a" & uMsg - 4025
  134.         
  135.       Case FD_CONNECT
  136.         
  137.          TempUC.RaiseConnected wParam
  138.         
  139.       Case FD_CLOSE
  140.          
  141.         TempUC.RaisePeerClosing wParam
  142.            
  143.       Case FD_WRITE
  144.         
  145.   
  146.         
  147.       Case FD_OOB
  148.      
  149.     End Select
  150.     
  151.   Else
  152.     
  153.     'Passes on the event.
  154.     WindowProc = CallWindowProc(OldWndProc, hWnd, uMsg, wParam, ByVal lParam)
  155.  
  156.   End If
  157.   
  158.   Set TempUC = Nothing
  159.  
  160. End Function
  161.  
  162. 'This function checks for new data on the specified socket
  163. Private Function ReceiveDataNew(SocketID As Long, UCKey As String)
  164.  
  165.   Dim RecvBuffer As String
  166.   Dim fixstr As String * 1024
  167.   Dim RetByteErr As Integer
  168.   fixstr = ""
  169.   RecvBuffer = ""
  170.    
  171.   'Attempts to receive data from the socket
  172.   RetByteErr = recv(SocketID, fixstr, 1024, 0)
  173.    
  174.   'Pick the info out of the junk
  175.   If RetByteErr < 0 Then
  176.     'HandleError
  177.     Exit Function
  178.   ElseIf RetByteErr = 0 Then
  179.     'Connection was closed
  180.     Exit Function
  181.   Else
  182.     RecvBuffer = Left$(fixstr, RetByteErr)
  183.   End If
  184.   
  185.   If RecvBuffer <> "" Then
  186.     'Raises the new data arrival event
  187.     Dim TempUC As TTOSock
  188.     Set TempUC = UC.Item(UCKey)
  189.     If ICanUseCryptionObject And IShouldUseCryptionObject(GetIndexFromsID(SocketID)) Then RecvBuffer = CryptionObject.Decrypt(RecvBuffer, CryptionKey(GetIndexFromsID(SocketID)))
  190.     TempUC.RaiseDataArrival SocketID, RecvBuffer
  191.     Set TempUC = Nothing
  192.   End If
  193.     
  194. End Function
  195.  
  196. 'This function destroys the UC object that was created in order to
  197. 'access controls in the usercontrol. This method must be called
  198. 'whenever we are attempting to destroy the user control. This can be
  199. 'a deadly circular reference.
  200. Public Sub CleanUp(UCKey As String)
  201.   
  202.   On Error Resume Next
  203.   UC.Remove UCKey
  204.       
  205. End Sub
  206.  
  207.   
  208. Public Sub CleanUpAll()
  209.  
  210.   Dim x As Integer
  211.   
  212.   On Error Resume Next
  213.   
  214.   For x = UC.Count To 0 Step -1
  215.     UC.Remove x
  216.   Next x
  217.  
  218. End Sub
  219.  
  220. Public Function ResolveIPtoNBO(IP As String) As Long
  221.  
  222.   Dim NBO As Long
  223.   
  224.   NBO = inet_addr(IP)
  225.   
  226.   If NBO = -1 Then NBO = GetHostByNameAlias(IP)
  227.    
  228.   ResolveIPtoNBO = NBO
  229.   
  230. End Function
  231.   
  232.   
  233.