-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: refactor server and proxy and remove gnet/v2
- Loading branch information
Showing
8 changed files
with
503 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package network | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/rs/zerolog" | ||
) | ||
|
||
type Option struct { | ||
Multicore bool | ||
NumEventLoop int | ||
// LB LoadBalancing | ||
ReuseAddr bool | ||
ReusePort bool | ||
MulticastInterfaceIndex int | ||
ReadBufferCap int | ||
WriteBufferCap int | ||
LockOSThread bool | ||
Ticker bool | ||
TCPKeepAlive time.Duration | ||
TCPNoDelay TCPSocketOpt | ||
SocketRecvBuffer int | ||
SocketSendBuffer int | ||
LogPath string | ||
LogLevel zerolog.Level | ||
Logger zerolog.Logger | ||
} | ||
|
||
type Action int | ||
|
||
const ( | ||
None Action = iota | ||
Close | ||
Shutdown | ||
) | ||
|
||
type TCPSocketOpt int | ||
|
||
const ( | ||
TCPNoDelay TCPSocketOpt = iota | ||
TCPDelay | ||
) | ||
|
||
type Engine struct { | ||
listener net.Listener | ||
host string | ||
port int | ||
connections map[string]*net.Conn | ||
bufferSize int | ||
handler EventHandler | ||
stopChannel chan struct{} | ||
} | ||
|
||
func (engine *Engine) CountConnections() int { | ||
return len(engine.connections) | ||
} | ||
|
||
func (engine *Engine) Stop(ctx context.Context) error { | ||
engine.stopChannel <- struct{}{} | ||
return nil | ||
} | ||
|
||
type ( | ||
EventHandler interface { | ||
OnBoot(eng Engine) (action Action) | ||
OnShutdown(eng Engine) | ||
OnOpen(c net.Conn) (out []byte, action Action) | ||
OnClose(c net.Conn, err error) (action Action) | ||
OnTraffic(c net.Conn) (action Action) | ||
OnTick() (delay time.Duration, action Action) | ||
} | ||
|
||
BuiltinEventEngine struct{} | ||
) | ||
|
||
// Create a new TCP server. | ||
func Run(network, address string, server *Server, opts ...Option) error { | ||
engine := Engine{ | ||
connections: make(map[string]*net.Conn), | ||
stopChannel: make(chan struct{}), | ||
} | ||
|
||
if action := server.OnBoot(engine); action != None { | ||
return nil | ||
} | ||
server.logger.Debug().Msg("Server booted") | ||
|
||
if ln, err := net.Listen(network, address); err != nil { | ||
server.logger.Error().Err(err).Msg("Failed to listen") | ||
} else { | ||
engine.listener = ln | ||
} | ||
defer engine.listener.Close() | ||
server.logger.Debug().Str("address", engine.listener.Addr().String()).Msg("Server listening") | ||
|
||
if engine.listener == nil { | ||
server.logger.Error().Msg("Listener is nil") | ||
return nil | ||
} | ||
server.logger.Debug().Msg("Server started") | ||
|
||
if host, port, err := net.SplitHostPort(engine.listener.Addr().String()); err != nil { | ||
server.logger.Error().Err(err).Msg("Failed to split host and port") | ||
return err | ||
} else { | ||
engine.host = host | ||
if engine.port, err = strconv.Atoi(port); err != nil { | ||
server.logger.Error().Err(err).Msg("Failed to convert port to integer") | ||
return err | ||
} | ||
} | ||
|
||
for { | ||
// if <-engine.stopChannel == struct{}{} { | ||
// server.logger.Debug().Msg("Server stopped") | ||
// break | ||
// } | ||
server.logger.Debug().Msg("Server tick") | ||
|
||
conn, err := engine.listener.Accept() | ||
if err != nil { | ||
server.logger.Error().Err(err).Msg("Failed to accept connection") | ||
return err | ||
} | ||
|
||
server.logger.Debug().Str("address", conn.RemoteAddr().String()).Msg("Connection accepted") | ||
|
||
if out, action := server.OnOpen(conn); action != None { | ||
conn.Write(out) | ||
conn.Close() | ||
if action == Shutdown { | ||
server.logger.Debug().Str("address", conn.RemoteAddr().String()).Msg( | ||
"Connection closed") | ||
return nil | ||
} | ||
} | ||
server.logger.Debug().Str("address", conn.RemoteAddr().String()).Msg("Connection accepted") | ||
|
||
// engine.connections[conn.RemoteAddr().String()] = &conn | ||
go func(server *Server, conn net.Conn) { | ||
for { | ||
// if n, err := conn.Read([]byte{}); n == 0 && err != nil { | ||
// return | ||
// } | ||
// if action := server.OnTraffic(conn); action == Close { | ||
// if action := server.OnClose(conn, err); action == Close { | ||
// conn.Close() | ||
// break | ||
// } | ||
// } | ||
if action := server.OnTraffic(conn); action == Close { | ||
if action := server.OnClose(conn, err); action == Close { | ||
// FIXME: this should be handled by the server | ||
server.logger.Debug().Str("address", conn.RemoteAddr().String()).Msg( | ||
"Connection closed") | ||
conn.Close() | ||
return | ||
} | ||
} | ||
time.Sleep(100 * time.Millisecond) | ||
} | ||
}(server, conn) | ||
|
||
// defer delete(engine.connections, conn.RemoteAddr().String()) | ||
|
||
// if duration, action := server.OnTick(); action == Shutdown { | ||
// return nil | ||
// } else if duration > 0 { | ||
// time.Sleep(duration) | ||
// } | ||
} | ||
// engine.handler.OnShutdown(engine) | ||
} |
Oops, something went wrong.