tidwall / evio

Fast event-loop networking for Go
MIT License
5.88k stars 491 forks source link

no way to write back data in Conn? #26

Closed dbskccc closed 5 years ago

dbskccc commented 6 years ago

there is no Write in Conn, how to send some data to it?

liu4480 commented 6 years ago

@tidwall I have a similar question as well,Wake could do this before, but it was removed in dd88755b17743a570aa642f0b55f5ff71f83853f, is there any replacement for Wake?

haoziAndy commented 6 years ago

@tidwall I have a similar question as well

tidwall commented 6 years ago

There is no way to write directly to a connection. All reads and writes flow through the events. In the previous version of evio it was possible to wake up a connection, which causes an empty Data event to fire, allowing for an opportunity to send bytes to a specific connection. This operation was removed in 2.0 because I ran into inefficiencies with tracking the paths needed to interrupt a specific loop that was running along side other loops in other goroutines.

I hope to have the wake operation return in the future, once I figure out a fast and safe way to do so.

haoziAndy commented 6 years ago

Thank you for reply. Why cant i send msg to connection directly through fd or net.conn?

tidwall commented 6 years ago

I'm assuming that the reason for sending directly to an evio connection is to do so from a separate goroutine. If so, then writing would require synchronizing the file descriptor with the event loop on a different thread. This would slow stuff down. I'm still investigating ways to do this efficiently, or rather without any noticeable penalty to throughput.

tidwall commented 5 years ago

I just pushed an update that adds a new Wake function to evio.Conn.

Calling conn.Wake() will trigger an event.Data for the target connection and pass a nil for the in packet. This allows for writing back to a client connection without leaving the loop. The Wake function can safely be called at any time, from any goroutine, but the Data event will only fire when the connection is still opened.