Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  2. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  3.  
  4. Private s_ServerIn  As New IoBuffer
  5. Private s_ServerOut As New IoBuffer
  6. Private s_ClientIn  As New IoBuffer
  7. Private s_ClientOut As New IoBuffer
  8.  
  9. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  10. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  11.  
  12. Public Sub OnServerAccept(ByVal Selector As IoSocketSelectorDecl, ByVal Socket As Long)
  13.     Debug.Print "Server::OnAccept(" + CStr(Socket) + ") " + Selector.GetRemoteAddress(Socket)
  14.    
  15.     Call Selector.Read(Socket, s_ServerIn.Data, s_ServerIn.Capacity)
  16.     Call Selector.Write(Socket, s_ServerOut.Data, s_ServerOut.Capacity)
  17. End Sub
  18.  
  19. Public Sub OnServerDisconnect(ByVal Selector As IoSocketSelectorDecl, ByVal Socket As Long, ByVal Reason As Long)
  20.     Debug.Print "Server::OnDisconnect(" + CStr(Reason) + ")"
  21.    
  22.     s_ServerIn.Reset: s_ServerOut.Reset
  23. End Sub
  24.  
  25. Public Sub OnServerError(ByVal Selector As IoSocketSelectorDecl, ByVal Reason As Long)
  26.     Debug.Print "Server::OnError(" + CStr(Reason) + ")"
  27. End Sub
  28.  
  29. Public Sub OnServerRead(ByVal Selector As IoSocketSelectorDecl, ByVal Socket As Long, ByVal Length As Long)
  30.     Debug.Print "Server::OnRead(" + CStr(Length) + ")"
  31.  
  32.     Call s_ClientIn.Commit(Length)
  33.    
  34.     ' Parser in here
  35.    Call s_ClientIn.Consume(Length)  ' Skip
  36.    
  37.     Call Selector.Read(Socket, s_ServerIn.Data, s_ServerOut.Capacity)
  38.    
  39.     s_Incoming = s_Incoming + Length
  40. End Sub
  41.  
  42. Public Sub OnServerWrite(ByVal Selector As IoSocketSelectorDecl, ByVal Socket As Long, ByVal Length As Long)
  43.     Debug.Print "Server::OnWrite(" + CStr(Length) + ")"
  44.  
  45.     Call Selector.Write(Socket, s_ServerIn.Data, s_ServerOut.Capacity)
  46.    
  47.     s_Outgoing = s_Outgoing + Length
  48. End Sub
  49.  
  50. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  51. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  52.  
  53. Public Sub OnClientConnect(ByVal Socket As IoSocketDecl)
  54.     Debug.Print "OnClientConnect"
  55.    
  56.     Call Socket.Write(s_ClientOut.Data, s_ClientOut.Capacity)
  57.     Call Socket.Read(s_ClientIn.Data, 1)
  58. End Sub
  59.  
  60. Public Sub OnClientDisconnect(ByVal Socket As IoSocketDecl, ByVal Reason As Long)
  61.     Debug.Print "OnClientDisconnect"
  62. End Sub
  63.  
  64. Public Sub OnClientRead(ByVal Socket As IoSocketDecl, ByVal Length As Long)
  65.     Call s_ClientIn.Commit(Length)
  66.    
  67.     ' Parser in here
  68.    Call s_ClientIn.Consume(Length)  ' Skip
  69.    
  70.     Call Socket.Read(s_ClientIn.Data, s_ClientIn.Capacity)
  71. End Sub
  72.  
  73. Public Sub OnClientWrite(ByVal Socket As IoSocketDecl, ByVal Length As Long)
  74.     Debug.Print "Write(" + CStr(Length) + ")"
  75.    
  76.     Call Socket.Write(s_ClientOut.Data, s_ClientOut.Capacity)
  77. End Sub
  78.  
  79. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  80. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  81.  
  82. Public Sub Main()
  83.     s_ClientOut.Capacity = 64000
  84.    
  85.     Dim Selector As New IoSocketSelector
  86.    
  87.     Selector.OnAccept AddressOf OnServerAccept
  88.     Selector.OnDisconnect AddressOf OnServerDisconnect
  89.     Selector.OnError AddressOf OnServerError
  90.     Selector.OnRead AddressOf OnServerRead
  91.     Selector.OnWrite AddressOf OnServerWrite
  92.     Selector.Listen 7666
  93.  
  94.     Dim Client(0 To 99) As New IoSocket
  95.      
  96.     For I = 0 To 99
  97.        
  98.         Client(I).OnConnect AddressOf OnClientConnect
  99.         Client(I).OnDisconnect AddressOf OnClientDisconnect
  100.         Client(I).OnRead AddressOf OnClientRead
  101.         Client(I).OnWrite AddressOf OnClientWrite
  102.         Client(I).Connect "190.0.163.20", 7668
  103.    
  104.     Next I
  105.  
  106.     While (True)
  107.         DoEvents
  108.        
  109.         Call Io.Poll(True)
  110.     Wend
  111.    
  112. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement