github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/network.go (about)

     1  package kiwi
     2  
     3  import (
     4  	"context"
     5  	"github.com/15mga/kiwi/util"
     6  )
     7  
     8  type (
     9  	FnAgent              func(IAgent)
    10  	FnAgentBool          func(IAgent, bool)
    11  	FnAgentErr           func(IAgent, *util.Err)
    12  	FnAgentBytes         func(IAgent, []byte)
    13  	AgentBytesToBytesErr func(IAgent, []byte) ([]byte, *util.Err)
    14  	AgentBytesToBytes    func(IAgent, []byte) []byte
    15  )
    16  
    17  // IAgent 连接代理接口
    18  type IAgent interface {
    19  	SetHead(key string, val any)
    20  	SetHeads(m util.M)
    21  	GetHead(key string) (any, bool)
    22  	DelHead(keys ...string)
    23  	CopyHead(m util.M)
    24  	SetCache(key string, val any)
    25  	SetCaches(m util.M)
    26  	GetCache(key string) (any, bool)
    27  	DelCache(keys ...string)
    28  	CopyCache(m util.M)
    29  	Id() string
    30  	SetId(id string)
    31  	Addr() string
    32  	Host() string
    33  	// Enable 状态
    34  	Enable() *util.Enable
    35  	// Send 发送数据
    36  	Send(bytes []byte) *util.Err
    37  	// Dispose 释放
    38  	Dispose()
    39  	BindConnected(fn FnAgent)
    40  	BindDisconnected(fn FnAgentErr)
    41  }
    42  
    43  // IListener 监听器
    44  type IListener interface {
    45  	// Addr 监听地址
    46  	Addr() string
    47  	Port() int
    48  	// Start 开始监听
    49  	Start() *util.Err
    50  	// Close 关闭监听
    51  	Close()
    52  }
    53  
    54  // IDialer 拨号器接口
    55  type IDialer interface {
    56  	// Name 拨号器名称
    57  	Name() string
    58  	// Connect 连接远程服务器
    59  	Connect(ctx context.Context) *util.Err
    60  	// Agent 连接代理
    61  	Agent() IAgent
    62  }
    63  
    64  type AgentRWMode uint8
    65  
    66  const (
    67  	AgentRW AgentRWMode = iota
    68  	AgentR
    69  	AgentW
    70  )
    71  
    72  // AgentOpt Agent代理选项
    73  type (
    74  	AgentOpt struct {
    75  		PacketMaxCap int        //最大包长
    76  		PacketMinCap int        //包长最小容量
    77  		OnErr        util.FnErr //处理错误
    78  		DeadlineSecs int
    79  		AgentMode    AgentRWMode
    80  		HeadLen      int
    81  	}
    82  	AgentOption func(o *AgentOpt)
    83  )
    84  
    85  // AgentPacketMaxCap 最大包长
    86  func AgentPacketMaxCap(packetMaxCap int) AgentOption {
    87  	return func(o *AgentOpt) {
    88  		o.PacketMaxCap = packetMaxCap
    89  	}
    90  }
    91  
    92  // AgentPacketMinCap 包长最小容量
    93  func AgentPacketMinCap(packetMinCap int) AgentOption {
    94  	return func(o *AgentOpt) {
    95  		o.PacketMinCap = packetMinCap
    96  	}
    97  }
    98  
    99  // AgentErr 连接关闭的回调,Error为空正常关闭
   100  func AgentErr(onErr util.FnErr) AgentOption {
   101  	return func(o *AgentOpt) {
   102  		o.OnErr = onErr
   103  	}
   104  }
   105  
   106  func AgentDeadline(secs int) AgentOption {
   107  	return func(o *AgentOpt) {
   108  		o.DeadlineSecs = secs
   109  	}
   110  }
   111  
   112  func AgentMode(mode AgentRWMode) AgentOption {
   113  	return func(o *AgentOpt) {
   114  		o.AgentMode = mode
   115  	}
   116  }
   117  
   118  func AgentHeadLen(length int) AgentOption {
   119  	return func(o *AgentOpt) {
   120  		o.HeadLen = length
   121  	}
   122  }