优化协议的Processor函数参数

This commit is contained in:
duanhf2012
2022-01-11 13:57:39 +08:00
parent 313cc032a1
commit e676587b4d
5 changed files with 55 additions and 35 deletions

View File

@@ -1,11 +1,11 @@
package network
import (
"fmt"
"github.com/duanhf2012/origin/log"
"net"
"sync"
"time"
"errors"
)
type ConnSet map[net.Conn]struct{}
@@ -86,27 +86,28 @@ func (tcpConn *TCPConn) GetRemoteIp() string {
return tcpConn.conn.RemoteAddr().String()
}
func (tcpConn *TCPConn) doWrite(b []byte) {
func (tcpConn *TCPConn) doWrite(b []byte) error{
if len(tcpConn.writeChan) == cap(tcpConn.writeChan) {
tcpConn.ReleaseReadMsg(b)
log.SError("close conn: channel full")
tcpConn.doDestroy()
return
return errors.New("close conn: channel full")
}
tcpConn.writeChan <- b
return nil
}
// b must not be modified by the others goroutines
func (tcpConn *TCPConn) Write(b []byte) {
func (tcpConn *TCPConn) Write(b []byte) error{
tcpConn.Lock()
defer tcpConn.Unlock()
if tcpConn.closeFlag || b == nil {
tcpConn.ReleaseReadMsg(b)
return
return errors.New("conn is close")
}
tcpConn.doWrite(b)
return tcpConn.doWrite(b)
}
func (tcpConn *TCPConn) Read(b []byte) (int, error) {
@@ -131,11 +132,20 @@ func (tcpConn *TCPConn) ReleaseReadMsg(byteBuff []byte){
func (tcpConn *TCPConn) WriteMsg(args ...[]byte) error {
if tcpConn.closeFlag == true {
return fmt.Errorf("conn is close")
return errors.New("conn is close")
}
return tcpConn.msgParser.Write(tcpConn, args...)
}
func (tcpConn *TCPConn) WriteRawMsg(args []byte) error {
if tcpConn.closeFlag == true {
return errors.New("conn is close")
}
return tcpConn.Write(args)
}
func (tcpConn *TCPConn) IsConnected() bool {
return tcpConn.closeFlag == false
}