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.




Reply With Quote