jeikabu / nng.NETCore

MIT License
40 stars 22 forks source link

pair RecvMsg cannot recieve data. use a working code in c++. #81

Closed aslucky closed 4 years ago

aslucky commented 4 years ago

Hi,

this maybe not a bug, I am new guy with c#. I have working code in c++ and python, then I connect to c# code use pair protocol. It can connect but in c# code RecvMsg not return.

If there is a demo code should be nice. test code has some irrelevant code it's not friendly enough to show how to use. I use nng.NETCore

c++ pair client code(working)nng-1.3.0 void CnngClientDemoDlg::OnBnClickedBtnStartPair2() { nng_socket sock; char* buf = NULL; int iRet = 0; size_t sz;

if ((iRet = nng_pair0_open(&sock)) != 0) {
    ASSERT(FALSE);
}
if ((iRet = nng_dial(sock, URL_PAIR, NULL, 0)) != 0) {
    ASSERT(FALSE);
}
string strSend = "test:123";
nng_msg* msg;
iRet = nng_msg_alloc(&msg, 0);
nng_msg_append(msg, strSend.c_str(), strSend.size());

if ((iRet = nng_sendmsg(sock, msg, 0)) != 0) {
    ASSERT(FALSE);
}
if ((iRet = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
    ASSERT(FALSE);
}
// This assumes that buf is ASCIIZ (zero terminated).
nng_msg_free(msg);
nng_close(sock);

}

c# pair server (not working) var path = Path.GetDirectoryName(typeof(nngTest).Assembly.Location); var ctx = new nng.NngLoadContext(path); var factory = nng.NngLoadContext.Init(ctx);

        var barrier = new AsyncBarrier(2);

        using (var socket = factory.PairOpen().ThenListenAs(out var listener, pair_url).Unwrap())

or using (var socket = factory.PairOpen().ThenListen(pair_url).Unwrap()) { var request = socket.RecvMsg().Unwrap(); var str = Encoding.ASCII.GetString(request.AsSpan()); Console.WriteLine(str); var reply = factory.CreateMessage(); var replyBytes = Encoding.ASCII.GetBytes("send msg"); reply.Append(replyBytes); socket.SendMsg(reply); }

c++ pair server (working with c++ client) nng-1.3.0 int rv; if ((rv = nng_pair0_open(&pwnd->m_sockPair)) != 0) { ASSERT(FALSE); } if ((rv = nng_listen(pwnd->m_sockPair, URL_PAIR, NULL, 0)) != 0) { ASSERT(FALSE); }

while (FLAG_EXIT_PAIR != (pwnd->m_flag & FLAG_EXIT_PAIR))
{
    char* buf = NULL;
    size_t   sz = 0;
    nng_msg* msg = NULL;
    if ((rv = nng_recvmsg(pwnd->m_sockPair, &msg, 0)) != 0)
    {
        if (rv == NNG_ECLOSED)
        {
            break;
        }
        else
        {
            nng_close(pwnd->m_sockPair);
            ASSERT(FALSE);
        }
    }
    string result = (char*)nng_msg_body(msg);
    printf("recv: %s", nng_msg_body(msg));
    // 这里会自动释放 buf
    rv = nng_send(pwnd->m_sockPair, buf, sz, 0);
    if (rv != 0) {
        ASSERT(FALSE);
    }
    nng_msg_free(msg);

}
jeikabu commented 4 years ago

It's unclear to me what your problem is. RecvMsg just calls nng_recvmsg. You might try re-writing the server in C# to better understand.

jeikabu commented 4 years ago

It looks like you're using nng.NET 1.2.4-rc0 with NNG 1.3.0. nng.NET 1.2.4-rc0 defaults to pair v1 socket while in NNG you're using nng_pair0_open (in 1.3.0 pair v1 is deprecated). This change may help you. Related issue.

1.2.4-rc1 has this fix, but you should probably just move to 1.3.0.

aslucky commented 4 years ago

thanks,I finally use pinvoke call nng.dll directly.

aslucky commented 4 years ago

Wish you do this well. I close the issue