mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-03 22:45:13 +08:00
TcpService服务新增读写超时配置
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/duanhf2012/origin/log"
|
"github.com/duanhf2012/origin/log"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ConnSet map[net.Conn]struct{}
|
type ConnSet map[net.Conn]struct{}
|
||||||
@@ -138,3 +139,11 @@ func (tcpConn *TCPConn) WriteMsg(args ...[]byte) error {
|
|||||||
func (tcpConn *TCPConn) IsConnected() bool {
|
func (tcpConn *TCPConn) IsConnected() bool {
|
||||||
return tcpConn.closeFlag == false
|
return tcpConn.closeFlag == false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tcpConn *TCPConn) SetReadDeadline(d time.Duration) {
|
||||||
|
tcpConn.conn.SetReadDeadline(time.Now().Add(d))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tcpConn *TCPConn) SetWriteDeadline(d time.Duration) {
|
||||||
|
tcpConn.conn.SetWriteDeadline(time.Now().Add(d))
|
||||||
|
}
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ type TcpService struct {
|
|||||||
mapClientLocker sync.RWMutex
|
mapClientLocker sync.RWMutex
|
||||||
mapClient map[uint64] *Client
|
mapClient map[uint64] *Client
|
||||||
process processor.IProcessor
|
process processor.IProcessor
|
||||||
|
|
||||||
|
ReadDeadline time.Duration
|
||||||
|
WriteDeadline time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
type TcpPackType int8
|
type TcpPackType int8
|
||||||
@@ -34,6 +37,8 @@ const Default_PendingWriteNum = 10000
|
|||||||
const Default_LittleEndian = false
|
const Default_LittleEndian = false
|
||||||
const Default_MinMsgLen = 2
|
const Default_MinMsgLen = 2
|
||||||
const Default_MaxMsgLen = 65535
|
const Default_MaxMsgLen = 65535
|
||||||
|
const Default_ReadDeadline = 180 //30s
|
||||||
|
const Default_WriteDeadline = 180 //30s
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MaxNodeId = 1<<10 - 1 //Uint10
|
MaxNodeId = 1<<10 - 1 //Uint10
|
||||||
@@ -89,6 +94,9 @@ func (tcpService *TcpService) OnInit() error{
|
|||||||
tcpService.tcpServer.LittleEndian = Default_LittleEndian
|
tcpService.tcpServer.LittleEndian = Default_LittleEndian
|
||||||
tcpService.tcpServer.MinMsgLen = Default_MinMsgLen
|
tcpService.tcpServer.MinMsgLen = Default_MinMsgLen
|
||||||
tcpService.tcpServer.MaxMsgLen = Default_MaxMsgLen
|
tcpService.tcpServer.MaxMsgLen = Default_MaxMsgLen
|
||||||
|
tcpService.ReadDeadline = Default_ReadDeadline
|
||||||
|
tcpService.WriteDeadline = Default_WriteDeadline
|
||||||
|
|
||||||
MaxConnNum,ok := tcpCfg["MaxConnNum"]
|
MaxConnNum,ok := tcpCfg["MaxConnNum"]
|
||||||
if ok == true {
|
if ok == true {
|
||||||
tcpService.tcpServer.MaxConnNum = int(MaxConnNum.(float64))
|
tcpService.tcpServer.MaxConnNum = int(MaxConnNum.(float64))
|
||||||
@@ -109,6 +117,17 @@ func (tcpService *TcpService) OnInit() error{
|
|||||||
if ok == true {
|
if ok == true {
|
||||||
tcpService.tcpServer.MaxMsgLen = uint32(MaxMsgLen.(float64))
|
tcpService.tcpServer.MaxMsgLen = uint32(MaxMsgLen.(float64))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
readDeadline,ok := tcpCfg["ReadDeadline"]
|
||||||
|
if ok == true {
|
||||||
|
tcpService.ReadDeadline = time.Second*time.Duration(readDeadline.(float64))
|
||||||
|
}
|
||||||
|
|
||||||
|
writeDeadline,ok := tcpCfg["WriteDeadline"]
|
||||||
|
if ok == true {
|
||||||
|
tcpService.WriteDeadline = time.Second*time.Duration(writeDeadline.(float64))
|
||||||
|
}
|
||||||
|
|
||||||
tcpService.mapClient = make( map[uint64] *Client, tcpService.tcpServer.MaxConnNum)
|
tcpService.mapClient = make( map[uint64] *Client, tcpService.tcpServer.MaxConnNum)
|
||||||
tcpService.tcpServer.NewAgent = tcpService.NewClient
|
tcpService.tcpServer.NewAgent = tcpService.NewClient
|
||||||
tcpService.tcpServer.Start()
|
tcpService.tcpServer.Start()
|
||||||
@@ -166,6 +185,8 @@ func (slf *Client) Run() {
|
|||||||
if slf.tcpConn == nil {
|
if slf.tcpConn == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slf.tcpConn.SetReadDeadline(slf.tcpService.ReadDeadline)
|
||||||
bytes,err := slf.tcpConn.ReadMsg()
|
bytes,err := slf.tcpConn.ReadMsg()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debug("read client id %d is error:%+v",slf.id,err)
|
log.Debug("read client id %d is error:%+v",slf.id,err)
|
||||||
@@ -201,6 +222,7 @@ func (tcpService *TcpService) SendMsg(clientId uint64,msg interface{}) error{
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
client.tcpConn.SetWriteDeadline(tcpService.WriteDeadline)
|
||||||
return client.tcpConn.WriteMsg(bytes)
|
return client.tcpConn.WriteMsg(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,6 +261,7 @@ func (tcpService *TcpService) SendRawMsg(clientId uint64,msg []byte) error{
|
|||||||
return fmt.Errorf("client %d is disconnect!",clientId)
|
return fmt.Errorf("client %d is disconnect!",clientId)
|
||||||
}
|
}
|
||||||
tcpService.mapClientLocker.Unlock()
|
tcpService.mapClientLocker.Unlock()
|
||||||
|
client.tcpConn.SetWriteDeadline(tcpService.WriteDeadline)
|
||||||
return client.tcpConn.WriteMsg(msg)
|
return client.tcpConn.WriteMsg(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user