Results 1 to 5 of 5

Thread: Many Messages Sent To Server - Few Processed

Threaded View

  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.

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