Hello all,
Ok so i have been working on a SocketAsyncEventArgs Chat server\client, all is working well the thing that is bothering me is that with my old way of reading and writing to the network stream i would just send the size of the buffer as an int32 then send the buffer eg:
Writing:
Dim count as int32 = buffer.count
Writer.write(count)
writer.write(buffer)
writer.flush
Reading:
dim count as int32 = reader.readint32
dim Buffer as byte() = reader.readbytes(count)
But i cant do that with SAEA's.... well i can but it slows it all down, so i just give the SAEA object that i use for reading a big buffer(8MB)
here is my read loop:
<pre lang="vb">
Private Sub ProcessReceive(e As SocketAsyncEventArgs)
Dim token As AsyncUserToken = DirectCast(e.UserToken, AsyncUserToken)
If e.BytesTransferred > 0 AndAlso e.SocketError = SocketError.Success Then
Interlocked.Add(m_totalBytesRead, e.BytesTransferred)
' Console.WriteLine("The server has read a total of {0} bytes", m_totalBytesRead)
Dim readEventArgs As SocketAsyncEventArgs = e 'm_readWritePool.Pop()
Dim msgbyte As MSGByteHolder = MSGBytpool.Pop
msgbyte.MSGByte = readEventArgs.Buffer
Dim willRaiseEvent As Boolean = ReadAsync.AcceptSocket.ReceiveAsync(ReadAsync)
Array.Resize(msgbyte.MSGByte, e.BytesTransferred)
Dim ObjectPacker As New ObjectPacker
Dim buffer As MsgObject = CType(ObjectPacker.ConvertBytesToObject(msgbyte.MSGByte), MsgObject)
RaiseEvent MsgReseaved(buffer)
If Not willRaiseEvent Then
ProcessReceive(ReadAsync)
End If
MSGBytpool.Push(msgbyte)
Else
CloseClientSocket()
End If
End Sub
Here is how i send:
<pre lang="vb">
Public Sub SendAsync(ByVal [Byte] As Byte())
Dim SendEventArgs As SocketAsyncEventArgs = m_readWritePool.Pop()
SendEventArgs.SetBuffer([Byte], 0, [Byte].Count)
SendEventArgs.AcceptSocket = ClientSocket
Dim willRaiseEvent As Boolean = SendEventArgs.AcceptSocket.SendAsync(SendEventArgs)
If Not willRaiseEvent Then
ProcessSend(SendEventArgs)
End If
End Sub
what would be the best way to do msg framing with this setup im just a lil stuck and cant think of a way to get it done.
I know it starts a new read befor it even passes off the old buffer but thats how i want it for now and it seems to work so far.... if it needs to be changed im open to that if any more of my code is needed ill be happy to put it up.
ty for your time