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:
Protected Overrides Sub Read(ByVal ar As IAsyncResult)
Dim asyncState = DirectCast(ar.AsyncState, ReadAsyncState)
Dim buffer = asyncState.Buffer
Dim client = asyncState.Client
Dim host = Me.clients(client)
Try
Dim stream = client.GetStream()
Dim byteCount As Integer = 0
Try
byteCount = stream.EndRead(ar)
Catch ex As Exception
If Not Me.Disposed Then
Me.RemoveClient(client)
Me.OnClientForcedDisconnect(New ConnectionEventArgs(host))
End If
Return
End Try
If byteCount = 0 Then
Me.RemoveClient(client)
Else
Dim message As DataClass = CType(MyBase.GetObject(buffer), DataClass)
stream.BeginRead(buffer, 0, Me.BufferSize, AddressOf Me.Read, New ReadAsyncState(client, buffer))
Me.OnDataReceived(New DataReceivedEventArgs(Me.clients(client), message))
End If
Catch ex As InvalidOperationException
Return
End Try
End Sub
This is what MyBase.GetObject does:
vb.net Code:
Public Function GetObject(ByVal buffer As Byte()) As Object
If buffer IsNot Nothing Then
Try
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Using stream As New IO.MemoryStream()
stream.Write(buffer, 0, buffer.Length)
stream.Seek(0, IO.SeekOrigin.Begin)
Return formatter.Deserialize(stream)
End Using
Catch ex As InvalidOperationException
Throw ex
Catch ex As Exception
Return Nothing
End Try
End If
Return Nothing
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.
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.
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.
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?
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:
Private Sub AcceptTcpClient(ByVal ar As IAsyncResult)
Try
Dim client = Me.server.EndAcceptTcpClient(ar)
Dim host As HostInfo = Nothing
With DirectCast(client.Client.RemoteEndPoint, IPEndPoint)
host = New HostInfo(.Address.ToString(), .Port)
End With
Me.clients.Add(client, host)
'Starts listening again for new clients
Me.server.BeginAcceptTcpClient(AddressOf Me.AcceptTcpClient, Nothing)
Dim stream = client.GetStream()
Dim buffer(Me.BufferSize - 1) As Byte
'Right here it spins off a new listener for that client
stream.BeginRead(buffer, 0, Me.BufferSize, AddressOf Me.Read, New ReadAsyncState(client, buffer))
Me.OnConnectionAccepted(New ConnectionEventArgs(host))
Catch ex As ObjectDisposedException
Return
End Try
End Sub