Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is NetPeer.Send thread safe? #109

Closed
friuns2 opened this issue Oct 31, 2017 · 9 comments
Closed

is NetPeer.Send thread safe? #109

friuns2 opened this issue Oct 31, 2017 · 9 comments
Assignees

Comments

@friuns2
Copy link

friuns2 commented Oct 31, 2017

hello is NetPeer.Send thread safe can i do like that?

public NetPeer peer;
     public void OnMessage(NetDataReader br)
        {
            Task.Run(() =>
            {
                var id = br.GetInt();
               ..............
                peer.Send(nw, SendOptions.ReliableOrdered);
            });
        }
@RevenantX
Copy link
Owner

RevenantX commented Oct 31, 2017

Hi! No, NetPeer.Send is not thread safe (especially for fragmented messages). But if you need i can make it thread safe (it almost done))

@friuns2
Copy link
Author

friuns2 commented Oct 31, 2017

cool thanks!

@pax83
Copy link

pax83 commented Nov 9, 2017

I plane to move my server from lidgren3 to LiteNetLib. I have rooms realization and each room has own thread. If it is not thread-safe, then in what thread should I use to send messages?

@RevenantX
Copy link
Owner

RevenantX commented Nov 9, 2017

@pax83 i will make thread safe send fix soon.
Use any thread but just one) Also you can do this using locks for now.

public void MyThreadSafeSend(byte[] data)
{
    lock(this)
    {
        netPeer.Send(...)
    }
}

@RevenantX
Copy link
Owner

Fixed in next version branch (packetprocessor)

@RevenantX
Copy link
Owner

@friuns2 merged into main branch.

@pha3z
Copy link

pha3z commented Mar 13, 2018

Fixed in next version branch (packetprocessor)

Are you saying that NetPacketProcessor is threadsafe, but the rest of the API is not?

So I must use NetPacketProcessor.Send(Peer, Packet, DeliveryMethod) and not peer.send() if I want to call it from multiple threads?

@RevenantX
Copy link
Owner

@pha3z this fixed in master branch for now. (merged from packetprocessor branch, not class)

@sebas77
Copy link

sebas77 commented Feb 4, 2022

Hi, sorry resuming this post (blame google for it :D).

is NetPeer.Send now thread safe? From what I can see from the code, it doesn't seem like

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants