Results 1 to 5 of 5

Thread: Many Messages Sent To Server - Few Processed

  1. #1

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Many Messages Sent To Server - Few Processed

    I'm using a modified version of John's Message server, instead of passing a string I am passing a serializable object called "DataClass".

    This is my modified Read sub to process data that gets sent from the client to the server:

    vb.net Code:
    1. Protected Overrides Sub Read(ByVal ar As IAsyncResult)
    2.     Dim asyncState = DirectCast(ar.AsyncState, ReadAsyncState)
    3.     Dim buffer = asyncState.Buffer
    4.     Dim client = asyncState.Client
    5.     Dim host = Me.clients(client)
    6.  
    7.     Try
    8.         Dim stream = client.GetStream()
    9.         Dim byteCount As Integer = 0
    10.  
    11.         Try
    12.             byteCount = stream.EndRead(ar)
    13.         Catch ex As Exception
    14.             If Not Me.Disposed Then
    15.                 Me.RemoveClient(client)
    16.                 Me.OnClientForcedDisconnect(New ConnectionEventArgs(host))
    17.             End If
    18.             Return
    19.         End Try
    20.  
    21.         If byteCount = 0 Then
    22.             Me.RemoveClient(client)
    23.         Else
    24.             Dim message As DataClass = CType(MyBase.GetObject(buffer), DataClass)
    25.  
    26.             stream.BeginRead(buffer, 0, Me.BufferSize, AddressOf Me.Read, New ReadAsyncState(client, buffer))
    27.  
    28.             Me.OnDataReceived(New DataReceivedEventArgs(Me.clients(client), message))
    29.         End If
    30.     Catch ex As InvalidOperationException
    31.         Return
    32.     End Try
    33. End Sub

    This is what MyBase.GetObject does:

    vb.net Code:
    1. Public Function GetObject(ByVal buffer As Byte()) As Object
    2.     If buffer IsNot Nothing Then
    3.         Try
    4.             Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
    5.             Using stream As New IO.MemoryStream()
    6.                 stream.Write(buffer, 0, buffer.Length)
    7.                 stream.Seek(0, IO.SeekOrigin.Begin)
    8.                 Return formatter.Deserialize(stream)
    9.             End Using
    10.         Catch ex As InvalidOperationException
    11.             Throw ex
    12.         Catch ex As Exception
    13.             Return Nothing
    14.         End Try
    15.     End If
    16.     Return Nothing
    17. End Function

    The message is received fine when they aren't in quick succession, however I tested it by sending like 1000 messages almost instantaneously and it only processed about 2. Obviously this isn't acceptable, but I am unsure how to properly implement some type of queue that will ensure all backlogged messages will get processed. Any help is much appreciated.
    Last edited by ForumAccount; Jan 20th, 2010 at 08:31 PM.

  2. #2

    Re: Many Messages Sent To Server - Few Processed

    Have a handler that will accept the incoming data and spin off a new thread to process it for each one coming in.

  3. #3

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Many Messages Sent To Server - Few Processed

    The problem is that it doesn't receive all the messages. If I send 1000 instant messages the Read method won't run 1000 times.

  4. #4

    Re: Many Messages Sent To Server - Few Processed

    Because each time a message is sent, the main thread is tied up with that message. Did you try it?

  5. #5

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Many Messages Sent To Server - Few Processed

    Maybe I'm confused here, but from what I understand about asynchronous sockets it's already doing that with "stream.BeginRead". This isn't run on the main thread, this is run on a separate thread used for listening to client data... Each time it receives data it deserializes it and then begins listening asynchronously again, then raises the event.

    The listener thread is created when the client connects:

    vb.net Code:
    1. Private Sub AcceptTcpClient(ByVal ar As IAsyncResult)
    2.     Try
    3.         Dim client = Me.server.EndAcceptTcpClient(ar)
    4.         Dim host As HostInfo = Nothing
    5.  
    6.         With DirectCast(client.Client.RemoteEndPoint, IPEndPoint)
    7.             host = New HostInfo(.Address.ToString(), .Port)
    8.         End With
    9.  
    10.         Me.clients.Add(client, host)
    11.  
    12.         'Starts listening again for new clients
    13.         Me.server.BeginAcceptTcpClient(AddressOf Me.AcceptTcpClient, Nothing)
    14.  
    15.         Dim stream = client.GetStream()
    16.         Dim buffer(Me.BufferSize - 1) As Byte
    17.         'Right here it spins off a new listener for that client
    18.         stream.BeginRead(buffer, 0, Me.BufferSize, AddressOf Me.Read, New ReadAsyncState(client, buffer))
    19.  
    20.         Me.OnConnectionAccepted(New ConnectionEventArgs(host))
    21.     Catch ex As ObjectDisposedException
    22.         Return
    23.     End Try
    24. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width