github.com/searKing/golang/go@v1.2.117/net/tcp/handler.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package tcp 6 7 import ( 8 "io" 9 "net" 10 ) 11 12 type OnOpenHandler interface { 13 OnOpen(conn net.Conn) error 14 } 15 type OnOpenHandlerFunc func(conn net.Conn) error 16 17 func (f OnOpenHandlerFunc) OnOpen(conn net.Conn) error { return f(conn) } 18 19 type OnMsgReadHandler interface { 20 OnMsgRead(b io.Reader) (msg any, err error) 21 } 22 type OnMsgReadHandlerFunc func(r io.Reader) (msg any, err error) 23 24 func (f OnMsgReadHandlerFunc) OnMsgRead(r io.Reader) (msg any, err error) { return f(r) } 25 26 type OnMsgHandleHandler interface { 27 OnMsgHandle(b io.Writer, msg any) error 28 } 29 type OnMsgHandleHandlerFunc func(w io.Writer, msg any) error 30 31 func (f OnMsgHandleHandlerFunc) OnMsgHandle(w io.Writer, msg any) error { return f(w, msg) } 32 33 type OnCloseHandler interface { 34 OnClose(w io.Writer, r io.Reader) error 35 } 36 type OnCloseHandlerFunc func(w io.Writer, r io.Reader) error 37 38 func (f OnCloseHandlerFunc) OnClose(w io.Writer, r io.Reader) error { return f(w, r) } 39 40 type OnErrorHandler interface { 41 OnError(w io.Writer, r io.Reader, err error) error 42 } 43 type OnErrorHandlerFunc func(w io.Writer, r io.Reader, err error) error 44 45 func (f OnErrorHandlerFunc) OnError(w io.Writer, r io.Reader, err error) error { return f(w, r, err) } 46 47 var NopOnOpenHandler = nopSC 48 var NopOnMsgReadHandler = nopSC 49 var NopOnMsgHandleHandler = nopSC 50 var NopOnCloseHandler = nopSC 51 var NopOnErrorHandler = nopSC 52 var NopOnHTTPResponseHandler = nopSC 53 var nopSC = &nopServerClient{} 54 55 type NopServer struct{ nopServerClient } 56 type NopClient struct{ nopServerClient } 57 58 var _ Handler = (*NopServer)(nil) 59 var _ Handler = (*NopClient)(nil) 60 61 type nopServerClient struct{} 62 63 func (srv *nopServerClient) OnOpen(conn net.Conn) error { return nil } 64 65 func (srv *nopServerClient) OnMsgRead(r io.Reader) (msg any, err error) { 66 return nil, nil 67 } 68 69 func (srv *nopServerClient) OnMsgHandle(w io.Writer, msg any) error { 70 return nil 71 } 72 73 func (srv *nopServerClient) OnClose(w io.Writer, r io.Reader) error { return nil } 74 75 func (srv *nopServerClient) OnError(w io.Writer, r io.Reader, err error) error { return err }