@@ -8,7 +8,10 @@ package conn
88
99import (
1010 "errors"
11+ "fmt"
1112 "net"
13+ "reflect"
14+ "runtime"
1215 "strings"
1316)
1417
@@ -69,6 +72,54 @@ type Endpoint interface {
6972 SrcIP () net.IP
7073}
7174
75+ var (
76+ ErrBindAlreadyOpen = errors .New ("bind is already open" )
77+ ErrWrongEndpointType = errors .New ("endpoint type does not correspond with bind type" )
78+ )
79+
80+ func (fn ReceiveFunc ) PrettyName () string {
81+ name := runtime .FuncForPC (reflect .ValueOf (fn ).Pointer ()).Name ()
82+ // 0. cheese/taco.beansIPv6.func12.func21218-fm
83+ name = strings .TrimSuffix (name , "-fm" )
84+ // 1. cheese/taco.beansIPv6.func12.func21218
85+ if idx := strings .LastIndexByte (name , '/' ); idx != - 1 {
86+ name = name [idx + 1 :]
87+ // 2. taco.beansIPv6.func12.func21218
88+ }
89+ for {
90+ var idx int
91+ for idx = len (name ) - 1 ; idx >= 0 ; idx -- {
92+ if name [idx ] < '0' || name [idx ] > '9' {
93+ break
94+ }
95+ }
96+ if idx == len (name )- 1 {
97+ break
98+ }
99+ const dotFunc = ".func"
100+ if ! strings .HasSuffix (name [:idx + 1 ], dotFunc ) {
101+ break
102+ }
103+ name = name [:idx + 1 - len (dotFunc )]
104+ // 3. taco.beansIPv6.func12
105+ // 4. taco.beansIPv6
106+ }
107+ if idx := strings .LastIndexByte (name , '.' ); idx != - 1 {
108+ name = name [idx + 1 :]
109+ // 5. beansIPv6
110+ }
111+ if name == "" {
112+ return fmt .Sprintf ("%p" , fn )
113+ }
114+ if strings .HasSuffix (name , "IPv4" ) {
115+ return "v4"
116+ }
117+ if strings .HasSuffix (name , "IPv6" ) {
118+ return "v6"
119+ }
120+ return name
121+ }
122+
72123func parseEndpoint (s string ) (* net.UDPAddr , error ) {
73124 // ensure that the host is an IP address
74125
@@ -98,8 +149,3 @@ func parseEndpoint(s string) (*net.UDPAddr, error) {
98149 }
99150 return addr , err
100151}
101-
102- var (
103- ErrBindAlreadyOpen = errors .New ("bind is already open" )
104- ErrWrongEndpointType = errors .New ("endpoint type does not correspond with bind type" )
105- )
0 commit comments