升级至8069版本:版本号更新/代理配置系统/红包计时埋点/长连接重构/回调修复

This commit is contained in:
2026-02-26 10:44:13 +08:00
parent 7cbd3d061d
commit 40a74d2ea7
38 changed files with 3639 additions and 235 deletions
+108 -21
View File
@@ -17,46 +17,94 @@ func MMLongConnect(mmInfo *MMInfo) error {
strPort := strconv.Itoa(int(mmInfo.LONGPort))
serverAddr := mmInfo.LongHost + ":" + strPort
fmt.Println("MMLongConnect serverAddr: ", serverAddr)
mminfoDialer := "0"
if mmInfo.Dialer != nil {
mminfoDialer = "1"
}
// mminfoDialer := "0"
// if mmInfo.Dialer != nil {
// mminfoDialer = "1"
// }
// 打印 mmInfo.Dialer
fmt.Println("MMLongConnect mmInfo.Dialer: ", mminfoDialer)
// fmt.Println("MMLongConnect mmInfo.Dialer: ", mminfoDialer)
// 从MMInfo获取重试参数,如果未设置则使用默认值
maxRetries := mmInfo.LongConnRetryTimes
if maxRetries <= 0 {
maxRetries = 30
}
retryInterval := time.Duration(mmInfo.LongConnRetryInterval) * time.Millisecond
if retryInterval <= 0 {
retryInterval = 500 * time.Millisecond
}
connTimeout := time.Duration(mmInfo.LongConnTimeout) * time.Second
if connTimeout <= 0 {
connTimeout = 15 * time.Second
}
// 调试:打印 AllowDirectOnProxyFail 的值
fmt.Printf("MMLongConnect AllowDirectOnProxyFail: %v\n", mmInfo.AllowDirectOnProxyFail)
// 定义
var conn net.Conn
var err error
for i := 0; i < 30; i++ {
if mmInfo.Dialer != nil {
proxyFailed := false // 标记代理是否已经失败过
for i := 0; i < maxRetries; i++ {
// 如果有代理且代理没有被标记为失败,尝试代理连接
if mmInfo.Dialer != nil && !proxyFailed {
// 使用代理连接
conn, err = mmInfo.Dialer.Dial("tcp4", serverAddr)
if err != nil {
baseutils.PrintLog(fmt.Sprintf("MMLongConnect attempt %d failed: %s", i+1, err.Error()))
// 等待 500 毫秒重时 (最大 30 次)
time.Sleep(500 * time.Millisecond)
baseutils.PrintLog(fmt.Sprintf("MMLongConnect with proxy attempt %d/%d failed: %s", i+1, maxRetries, err.Error()))
// 检查是否允许降级到直连
if mmInfo.AllowDirectOnProxyFail {
baseutils.PrintLog("MMLongConnect: Proxy failed, falling back to direct connection")
proxyFailed = true // 标记代理失败,后续使用直连
// 不等待,立即尝试直连
} else {
// 不允许直连,等待后重试代理
time.Sleep(retryInterval)
}
continue
}
// 设置连接超时
if err := conn.SetDeadline(time.Now().Add(connTimeout)); err != nil {
baseutils.PrintLog(fmt.Sprintf("MMLongConnect SetDeadline failed: %s", err.Error()))
conn.Close()
time.Sleep(retryInterval)
continue
}
mmInfo.Conn = conn
mmInfo.reader = bufio.NewReader(conn)
fmt.Println("MMLongConnect success!")
fmt.Println("MMLongConnect with proxy success!")
return nil
}
// 没有使用代理
conn, err = net.Dial("tcp4", serverAddr)
// 没有使用代理或代理已失败 - 直连
conn, err = net.DialTimeout("tcp4", serverAddr, connTimeout)
if err != nil {
baseutils.PrintLog(fmt.Sprintf("MMLongConnect attempt %d failed: %s", i+1, err.Error()))
// 等待 500 毫秒重时 (最大 30 次)
time.Sleep(500 * time.Millisecond)
baseutils.PrintLog(fmt.Sprintf("MMLongConnect direct attempt %d/%d failed: %s", i+1, maxRetries, err.Error()))
// 等待重试
time.Sleep(retryInterval)
continue
}
// 设置连接超时
if err := conn.SetDeadline(time.Now().Add(connTimeout)); err != nil {
baseutils.PrintLog(fmt.Sprintf("MMLongConnect SetDeadline failed: %s", err.Error()))
conn.Close()
time.Sleep(retryInterval)
continue
}
mmInfo.Conn = conn
mmInfo.reader = bufio.NewReader(conn)
if proxyFailed {
fmt.Println("MMLongConnect direct success (after proxy fallback)!")
} else {
fmt.Println("MMLongConnect direct success!")
}
return nil
}
if err != nil {
return err
return fmt.Errorf("MMLongConnect failed after %d attempts: %w", maxRetries, err)
}
return nil
return errors.New("MMLongConnect failed: unknown error")
}
// MMTCPSendData MMTCPSendData 长链接发送数据
@@ -70,11 +118,20 @@ func MMTCPSendData(mmInfo *MMInfo, data []byte) error {
}
}
// 设置写超时
writeTimeout := time.Duration(mmInfo.LongConnTimeout) * time.Second
if writeTimeout <= 0 {
writeTimeout = 15 * time.Second
}
if err := mmInfo.Conn.SetWriteDeadline(time.Now().Add(writeTimeout)); err != nil {
return fmt.Errorf("SetWriteDeadline failed: %w", err)
}
// 发送数据
length, err := mmInfo.Conn.Write(data)
// 判断是否出错
if err != nil {
return err
return fmt.Errorf("write failed: %w", err)
}
// 判断数据是否发送完毕
if length != len(data) {
@@ -87,6 +144,18 @@ func MMTCPSendData(mmInfo *MMInfo, data []byte) error {
// MMTCPRecvItems 循环接收长链接数据
// Deprecated
func MMTCPRecvItems(mmInfo *MMInfo) ([]*PackItem, error) {
// 设置读超时
readTimeout := time.Duration(mmInfo.LongConnReadTimeout) * time.Second
if readTimeout <= 0 {
readTimeout = time.Duration(mmInfo.LongConnTimeout) * time.Second
if readTimeout <= 0 {
readTimeout = 15 * time.Second
}
}
if err := mmInfo.Conn.SetReadDeadline(time.Now().Add(readTimeout)); err != nil {
return nil, fmt.Errorf("SetReadDeadline failed: %w", err)
}
// 接收返回数据
recvBuf := make([]byte, 10240)
recvLen, err := mmInfo.Conn.Read(recvBuf)
@@ -107,17 +176,35 @@ func MMTCPRecvOneItem(mmInfo *MMInfo) (*PackItem, error) {
return nil, errors.New("mmInfo.Conn or mmInfo.reader is nil")
}
// 设置读超时
readTimeout := time.Duration(mmInfo.LongConnReadTimeout) * time.Second
if readTimeout <= 0 {
readTimeout = time.Duration(mmInfo.LongConnTimeout) * time.Second
if readTimeout <= 0 {
readTimeout = 15 * time.Second
}
}
if err := mmInfo.Conn.SetReadDeadline(time.Now().Add(readTimeout)); err != nil {
return nil, fmt.Errorf("SetReadDeadline failed: %w", err)
}
// 读取头部数据
recordHeadData := make([]byte, 5)
if _, err := io.ReadFull(mmInfo.reader, recordHeadData); err != nil {
return nil, err
return nil, fmt.Errorf("read header failed: %w", err)
}
// 读取Content
recordHead := RecordHeadDeSerialize(recordHeadData)
bodyData := make([]byte, recordHead.Size)
// 重新设置读超时(针对body
if err := mmInfo.Conn.SetReadDeadline(time.Now().Add(readTimeout)); err != nil {
return nil, fmt.Errorf("SetReadDeadline for body failed: %w", err)
}
if _, err := io.ReadFull(mmInfo.reader, bodyData); err != nil {
return nil, err
return nil, fmt.Errorf("read body failed: %w", err)
}
return &PackItem{
RecordHead: recordHeadData,