From: qydysky Date: Sat, 13 Apr 2024 03:08:26 +0000 (+0000) Subject: 1 X-Git-Tag: v0.28.20240413031404 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=4a0f2b1efe13f7da8d47952c055338b1a2857b8c;p=part%2F.git 1 --- diff --git a/Net.go b/Net.go index 87a8467..8c89074 100644 --- a/Net.go +++ b/Net.go @@ -66,13 +66,13 @@ const ( ErrorMsg = iota AcceptMsg DenyMsg - PortMsg + LisnMsg ) // when Type is ErrorMsg, Msg is set to error // when Type is AcceptMsg, Msg is set to net.Addr // when Type is DenyMsg, Msg is set to net.Addr -// when Type is PortMsg, Msg is set to Listen Port(int) +// when Type is LisnMsg, Msg is set to net.Addr type ForwardMsg struct { Type int Msg interface{} @@ -162,12 +162,12 @@ func Forward(targetaddr, network, listenaddr string, acceptCIDRs []string) (clos close(closec) } - //返回监听端口 + //返回监听地址 select { default: case msg_chan <- ForwardMsg{ - Type: PortMsg, - Msg: listener.Addr().(*net.TCPAddr).Port, + Type: LisnMsg, + Msg: listener.Addr(), }: } @@ -310,8 +310,8 @@ func ForwardUdp(targetaddr, network, listenaddr string, acceptCIDRs []string) (c select { default: case msg_chan <- ForwardMsg{ - Type: PortMsg, - Msg: serConn.LocalAddr().(*net.UDPAddr).Port, + Type: LisnMsg, + Msg: serConn.LocalAddr(), }: } diff --git a/rpc/Rpc.go b/rpc/Rpc.go index 9748754..f9ccae8 100644 --- a/rpc/Rpc.go +++ b/rpc/Rpc.go @@ -121,14 +121,13 @@ func UnRegister(t *Server, path string) { } func Call[T, E any](host, path string, it *T, ot *E) error { - var buf bytes.Buffer - if e := gob.NewEncoder(&buf).Encode(it); e != nil { + t := &Gob{} + if e := t.encode(it).Err; e != nil { return errors.Join(ErrCliEncode, e) } else { if c, e := rpc.DialHTTPPath("tcp", host, path); e != nil { return errors.Join(ErrDial, e) } else { - t := &Gob{Data: buf.Bytes()} call := <-c.Go("DealGob.Deal", t, t, make(chan *rpc.Call, 1)).Done if call.Error != nil { return errors.Join(ErrCliDeal, call.Error) @@ -136,7 +135,7 @@ func Call[T, E any](host, path string, it *T, ot *E) error { if t.Err != nil { return errors.Join(ErrSerGob, t.Err) } - if e := gob.NewDecoder(bytes.NewReader(t.Data)).Decode(ot); e != nil { + if e := t.decode(ot).Err; e != nil { return errors.Join(ErrCliDecode, e) } return nil diff --git a/rpc/Rpc_test.go b/rpc/Rpc_test.go index 69342a6..c06b821 100644 --- a/rpc/Rpc_test.go +++ b/rpc/Rpc_test.go @@ -20,7 +20,7 @@ type test2_1 struct { } func TestMain(t *testing.T) { - pob := NewServer("127.0.0.1:10902") + pob := NewServer("127.0.0.1:10904") defer pob.Shutdown() if e := Register(pob, "/123", func(i *int, o *test1) error { *i += 1 @@ -35,7 +35,7 @@ func TestMain(t *testing.T) { var i int = 9 var out test2 - if e := Call("127.0.0.1:10902", "/123", &i, &out); e != nil { + if e := Call("127.0.0.1:10904", "/123", &i, &out); e != nil { t.Fatal(e) } @@ -44,6 +44,48 @@ func TestMain(t *testing.T) { } } +func TestMain2(t *testing.T) { + pob := NewServer("127.0.0.1:10903") + defer pob.Shutdown() + if e := Register(pob, "/add", func(i *int, o *int) error { + *o = *i + 1 + return nil + }); e != nil { + t.Fatal(e) + } + + var in int = 1 + var out int + if e := Call("127.0.0.1:10903", "/add", &in, &out); e != nil { + t.Fatal(e) + } + + if out != 2 { + t.FailNow() + } +} + +func Benchmark1(b *testing.B) { + pob := NewServer("127.0.0.1:10903") + defer pob.Shutdown() + if e := Register(pob, "/add", func(i *int, o *int) error { + *o = *i + 1 + return nil + }); e != nil { + b.Fatal(e) + } + + b.ResetTimer() + + for i := 0; i < b.N; i++ { + var in int = 1 + var out int + if e := Call("127.0.0.1:10903", "/add", &in, &out); e != nil || out != 2 { + b.Fatal(e) + } + } +} + // func TestMain2(t *testing.T) { // pob := NewServer("127.0.0.1:10902")