new server
This commit is contained in:
19
models/entity.go
Normal file
19
models/entity.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package models
|
||||
|
||||
type EntityType uint8
|
||||
|
||||
const (
|
||||
EntityPlayer EntityType = 1
|
||||
EntityNPC EntityType = 2
|
||||
EntityBullet EntityType = 3
|
||||
)
|
||||
|
||||
type Entity struct {
|
||||
ID uint32
|
||||
Type EntityType
|
||||
|
||||
X float32
|
||||
Y float32
|
||||
Z float32
|
||||
Yaw float32
|
||||
}
|
||||
@@ -1,6 +1,48 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
Type int8
|
||||
Payload interface{}
|
||||
Type uint16
|
||||
Version uint16
|
||||
Payload []byte
|
||||
}
|
||||
|
||||
func (m *Message) Encode() []byte {
|
||||
payloadLen := len(m.Payload)
|
||||
if payloadLen > 0xFFFF {
|
||||
return nil
|
||||
}
|
||||
|
||||
buf := make([]byte, 8+payloadLen)
|
||||
|
||||
binary.LittleEndian.PutUint16(buf[0:2], m.Type)
|
||||
binary.LittleEndian.PutUint16(buf[2:4], m.Version)
|
||||
binary.LittleEndian.PutUint16(buf[4:6], uint16(payloadLen))
|
||||
|
||||
copy(buf[8:], m.Payload)
|
||||
return buf
|
||||
}
|
||||
|
||||
func Decode(data []byte) (*Message, error) {
|
||||
if len(data) < 8 {
|
||||
return nil, errors.New("message too short")
|
||||
}
|
||||
|
||||
payloadLen := binary.LittleEndian.Uint16(data[4:6])
|
||||
if len(data) != int(8+payloadLen) {
|
||||
return nil, errors.New("invalid payload length")
|
||||
}
|
||||
|
||||
msg := &Message{
|
||||
Type: binary.LittleEndian.Uint16(data[0:2]),
|
||||
Version: binary.LittleEndian.Uint16(data[2:4]),
|
||||
Payload: make([]byte, payloadLen),
|
||||
}
|
||||
|
||||
copy(msg.Payload, data[8:])
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
@@ -3,17 +3,7 @@ package models
|
||||
import "github.com/gorilla/websocket"
|
||||
|
||||
type Player struct {
|
||||
ID uint32
|
||||
|
||||
X float32
|
||||
Y float32
|
||||
Z float32
|
||||
|
||||
InputX float32
|
||||
InputZ float32
|
||||
|
||||
Name string
|
||||
PhotoURL string
|
||||
|
||||
Entity
|
||||
Name string
|
||||
Conn *websocket.Conn
|
||||
}
|
||||
|
||||
50
models/reader.go
Normal file
50
models/reader.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"math"
|
||||
)
|
||||
|
||||
type Reader struct {
|
||||
buf []byte
|
||||
off int
|
||||
}
|
||||
|
||||
func NewReader(b []byte) *Reader {
|
||||
return &Reader{buf: b}
|
||||
}
|
||||
|
||||
func (r *Reader) ReadU8() uint8 {
|
||||
v := r.buf[r.off]
|
||||
r.off++
|
||||
return v
|
||||
}
|
||||
|
||||
func (r *Reader) ReadU16() uint16 {
|
||||
v := binary.LittleEndian.Uint16(r.buf[r.off:])
|
||||
r.off += 2
|
||||
return v
|
||||
}
|
||||
|
||||
func (r *Reader) ReadU32() uint32 {
|
||||
v := binary.LittleEndian.Uint32(r.buf[r.off:])
|
||||
r.off += 4
|
||||
return v
|
||||
}
|
||||
|
||||
func (r *Reader) ReadF32() float32 {
|
||||
v := math.Float32frombits(binary.LittleEndian.Uint32(r.buf[r.off:]))
|
||||
r.off += 4
|
||||
return v
|
||||
}
|
||||
|
||||
func (r *Reader) ReadBool() bool {
|
||||
return r.ReadU8() == 1
|
||||
}
|
||||
|
||||
func (r *Reader) ReadString() string {
|
||||
l := r.ReadU16()
|
||||
s := string(r.buf[r.off : r.off+int(l)])
|
||||
r.off += int(l)
|
||||
return s
|
||||
}
|
||||
49
models/writer.go
Normal file
49
models/writer.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"math"
|
||||
)
|
||||
|
||||
type Writer struct {
|
||||
buf []byte
|
||||
}
|
||||
|
||||
func (w *Writer) Bytes() []byte {
|
||||
return w.buf
|
||||
}
|
||||
|
||||
func (w *Writer) WriteU8(v uint8) {
|
||||
w.buf = append(w.buf, v)
|
||||
}
|
||||
|
||||
func (w *Writer) WriteU16(v uint16) {
|
||||
tmp := make([]byte, 2)
|
||||
binary.LittleEndian.PutUint16(tmp, v)
|
||||
w.buf = append(w.buf, tmp...)
|
||||
}
|
||||
|
||||
func (w *Writer) WriteU32(v uint32) {
|
||||
tmp := make([]byte, 4)
|
||||
binary.LittleEndian.PutUint32(tmp, v)
|
||||
w.buf = append(w.buf, tmp...)
|
||||
}
|
||||
|
||||
func (w *Writer) WriteF32(v float32) {
|
||||
tmp := make([]byte, 4)
|
||||
binary.LittleEndian.PutUint32(tmp, math.Float32bits(v))
|
||||
w.buf = append(w.buf, tmp...)
|
||||
}
|
||||
|
||||
func (w *Writer) WriteBool(v bool) {
|
||||
if v {
|
||||
w.WriteU8(1)
|
||||
} else {
|
||||
w.WriteU8(0)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Writer) WriteString(s string) {
|
||||
w.WriteU16(uint16(len(s)))
|
||||
w.buf = append(w.buf, []byte(s)...)
|
||||
}
|
||||
Reference in New Issue
Block a user