Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.18 KB | None | 0 0
  1. ''' <summary>
  2. ''' Logging Level enums
  3. ''' </summary>
  4. ''' <remarks></remarks>
  5. Public Enum Logging_Level
  6.     None = 0
  7.     Normal = 1
  8.     IncludeDebug = 2
  9.     SystemWarnings = 3
  10.     Exceptions = 4
  11. End Enum
  12.  
  13. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  14. ''''' ArkMessenger TCPV3.vb
  15. ''''' Version 3.0
  16. ''''' Developer: Adam Sears
  17. ''''' Rewrite: #3
  18. ''''' Changelog:
  19. '''''
  20. ''''' 11/08/10
  21. ''''' Expanded the powers of the CManager Class to handle most Client interactions
  22. ''''' Integrated the handlers of various structures that the system will use.
  23. '''''
  24. ''''' 11/07/10
  25. ''''' First successful test of the code. Added in Chatroom Manager (CRManager)
  26. '''''
  27. ''''' 11/02/10
  28. ''''' Cleaned up the [Client] Class code.
  29. ''''' Finalized some Server routines
  30. '''''
  31. ''''' 10/25/10
  32. ''''' Implemented the Client Manager (CManager)
  33. ''''' Implemented the Client Saver (C_SaveLoad)
  34. ''''' Cleaned up TCPV3 base class.
  35. ''''' Cleaned up Server base class.
  36. '''''
  37. ''''' 10/20/10
  38. ''''' Started and finished TCPV3 base class.
  39. ''''' Started and finished Server base class.
  40. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  41.  
  42. ''' <summary>
  43. ''' Handles all low level server side incoming messages.
  44. ''' </summary>
  45. ''' <remarks></remarks>
  46. Public Class TCPV3
  47.  
  48.     ''' <summary>
  49.     ''' Returns the port the TCPV3 Class is listening on.
  50.     ''' </summary>
  51.     ''' <value></value>
  52.     ''' <returns></returns>
  53.     ''' <remarks></remarks>
  54.     Public ReadOnly Property Port As Integer
  55.         Get
  56.             Return _port
  57.         End Get
  58.     End Property
  59.  
  60.     ''' <summary>
  61.     ''' This sub is called whenever an Incoming Connection is established.
  62.     ''' </summary>
  63.     ''' <param name="ir">The IAsyncResult Data.</param>
  64.     ''' <remarks></remarks>
  65.     Public Overridable Sub OnIncomingConnection(ByVal ir As IAsyncResult)
  66.  
  67.         Me.Logging("SYSTEM: Connection established, passing TcpClient on.", Logging_Level.SystemWarnings)
  68.  
  69.         'Grab the new client.
  70.         Dim client As TcpClient = _listener.EndAcceptTcpClient(ir)
  71.  
  72.         'Client grabbed, raise event passing it back up.
  73.         RaiseEvent IncomingConnection(client)
  74.  
  75.         Me.Logging("SYSTEM: Passed on, ready for another connection.", Logging_Level.SystemWarnings)
  76.  
  77.         'Setup for the next connection.
  78.         _listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf Me.OnIncomingConnection), _listener)
  79.  
  80.     End Sub
  81.  
  82.     'Events
  83.     Public Event IncomingConnection(ByRef client As TcpClient)
  84.     Public Event Log(ByVal text As String)
  85.  
  86.     'Variable List
  87.     Private _listener As TcpListener
  88.     Private _port As Integer = 6110I
  89.     Private _listening As Boolean = False
  90.     Public _logging As Logging_Level = Logging_Level.Normal
  91.  
  92.     ''' <summary>
  93.     ''' Creates a new TCPV3 Class with the default listener port.
  94.     ''' </summary>
  95.     ''' <remarks></remarks>
  96.     Public Sub New()
  97.  
  98.         'If not listening, create a new listener.
  99.         'If Not _listening Then _listener = New TcpListener(IPAddress.Any, _port)
  100.  
  101.     End Sub
  102.  
  103.     ''' <summary>
  104.     ''' Creates a new TCPV3 Class with the specified port.
  105.     ''' </summary>
  106.     ''' <param name="Port">The port to listen on.</param>
  107.     ''' <remarks></remarks>
  108.     Public Sub New(ByVal Port As Integer)
  109.  
  110.         'If not listening, set the ports up and create the listener.
  111.         If Not _listening Then
  112.             _port = Port
  113.             '_listener = New TcpListener(IPAddress.Any, _port)
  114.         End If
  115.  
  116.     End Sub
  117.  
  118.     ''' <summary>
  119.     ''' Starts the TCPV3 listener.
  120.     ''' </summary>
  121.     ''' <remarks></remarks>
  122.     Public Sub Start()
  123.  
  124.         'If we're not listening, we are allowed to start.
  125.         If Not _listening Then
  126.  
  127.             'Say that we ARE listening.
  128.             _listening = True
  129.  
  130.             'Create the listener
  131.             _listener = New TcpListener(IPAddress.Any, Port)
  132.  
  133.             Me.Logging("SYSTEM: Listener created, activating...", Logging_Level.SystemWarnings)
  134.  
  135.             'Start the actual listener.
  136.             _listener.Start()
  137.  
  138.             'Get ready for the new Clients.
  139.             _listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf Me.OnIncomingConnection), _listener)
  140.  
  141.             Me.Logging("SYSTEM: Waiting for incoming connections...", Logging_Level.SystemWarnings)
  142.  
  143.         End If
  144.  
  145.     End Sub
  146.  
  147.     ''' <summary>
  148.     ''' Stops the TCPV3 listener.
  149.     ''' </summary>
  150.     ''' <remarks></remarks>
  151.     Public Sub [Stop]()
  152.  
  153.         'If we are in fact listening then go ahead and disable all the listening.
  154.         If _listening Then
  155.  
  156.             Me.Logging("SYSTEM: Listener stopped.", Logging_Level.SystemWarnings)
  157.  
  158.             _listening = False
  159.             _listener.Stop()
  160.  
  161.         End If
  162.  
  163.     End Sub
  164.  
  165.     ''' <summary>
  166.     ''' Raises events to logs.
  167.     ''' </summary>
  168.     ''' <param name="text">The text to log.</param>
  169.     ''' <param name="level">The level of the log.</param>
  170.     ''' <remarks></remarks>
  171.     Private Sub Logging(ByVal text As String, ByVal level As Logging_Level)
  172.  
  173.         If level <= _logging Then RaiseEvent Log(String.Format("TCPV3 Log: {0}", text))
  174.  
  175.     End Sub
  176.  
  177. End Class
  178.  
  179. 'End of File
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement