0

I have C++ service which sends whar_t strings to nodejs script. To send messages it uses IOCP WSASend. The problem is that pretty often nodejs can't receive (decode) those messages correctly. the message itself is a serialized json with SOH (\u0001) in front and "\r" at the end.

Message is pretty big and nodejs script receives at in chunks. It keeps concatenating the chunks until it finds "\r"

var pool = "";
socket.on('data', function (data) {
    pool +=  Buffer.from(data.toString(), 'utf16le').toString();
    var list = pool.split('\u0001');

    for (var i in list) {
        pool = '';

        if (!list[i].length) {
            continue;
        }

        if (list[i].substr(0, 1) != '{') {
            continue;
        }

        if (list[i].substr(-1, 1) != '\r') {
            pool = list[i];
            break;
        }

        var cmd = JSON.parse(list[i]);
    }
}

So it receives the chunk, append to the existing string and checks if the end reached. Works well until the broken chunk comes. I wrote the chunks to the file to track and found out that the broken chunk looks like Chinese. I suspect that the chunk could be started from the odd byte and full message shifted one byte. Needless to say that the message end "\r" will never be found. And even if found never parsed properly.

OVERLAPPED structure I send with WSASend looks like that (need SmartBuffer because same message could be sent to many sockets):

typedef std::shared_ptr<wchar_t> SmartBuffer;
class OVERLAPPED_EX : public OVERLAPPED
{
public:
    WSABUF m_wsabuf;
    UCHAR m_type;
    SOCKET m_socket;
    DWORD bytes;            
    SmartBuffer m_buffer;   

    OVERLAPPED_EX(SOCKET s, UCHAR t, SmartBuffer buf) : OVERLAPPED(),
        m_socket(s), m_type(t), bytes(0)
    {
        hEvent = NULL;
        m_buffer = buf;
        m_wsabuf.buf = (char*)m_buffer.get();
        m_wsabuf.len = wcslen((wchar_t*)buf.get()) * sizeof(wchar_t);
    };

    ~OVERLAPPED_EX() {};
};

How to send widechar messages from c++ to nodejs or receive in nodejs properly please?

htonus
  • 629
  • 1
  • 9
  • 19
  • how `iocp` here related ? and you not need have `WSABUF` in `OVERLAPPED_EX` - it can be in local stack. only the content of buffers must be valid until `WSASend` not finished. but not `WSABUF` – RbMm Apr 08 '18 at 12:48
  • Just provided complete info. WSABUF inside OVERLAPPED_EX to keep all references in one place. Once send completed - I just delete OVERLAPPED_EX and shared_ptr deletes the buffer – htonus Apr 08 '18 at 14:11

0 Answers0