github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/cmd/fastcache1e6/prod_tcp.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "github.com/angenalZZZ/gofunc/net" 6 "github.com/angenalZZZ/gofunc/net/pool/goroutine" 7 "sync/atomic" 8 "time" 9 ) 10 11 type netTcpServer struct { 12 *net.EventServer 13 *goroutine.Pool 14 connCount int32 15 } 16 17 // OnInitComplete fires when the server is ready for accepting connections. 18 // The server parameter has information and various utilities. 19 func (es *netTcpServer) OnInitComplete(server net.Server) (action net.Action) { 20 fmt.Printf("Io2TCP server is listening on %s (multi-cores: %t, loops: %d)\n", 21 server.Addr.String(), server.Multicore, server.NumEventLoop) 22 return 23 } 24 25 // OnShutdown fires when the server is being shut down, it is called right after 26 // all event-loops and connections are closed. 27 func (es *netTcpServer) OnShutdown(_ net.Server) {} 28 29 // OnOpened fires when a new connection has been opened. 30 // The info parameter has information about the connection such as 31 // it's local and remote address. 32 // Use the out return value to write data to the connection. 33 func (es *netTcpServer) OnOpened(_ net.Conn) (out []byte, action net.Action) { 34 _ = atomic.AddInt32(&es.connCount, 1) 35 return 36 } 37 38 // OnClosed fires when a connection has been closed. 39 // The err parameter is the last known connection error. 40 func (es *netTcpServer) OnClosed(_ net.Conn, _ error) (action net.Action) { 41 _ = atomic.AddInt32(&es.connCount, -1) 42 return 43 } 44 45 // PreWrite fires just before any data is written to any client socket. 46 func (es *netTcpServer) PreWrite() {} 47 48 // React fires when a connection sends the server data. 49 // Invoke c.Read() or c.ReadN(n) within the parameter c to read incoming data from client/connection. 50 // Use the out return value to write data to the client/connection. 51 func (es *netTcpServer) React(frame []byte, _ net.Conn) (out []byte, action net.Action) { 52 // Write synchronously. 53 out = defaultService.Handle(frame) 54 return 55 56 /* 57 // Use ants pool to unblock the event-loop. 58 _ = es.Pool.Submit(func() { 59 c.AsyncWrite(frame) 60 }) 61 */ 62 63 /* 64 // Write asynchronously. 65 data := append([]byte{}, frame...) 66 go func() { 67 time.Sleep(time.Second) 68 c.AsyncWrite(data) 69 }() 70 return 71 */ 72 } 73 74 // Tick fires immediately after the server starts and will fire again 75 // following the duration specified by the delay return value. 76 func (es *netTcpServer) Tick() (delay time.Duration, action net.Action) { 77 fmt.Println("Io2TCP tick event, conn count:", es.connCount) 78 delay = time.Second 79 return 80 } 81 82 func ProdTCP() { 83 echo := new(netTcpServer) 84 echo.Pool = goroutine.Default() 85 defer echo.Pool.Release() 86 if err := net.Serve(echo, fmt.Sprintf("tcp://:%d", *flagPort), 87 net.WithMulticore(true), 88 net.WithTicker(true)); err != nil { 89 _ = fmt.Errorf("Io2TCP failed to serve: %v\n", err) 90 } 91 }