Added PreWrite

This commit is contained in:
Josh Baker 2018-05-26 07:06:27 -07:00
parent 0e201dccae
commit e74aca3b5a
3 changed files with 17 additions and 0 deletions

View File

@ -111,6 +111,8 @@ type Events struct {
// underlying socket connection. It can be freely used in goroutines
// and should be closed when it's no longer needed.
Detached func(c Conn, rwc io.ReadWriteCloser) (action Action)
// PreWrite fires just before any data is written to any client socket.
PreWrite func()
// Data fires when a connection sends the server data.
// The in parameter is the incoming data.
// Use the out return value to write data to the connection.

View File

@ -369,6 +369,9 @@ func stdloopRead(s *stdserver, l *stdloop, c *stdconn, in []byte) error {
if s.events.Data != nil {
out, action := s.events.Data(c, in)
if len(out) > 0 {
if s.events.PreWrite != nil {
s.events.PreWrite()
}
c.conn.Write(out)
}
switch action {
@ -387,6 +390,9 @@ func stdloopReadUDP(s *stdserver, l *stdloop, c *stdudpconn) error {
if s.events.Data != nil {
out, action := s.events.Data(c, c.in)
if len(out) > 0 {
if s.events.PreWrite != nil {
s.events.PreWrite()
}
s.lns[c.addrIndex].pconn.WriteTo(out, c.remoteAddr)
}
switch action {
@ -418,6 +424,9 @@ func stdloopAccept(s *stdserver, l *stdloop, c *stdconn) error {
if s.events.Opened != nil {
out, opts, action := s.events.Opened(c)
if len(out) > 0 {
if s.events.PreWrite != nil {
s.events.PreWrite()
}
c.conn.Write(out)
}
if opts.TCPKeepAlive > 0 {

View File

@ -315,6 +315,9 @@ func loopUDPRead(s *server, l *loop, lnidx, fd int) error {
in := append([]byte{}, l.packet[:n]...)
out, action := s.events.Data(c, in)
if len(out) > 0 {
if s.events.PreWrite != nil {
s.events.PreWrite()
}
syscall.Sendto(fd, out, 0, sa)
}
switch action {
@ -350,6 +353,9 @@ func loopOpened(s *server, l *loop, c *conn) error {
}
func loopWrite(s *server, l *loop, c *conn) error {
if s.events.PreWrite != nil {
s.events.PreWrite()
}
n, err := syscall.Write(c.fd, c.out)
if err != nil {
if err == syscall.EAGAIN {