feat(notify): 重构通信内核并补齐 stream/bulk/record/transfer 能力
- 引入 LogicalConn/TransportConn 分层,ClientConn 保留兼容适配层 - 新增 Stream、Bulk、RecordStream 三条数据面能力及对应控制路径 - 完成 transfer/file 传输内核与状态快照、诊断能力 - 补齐 reconnect、inbound dispatcher、modern psk 等基础模块 - 增加大规模回归、并发与基准测试覆盖 - 更新依赖库
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
//go:build !windows
|
||||
|
||||
package transport
|
||||
|
||||
import (
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
func dialNamedPipe(_ string, _ *time.Duration) (net.Conn, error) {
|
||||
return nil, ErrNamedPipeUnsupported
|
||||
}
|
||||
|
||||
func listenNamedPipe(_ string) (net.Listener, error) {
|
||||
return nil, ErrNamedPipeUnsupported
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
//go:build windows
|
||||
|
||||
package transport
|
||||
|
||||
import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/Microsoft/go-winio"
|
||||
)
|
||||
|
||||
func dialNamedPipe(addr string, timeout *time.Duration) (net.Conn, error) {
|
||||
return winio.DialPipe(NormalizeNamedPipeAddr(addr), timeout)
|
||||
}
|
||||
|
||||
func listenNamedPipe(addr string) (net.Listener, error) {
|
||||
return winio.ListenPipe(NormalizeNamedPipeAddr(addr), &winio.PipeConfig{
|
||||
MessageMode: false,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrNamedPipeUnsupported = errors.New("named pipe transport is only supported on windows")
|
||||
|
||||
func IsUDPNetwork(network string) bool {
|
||||
return strings.Contains(strings.ToLower(strings.TrimSpace(network)), "udp")
|
||||
}
|
||||
|
||||
func IsNamedPipeNetwork(network string) bool {
|
||||
switch strings.ToLower(strings.TrimSpace(network)) {
|
||||
case "npipe", "pipe", "namedpipe", "named-pipe":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func Dial(network string, addr string) (net.Conn, error) {
|
||||
if IsNamedPipeNetwork(network) {
|
||||
return dialNamedPipe(addr, nil)
|
||||
}
|
||||
return net.Dial(network, addr)
|
||||
}
|
||||
|
||||
func DialTimeout(network string, addr string, timeout time.Duration) (net.Conn, error) {
|
||||
if IsNamedPipeNetwork(network) {
|
||||
return dialNamedPipe(addr, &timeout)
|
||||
}
|
||||
return net.DialTimeout(network, addr, timeout)
|
||||
}
|
||||
|
||||
func Listen(network string, addr string) (net.Listener, error) {
|
||||
if IsNamedPipeNetwork(network) {
|
||||
return listenNamedPipe(addr)
|
||||
}
|
||||
return net.Listen(network, addr)
|
||||
}
|
||||
|
||||
func NormalizeNamedPipeAddr(addr string) string {
|
||||
trimmed := strings.TrimSpace(addr)
|
||||
if trimmed == "" {
|
||||
return trimmed
|
||||
}
|
||||
if strings.HasPrefix(trimmed, `\\.\pipe\`) {
|
||||
return trimmed
|
||||
}
|
||||
if strings.HasPrefix(trimmed, `//./pipe/`) {
|
||||
return `\\.\pipe\` + strings.TrimPrefix(trimmed, `//./pipe/`)
|
||||
}
|
||||
trimmed = strings.TrimPrefix(trimmed, `\\`)
|
||||
trimmed = strings.TrimPrefix(trimmed, `//`)
|
||||
trimmed = strings.TrimPrefix(trimmed, `.\pipe\`)
|
||||
trimmed = strings.TrimPrefix(trimmed, `./pipe/`)
|
||||
trimmed = strings.TrimPrefix(trimmed, `pipe\`)
|
||||
trimmed = strings.TrimPrefix(trimmed, `pipe/`)
|
||||
trimmed = strings.TrimLeft(strings.ReplaceAll(trimmed, "/", `\`), `\`)
|
||||
return `\\.\pipe\` + trimmed
|
||||
}
|
||||
|
||||
func ConnRemoteAddrString(conn net.Conn) string {
|
||||
if conn == nil {
|
||||
return "unknown"
|
||||
}
|
||||
addr := conn.RemoteAddr()
|
||||
if addr == nil {
|
||||
return "unknown"
|
||||
}
|
||||
if value := addr.String(); value != "" {
|
||||
return value
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
//go:build !windows
|
||||
|
||||
package transport
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestDialNamedPipeUnsupportedOnNonWindows(t *testing.T) {
|
||||
_, err := DialTimeout("npipe", "notify-demo", time.Millisecond)
|
||||
if !errors.Is(err, ErrNamedPipeUnsupported) {
|
||||
t.Fatalf("DialTimeout error = %v, want %v", err, ErrNamedPipeUnsupported)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListenNamedPipeUnsupportedOnNonWindows(t *testing.T) {
|
||||
_, err := Listen("npipe", "notify-demo")
|
||||
if !errors.Is(err, ErrNamedPipeUnsupported) {
|
||||
t.Fatalf("Listen error = %v, want %v", err, ErrNamedPipeUnsupported)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package transport
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNamedPipeNetworkAliases(t *testing.T) {
|
||||
tests := []struct {
|
||||
network string
|
||||
want bool
|
||||
}{
|
||||
{network: "npipe", want: true},
|
||||
{network: "pipe", want: true},
|
||||
{network: "namedpipe", want: true},
|
||||
{network: "named-pipe", want: true},
|
||||
{network: "tcp", want: false},
|
||||
{network: "unix", want: false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
if got := IsNamedPipeNetwork(tt.network); got != tt.want {
|
||||
t.Fatalf("IsNamedPipeNetwork(%q) = %v, want %v", tt.network, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeNamedPipeAddr(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
addr string
|
||||
want string
|
||||
}{
|
||||
{name: "short-name", addr: "notify-demo", want: `\\.\pipe\notify-demo`},
|
||||
{name: "pipe-prefix", addr: `pipe\notify-demo`, want: `\\.\pipe\notify-demo`},
|
||||
{name: "slash-prefix", addr: "//./pipe/notify-demo", want: `\\.\pipe\notify-demo`},
|
||||
{name: "normalized", addr: `\\.\pipe\notify-demo`, want: `\\.\pipe\notify-demo`},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := NormalizeNamedPipeAddr(tt.addr); got != tt.want {
|
||||
t.Fatalf("NormalizeNamedPipeAddr(%q) = %q, want %q", tt.addr, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
//go:build windows
|
||||
|
||||
package transport
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNamedPipeRoundTripByteMode(t *testing.T) {
|
||||
pipeName := fmt.Sprintf("notify-npipe-test-%d", time.Now().UnixNano())
|
||||
listener, err := Listen("npipe", pipeName)
|
||||
if err != nil {
|
||||
t.Fatalf("Listen failed: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
_ = listener.Close()
|
||||
}()
|
||||
|
||||
serverErr := make(chan error, 1)
|
||||
go func() {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
serverErr <- err
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
_ = conn.Close()
|
||||
}()
|
||||
|
||||
buf := make([]byte, 4)
|
||||
if _, err := io.ReadFull(conn, buf); err != nil {
|
||||
serverErr <- err
|
||||
return
|
||||
}
|
||||
if got, want := string(buf), "ping"; got != want {
|
||||
serverErr <- fmt.Errorf("server got %q, want %q", got, want)
|
||||
return
|
||||
}
|
||||
if _, err := conn.Write([]byte("pong")); err != nil {
|
||||
serverErr <- err
|
||||
return
|
||||
}
|
||||
serverErr <- nil
|
||||
}()
|
||||
|
||||
conn, err := DialTimeout("npipe", pipeName, 2*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("DialTimeout failed: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
_ = conn.Close()
|
||||
}()
|
||||
|
||||
if _, err := conn.Write([]byte("ping")); err != nil {
|
||||
t.Fatalf("client write failed: %v", err)
|
||||
}
|
||||
|
||||
reply := make([]byte, 4)
|
||||
if _, err := io.ReadFull(conn, reply); err != nil {
|
||||
t.Fatalf("client read failed: %v", err)
|
||||
}
|
||||
if got, want := string(reply), "pong"; got != want {
|
||||
t.Fatalf("client got %q, want %q", got, want)
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-serverErr:
|
||||
if err != nil {
|
||||
t.Fatalf("server error: %v", err)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("server timeout")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user