Can a TCPSocket Simultaneously Send and Receive?

[quote=176096:@Norman Palardy]appending strings isn’t the fastest process and has some overhead as you create & destroy temporaries with the assignment

however you also have to worry about the memoryblock growing indefinitely if you do this so you need to rip packets off and shrink the mb by whatever size the packet is

Also note that in this case a MB is much more explicit about things being BYTES - which you’re doing implicitly using the string the way you are. In the new framework you would be required to use a memory block as Text does not have the dual nature that strings do today[/quote]

I’m having a little trouble getting this hooked up. Also, I’m wondering what if my socket writes to my buffer just before I shrink it?

[code]Sub DataAvailable()
if PacketProcessor <> Nil then
if PacketProcessor.mBuffer <> Nil then

  PacketProcessor.mb.Size = PacketProcessor.mb.Size + me.BytesAvailable
  
else
  
  PacketProcessor.mb = new MemoryBlock( me.BytesAvailable )
  PacketProcessor.mBuffer = new BinaryStream( PacketProcessor.mb )
  
end if

PacketProcessor.mBuffer.Write me.ReadAll(Encodings.UTF8)

if PacketProcessor.State = Thread.NotRunning then
  
  PacketProcessor.Run
  
end if

end if
End Sub
[/code]

[code]Sub Run()
While mBuffer.Length > 0
dim bytes as Integer = ProcessPacket( mBuffer.Read(mBuffer.length) )

if bytes > 0 then
  // We've successfully received a full packet
  // Rip the full packet off and shrink the mb by whatever size the packet is
  
  dim size as Integer = mb.Size
  dim newSize as Integer = size - bytes
  dim m as new MemoryBlock( newSize )
  
  for i as Integer = bytes to size-1
    m.byte(i) = mb.byte(i)
  next
  
  mb = m
  
  mBuffer.Position = 0
  
end if

Wend
End Sub
[/code]

[quote=176320:@Mark Scardingo]I’m having a little trouble getting this hooked up. Also, I’m wondering what if my socket writes to my buffer just before I shrink it?
[/quote]
Hard to tell from that code

Then, is it possible to get an example maybe? I’ve been searching all documentation and boards and cannot find anything on how to push and pull off a binarystream backed by a memory block.

In another thread, Greg explained it, but it’s hard to figure out the implementation.

[quote=169925:@Greg O’Lone]You’re going to find this behavior on Linux too. One way to handle this is to use a binary stream backed by a memoryblock. When DataAvailable fires, simply call:

bs.write me.ReadAll

If you want to take data off the front, just remember the position, copy the data you’re keeping in memory to another memoryblock and hook up the binary stream again, with the position set to the old position - the number of bytes you removed.

I’ve also done this with arrays of strings, but you’ve got to be very careful about encodings that way.[/quote]

Thanks

How much data do you typically receive? Hundreds of bytes? Megabytes? It makes a difference.

A full packet could be about a few hundred bytes. However the server could send many of these at one time, only milliseconds apart. I’m afraid my packet processing method could be lengthy and there are certain times when I need to send a reply back.

Honestly, for a few hundred bytes at a time, I would seriously try using the internal buffers. It’s worth seeing if it works. Don’t do more work then necessary. I agree that for large amounts of data, you would not want to rely on the buffers. But small amounts really should be no issue.

The best way to see if something works is to actually try it.

One thing to be wary of is that your for loop COULD result in new data available events getting fired and the buffer being extended
So you need to worry about that
On the old framework you can avoid the loop if you use the “stringvalue” method and just grab everything from the byte after the ones you already handled to the end
It might even be quicker than a loop