mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-03 22:45:13 +08:00
优化协议的Processor函数参数
This commit is contained in:
@@ -38,14 +38,14 @@ func (pbRawProcessor *PBRawProcessor) SetByteOrder(littleEndian bool) {
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (pbRawProcessor *PBRawProcessor ) MsgRoute(msg interface{},userdata interface{}) error{
|
||||
func (pbRawProcessor *PBRawProcessor ) MsgRoute(clientId uint64, msg interface{}) error{
|
||||
pPackInfo := msg.(*PBRawPackInfo)
|
||||
pbRawProcessor.msgHandler(userdata.(uint64),pPackInfo.typ,pPackInfo.rawMsg)
|
||||
pbRawProcessor.msgHandler(clientId,pPackInfo.typ,pPackInfo.rawMsg)
|
||||
return nil
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (pbRawProcessor *PBRawProcessor ) Unmarshal(data []byte) (interface{}, error) {
|
||||
func (pbRawProcessor *PBRawProcessor ) Unmarshal(clientId uint64,data []byte) (interface{}, error) {
|
||||
var msgType uint16
|
||||
if pbRawProcessor.LittleEndian == true {
|
||||
msgType = binary.LittleEndian.Uint16(data[:2])
|
||||
@@ -57,7 +57,7 @@ func (pbRawProcessor *PBRawProcessor ) Unmarshal(data []byte) (interface{}, erro
|
||||
}
|
||||
|
||||
// must goroutine safe
|
||||
func (pbRawProcessor *PBRawProcessor ) Marshal(msg interface{}) ([]byte, error){
|
||||
func (pbRawProcessor *PBRawProcessor ) Marshal(clientId uint64,msg interface{}) ([]byte, error){
|
||||
pMsg := msg.(*PBRawPackInfo)
|
||||
|
||||
buff := make([]byte, 2, len(pMsg.rawMsg)+RawMsgTypeSize)
|
||||
@@ -81,20 +81,20 @@ func (pbRawProcessor *PBRawProcessor) MakeRawMsg(msgType uint16,msg []byte,pbRaw
|
||||
//return &PBRawPackInfo{typ:msgType,rawMsg:msg}
|
||||
}
|
||||
|
||||
func (pbRawProcessor *PBRawProcessor) UnknownMsgRoute(msg interface{}, userData interface{}){
|
||||
func (pbRawProcessor *PBRawProcessor) UnknownMsgRoute(clientId uint64,msg interface{}){
|
||||
if pbRawProcessor.unknownMessageHandler == nil {
|
||||
return
|
||||
}
|
||||
pbRawProcessor.unknownMessageHandler(userData.(uint64),msg.([]byte))
|
||||
pbRawProcessor.unknownMessageHandler(clientId,msg.([]byte))
|
||||
}
|
||||
|
||||
// connect event
|
||||
func (pbRawProcessor *PBRawProcessor) ConnectedRoute(userData interface{}){
|
||||
pbRawProcessor.connectHandler(userData.(uint64))
|
||||
func (pbRawProcessor *PBRawProcessor) ConnectedRoute(clientId uint64){
|
||||
pbRawProcessor.connectHandler(clientId)
|
||||
}
|
||||
|
||||
func (pbRawProcessor *PBRawProcessor) DisConnectedRoute(userData interface{}){
|
||||
pbRawProcessor.disconnectHandler(userData.(uint64))
|
||||
func (pbRawProcessor *PBRawProcessor) DisConnectedRoute(clientId uint64){
|
||||
pbRawProcessor.disconnectHandler(clientId)
|
||||
}
|
||||
|
||||
func (pbRawProcessor *PBRawProcessor) SetUnknownMsgHandler(unknownMessageHandler UnknownRawMessageHandler){
|
||||
|
||||
@@ -3,30 +3,30 @@ package processor
|
||||
|
||||
type IProcessor interface {
|
||||
// must goroutine safe
|
||||
MsgRoute(msg interface{}, userData interface{}) error
|
||||
MsgRoute(clientId uint64,msg interface{}) error
|
||||
//must goroutine safe
|
||||
UnknownMsgRoute(msg interface{}, userData interface{})
|
||||
UnknownMsgRoute(clientId uint64,msg interface{})
|
||||
// connect event
|
||||
ConnectedRoute(userData interface{})
|
||||
DisConnectedRoute(userData interface{})
|
||||
ConnectedRoute(clientId uint64)
|
||||
DisConnectedRoute(clientId uint64)
|
||||
|
||||
// must goroutine safe
|
||||
Unmarshal(data []byte) (interface{}, error)
|
||||
Unmarshal(clientId uint64,data []byte) (interface{}, error)
|
||||
// must goroutine safe
|
||||
Marshal(msg interface{}) ([]byte, error)
|
||||
Marshal(clientId uint64,msg interface{}) ([]byte, error)
|
||||
}
|
||||
|
||||
type IRawProcessor interface {
|
||||
SetByteOrder(littleEndian bool)
|
||||
MsgRoute(msg interface{},userdata interface{}) error
|
||||
Unmarshal(data []byte) (interface{}, error)
|
||||
Marshal(msg interface{}) ([]byte, error)
|
||||
MsgRoute(clientId uint64,msg interface{}) error
|
||||
Unmarshal(clientId uint64,data []byte) (interface{}, error)
|
||||
Marshal(clientId uint64,msg interface{}) ([]byte, error)
|
||||
|
||||
SetRawMsgHandler(handle RawMessageHandler)
|
||||
MakeRawMsg(msgType uint16,msg []byte,pbRawPackInfo *PBRawPackInfo)
|
||||
UnknownMsgRoute(msg interface{}, userData interface{})
|
||||
ConnectedRoute(userData interface{})
|
||||
DisConnectedRoute(userData interface{})
|
||||
UnknownMsgRoute(clientId uint64,msg interface{})
|
||||
ConnectedRoute(clientId uint64)
|
||||
DisConnectedRoute(clientId uint64)
|
||||
|
||||
SetUnknownMsgHandler(unknownMessageHandler UnknownRawMessageHandler)
|
||||
SetConnectedHandler(connectHandler RawConnectHandler)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user