2023-12-01 23:11:41 +00:00
|
|
|
package cnet
|
2023-03-05 08:14:36 +00:00
|
|
|
|
|
|
|
import (
|
2023-12-01 06:56:34 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2023-03-05 08:14:36 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2023-03-11 01:56:05 +00:00
|
|
|
"io"
|
2023-03-05 08:14:36 +00:00
|
|
|
"net"
|
2023-11-28 03:23:28 +00:00
|
|
|
"sync/atomic"
|
2023-12-01 21:29:19 +00:00
|
|
|
|
2023-12-02 01:56:23 +00:00
|
|
|
"github.com/CPunch/gopenfusion/cnet/protocol"
|
2023-03-05 08:14:36 +00:00
|
|
|
)
|
|
|
|
|
2023-03-07 07:20:36 +00:00
|
|
|
const (
|
|
|
|
USE_E = iota
|
|
|
|
USE_FE
|
|
|
|
)
|
|
|
|
|
2023-12-01 06:56:34 +00:00
|
|
|
type PacketEvent struct {
|
|
|
|
Type int
|
|
|
|
Pkt *bytes.Buffer
|
|
|
|
PktID uint32
|
|
|
|
}
|
|
|
|
|
2023-12-02 01:15:00 +00:00
|
|
|
// Peer is a simple wrapper for net.Conn connections to send/recv packets over the Fusionfall packet protocol.
|
|
|
|
type Peer struct {
|
2023-12-01 06:56:34 +00:00
|
|
|
uData interface{}
|
2023-11-28 03:23:28 +00:00
|
|
|
conn net.Conn
|
2023-12-01 06:56:34 +00:00
|
|
|
ctx context.Context
|
2023-11-28 03:23:28 +00:00
|
|
|
whichKey int
|
|
|
|
alive *atomic.Bool
|
|
|
|
|
|
|
|
// May not be set while Send() or Handler() are concurrently running.
|
|
|
|
E_key []byte
|
|
|
|
|
|
|
|
// May not be set while Send() or Handler() are concurrently running.
|
|
|
|
FE_key []byte
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
|
2023-12-02 01:15:00 +00:00
|
|
|
func NewPeer(ctx context.Context, conn net.Conn) *Peer {
|
|
|
|
p := &Peer{
|
2023-11-28 03:23:28 +00:00
|
|
|
conn: conn,
|
2023-12-01 06:56:34 +00:00
|
|
|
ctx: ctx,
|
2023-11-28 03:23:28 +00:00
|
|
|
whichKey: USE_E,
|
|
|
|
alive: &atomic.Bool{},
|
|
|
|
|
2023-12-01 21:29:19 +00:00
|
|
|
E_key: []byte(protocol.DEFAULT_KEY),
|
2023-11-28 03:23:28 +00:00
|
|
|
FE_key: nil,
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
2023-11-28 03:23:28 +00:00
|
|
|
|
|
|
|
return p
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
|
2023-12-02 01:15:00 +00:00
|
|
|
func (peer *Peer) SetUserData(uData interface{}) {
|
2023-12-01 06:56:34 +00:00
|
|
|
peer.uData = uData
|
|
|
|
}
|
|
|
|
|
2023-12-02 01:15:00 +00:00
|
|
|
func (peer *Peer) UserData() interface{} {
|
2023-12-01 06:56:34 +00:00
|
|
|
return peer.uData
|
|
|
|
}
|
|
|
|
|
2023-12-02 01:15:00 +00:00
|
|
|
func (peer *Peer) Send(typeID uint32, data ...interface{}) error {
|
2023-03-11 09:28:11 +00:00
|
|
|
// grab buffer from pool
|
2023-12-01 21:29:19 +00:00
|
|
|
buf := protocol.GetBuffer()
|
|
|
|
defer protocol.PutBuffer(buf)
|
2023-03-11 01:56:05 +00:00
|
|
|
|
2023-08-23 22:38:10 +00:00
|
|
|
// allocate space for packet size
|
|
|
|
buf.Write(make([]byte, 4))
|
|
|
|
|
2023-03-11 05:59:13 +00:00
|
|
|
// body start
|
2023-12-01 21:29:19 +00:00
|
|
|
pkt := protocol.NewPacket(buf)
|
2023-03-05 08:14:36 +00:00
|
|
|
|
2023-03-11 05:59:13 +00:00
|
|
|
// encode type id
|
2023-03-11 09:28:11 +00:00
|
|
|
if err := pkt.Encode(typeID); err != nil {
|
2023-03-11 05:59:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-03-05 08:14:36 +00:00
|
|
|
|
2023-03-11 05:59:13 +00:00
|
|
|
// encode data
|
|
|
|
for _, trailer := range data {
|
|
|
|
if err := pkt.Encode(trailer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-03-05 08:14:36 +00:00
|
|
|
|
2023-08-23 22:38:10 +00:00
|
|
|
// prepend the packet size
|
|
|
|
binary.LittleEndian.PutUint32(buf.Bytes()[:4], uint32(buf.Len()-4))
|
|
|
|
|
2023-03-11 05:59:13 +00:00
|
|
|
// encrypt body
|
2023-11-28 03:23:28 +00:00
|
|
|
var key []byte
|
2023-03-11 05:59:13 +00:00
|
|
|
switch peer.whichKey {
|
2023-03-07 07:20:36 +00:00
|
|
|
case USE_E:
|
2023-11-28 03:23:28 +00:00
|
|
|
key = peer.E_key
|
2023-03-07 07:20:36 +00:00
|
|
|
case USE_FE:
|
2023-11-28 03:23:28 +00:00
|
|
|
key = peer.FE_key
|
2023-03-11 05:59:13 +00:00
|
|
|
}
|
2023-12-01 21:29:19 +00:00
|
|
|
protocol.EncryptData(buf.Bytes()[4:], key)
|
2023-03-11 05:59:13 +00:00
|
|
|
|
2023-08-23 22:38:10 +00:00
|
|
|
// send full packet
|
2023-12-01 06:56:34 +00:00
|
|
|
// log.Printf("Sending %#v, sizeof: %d, buffer: %v", data, buf.Len(), buf.Bytes())
|
2023-03-11 05:59:13 +00:00
|
|
|
if _, err := peer.conn.Write(buf.Bytes()); err != nil {
|
2023-08-23 22:38:10 +00:00
|
|
|
return fmt.Errorf("failed to write packet body! %v", err)
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
2023-03-11 02:30:16 +00:00
|
|
|
return nil
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
|
2023-12-02 01:15:00 +00:00
|
|
|
func (peer *Peer) SetActiveKey(whichKey int) {
|
2023-03-22 22:07:16 +00:00
|
|
|
peer.whichKey = whichKey
|
|
|
|
}
|
|
|
|
|
2023-12-02 01:15:00 +00:00
|
|
|
func (peer *Peer) Kill() {
|
2023-11-30 01:57:45 +00:00
|
|
|
// de-bounce: only kill if alive
|
2023-11-28 03:34:39 +00:00
|
|
|
if !peer.alive.CompareAndSwap(true, false) {
|
2023-03-09 07:48:13 +00:00
|
|
|
return
|
2023-03-07 07:20:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 05:59:13 +00:00
|
|
|
peer.conn.Close()
|
2023-03-07 07:20:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-18 21:40:20 +00:00
|
|
|
// meant to be invoked as a goroutine
|
2023-12-02 01:15:00 +00:00
|
|
|
func (peer *Peer) Handler(eRecv chan<- *PacketEvent) error {
|
2023-11-30 01:57:45 +00:00
|
|
|
defer func() {
|
|
|
|
close(eRecv)
|
|
|
|
peer.Kill()
|
|
|
|
}()
|
2023-03-05 08:14:36 +00:00
|
|
|
|
2023-11-28 03:23:28 +00:00
|
|
|
peer.alive.Store(true)
|
2023-03-05 08:14:36 +00:00
|
|
|
for {
|
2023-12-01 06:56:34 +00:00
|
|
|
select {
|
|
|
|
case <-peer.ctx.Done():
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
// read packet size, the goroutine spends most of it's time parked here
|
|
|
|
var sz uint32
|
|
|
|
if err := binary.Read(peer.conn, binary.LittleEndian, &sz); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// client should never send a packet size outside of this range
|
2023-12-01 21:29:19 +00:00
|
|
|
if sz > protocol.CN_PACKET_BUFFER_SIZE || sz < 4 {
|
2023-12-01 06:56:34 +00:00
|
|
|
return fmt.Errorf("invalid packet size: %d", sz)
|
|
|
|
}
|
|
|
|
|
|
|
|
// grab buffer && read packet body
|
2023-12-01 21:29:19 +00:00
|
|
|
buf := protocol.GetBuffer()
|
2023-12-01 06:56:34 +00:00
|
|
|
if _, err := buf.ReadFrom(io.LimitReader(peer.conn, int64(sz))); err != nil {
|
|
|
|
return fmt.Errorf("failed to read packet body: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt
|
2023-12-01 21:29:19 +00:00
|
|
|
protocol.DecryptData(buf.Bytes(), peer.E_key)
|
|
|
|
pkt := protocol.NewPacket(buf)
|
2023-12-01 06:56:34 +00:00
|
|
|
|
|
|
|
// create packet && read pktID
|
|
|
|
var pktID uint32
|
|
|
|
if err := pkt.Decode(&pktID); err != nil {
|
|
|
|
return fmt.Errorf("failed to read packet type! %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// dispatch packet
|
|
|
|
// log.Printf("Got packet ID: %x, with a sizeof: %d\n", pktID, sz)
|
|
|
|
eRecv <- &PacketEvent{Pkt: buf, PktID: pktID}
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|