M0r13n / pyais

AIS message decoding and encoding in Python (AIVDM/AIVDO)
MIT License
176 stars 61 forks source link

handles line breaks (\n) when reading from sockets #114

Closed M0r13n closed 1 year ago

M0r13n commented 1 year ago

Relates to https://github.com/M0r13n/pyais/pull/113

jwheare commented 1 year ago

I think there's a new bug here. I'm seeing a situation where it looks like a fixed partial line is being prepended to all subsequent lines. Does the partial variable need to be reset when a complete line is yielded?

jwheare commented 1 year ago

Though looking closer at my data now I'm not certain of this, it seems to only be happening for one mmsi so it could also just be a bad transmitter. It apparently fixed itself after I restarted the socket listener so I got suspicious. Will keep monitoring things here...

M0r13n commented 1 year ago

@jwheare

Good point. I was able to reproduce the issue with a simple test case:

# HAVING a socket
stream = SocketStream(None)

# WHEN receiving a partial line and a complete line afterwards
receiver = MockReceiver([b'Hello\nWor', b'ld\n', b'FooBar\n'])
stream.recv = receiver.recv

# THEN the partial line is not prepended to the next line
result = list(stream.read())
expected = [b'Hello\n', b'World\n', 'FooBar\n']
self.assertEqual(result, expected)

The result should have been [b'Hello\n', b'World\n', 'FooBar\n'] but is [b'Hello\n', b'World\n', b'WorFooBar\n']. This seems to be exactly the bug that you observed.

Fix is incoming

M0r13n commented 1 year ago

I merged the fix. A fixed version is available on PyPi: v2.5.4

jwheare commented 1 year ago

Thanks! I'm running the fix from #115 via 2.5.4 now and will keep an eye on it.