After a night of chasing a bug I finally found the error in the logic of my IPCSocket subclass. It was reading too much data off the buffer and made me think messages weren’t coming through. The fix was to loop in DataAvailable reading messages until none are left.
My question is why polling didn’t work? Before implementing the loop I had modified DataAvailable to properly read only one message (leaving data in the buffer) and there was a Timer calling Poll on the socket. When the Timer calls Poll I also logged bytesAvailable and see that there are bytes waiting but no DataAvailable event gets triggered.
The docs for IPCSocket.Poll simple says “Manually polls the socket.” and for IPCSocket proper “Use a Timer with a very low Period value, e.g. 1, to call the Poll method in order to have the DataAvailable event fire as quickly as possible after receiving new data.”
So when there’s 74 bytes in the buffer shouldn’t Poll raise a DataAvailable event?
Also, here’s the full code, are there any more problems or improvements I can make?
EZIPCSocket inherits IPCSocket adding send(tag, data) and event ReceiveData(tag, data). tag is used to identify the type of message and ReceiveData doesn’t fire until the whole message is across.
[code]Sub send(tag As Int32, data As MemoryBlock)
if not IsConnected then
RaiseEvent Error(1)
return
end
if data = nil or data.Size = data.SizeUnknown then
RaiseEvent Error(2)
return
end
dim fullSize As integer = 8 + data.Size
dim head As new MemoryBlock(8)
head.Int32Value(0) = fullSize
head.Int32Value(4) = tag
Write(head)
Write(data)
Flush
End Sub[/code]
[code]Sub DataAvailable()
do
if BytesAvailable < 8 then return //not enough to be a message
dim m As MemoryBlock = Lookahead
dim fullSize As integer = m.Int32Value(0)
if fullSize > m.Size then return //haven't received full message
Call Read(fullSize) //pull 1 message off buffer
dim tag As integer = m.Int32Value(4) //pull parts out of received data
dim data As MemoryBlock = m.StringValue(8, fullSize-8)
RaiseEvent ReceiveData(tag, data) //pass completed message along
loop
End Sub[/code]
Oh boy, you should’ve seen my post before I found my bug