You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.0 KiB
Go
41 lines
1.0 KiB
Go
2 years ago
|
package packet
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"git.noahlan.cn/northlan/nnet/log"
|
||
|
"git.noahlan.cn/northlan/nnet/nface"
|
||
|
)
|
||
|
|
||
|
type DefaultProcessor struct{}
|
||
|
|
||
|
func NewDefaultProcessor() *DefaultProcessor {
|
||
|
return &DefaultProcessor{}
|
||
|
}
|
||
|
|
||
|
func (d *DefaultProcessor) ProcessPacket(conn nface.IConnection, packet interface{}) error {
|
||
|
p := packet.(*Packet)
|
||
|
switch p.Type {
|
||
|
case Handshake:
|
||
|
// TODO validate handshake
|
||
|
if _, err := conn.Conn().Write([]byte{}); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
conn.SetStatus(nface.StatusPrepare)
|
||
|
log.Debugf("Connection handshake Id=%d, Remote=%s", conn.ID(), conn.Conn().RemoteAddr())
|
||
|
case HandshakeAck:
|
||
|
conn.SetStatus(nface.StatusWorking)
|
||
|
log.Debugf("Receive handshake ACK Id=%d, Remote=%s", conn.ID(), conn.Conn().RemoteAddr())
|
||
|
|
||
|
case Data:
|
||
|
if conn.Status() < nface.StatusWorking {
|
||
|
return fmt.Errorf("receive data on socket which not yet ACK, session will be closed immediately, remote=%s",
|
||
|
conn.Conn().RemoteAddr())
|
||
|
}
|
||
|
// TODO message data 处理
|
||
|
case Heartbeat:
|
||
|
// expected
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|