]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.20240413031404
authorqydysky <qydysky@foxmail.com>
Sat, 13 Apr 2024 03:08:26 +0000 (03:08 +0000)
committerqydysky <qydysky@foxmail.com>
Sat, 13 Apr 2024 03:08:26 +0000 (03:08 +0000)
Net.go
rpc/Rpc.go
rpc/Rpc_test.go

diff --git a/Net.go b/Net.go
index 87a84675d7a0e73fa62730071524765b1d86e079..8c89074e839d8dd3926adf3039af3274a6837e9d 100644 (file)
--- 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(),
        }:
        }
 
index 974875429a85a6ef1ded1d6a830f22a8cd26f594..f9ccae80e15225fae37e77795cbf7a915ce02ad4 100644 (file)
@@ -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
index 69342a684be31c24ae3d8d6e5d1e2dab2da977be..c06b8218cc732ee8bb4a8a0c0aa88693f66c5918 100644 (file)
@@ -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")