new server
This commit is contained in:
55
controllers/connection.go
Normal file
55
controllers/connection.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"server/models"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Hub) readLoop(conn *websocket.Conn) {
|
||||||
|
handMessage := models.Message{Type: MSG_WELCOME}
|
||||||
|
_ = conn.WriteMessage(websocket.BinaryMessage, handMessage.Encode())
|
||||||
|
|
||||||
|
var player *models.Player
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if player != nil {
|
||||||
|
h.removePlayer(player.ID)
|
||||||
|
log.Println("Player left:", player.ID)
|
||||||
|
}
|
||||||
|
conn.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
for {
|
||||||
|
_, bytes, err := conn.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, err := models.Decode(bytes)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
switch msg.Type {
|
||||||
|
|
||||||
|
case MSG_WELCOME:
|
||||||
|
if player != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
player = h.handShake(msg.Payload, conn)
|
||||||
|
log.Println("Player joined:", player.ID)
|
||||||
|
|
||||||
|
case MSG_INPUT:
|
||||||
|
if player == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
reader := models.NewReader(msg.Payload)
|
||||||
|
x := reader.ReadF32()
|
||||||
|
y := reader.ReadF32()
|
||||||
|
h.updatePosition(x, y, player)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,15 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"server/models"
|
"server/models"
|
||||||
|
"sync"
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Hub struct {
|
type Hub struct {
|
||||||
Players map[uint32]*models.Player
|
Players map[uint32]*models.Player
|
||||||
ServerBuffer []byte
|
Mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHub() *Hub {
|
func NewHub() *Hub {
|
||||||
@@ -20,55 +18,9 @@ func NewHub() *Hub {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hub) readLoop(conn *websocket.Conn) {
|
|
||||||
p := &models.Player{
|
|
||||||
ID: uint32(len(h.Players) + 1),
|
|
||||||
Conn: conn,
|
|
||||||
InputX: 0,
|
|
||||||
InputZ: 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("Player connected:", p.ID)
|
|
||||||
|
|
||||||
h.Players[p.ID] = p
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
delete(h.Players, p.ID)
|
|
||||||
|
|
||||||
p.Conn.Close()
|
|
||||||
log.Println("Player disconnected:", p.ID)
|
|
||||||
}()
|
|
||||||
|
|
||||||
for {
|
|
||||||
_, data, err := p.Conn.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(data) < 8 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
buf := bytes.NewReader(data)
|
|
||||||
log.Println(buf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Hub) ws(w http.ResponseWriter, r *http.Request) {
|
|
||||||
upgrader := websocket.Upgrader{
|
|
||||||
CheckOrigin: func(r *http.Request) bool { return true },
|
|
||||||
}
|
|
||||||
|
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
go h.readLoop(conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Hub) Start() {
|
func (h *Hub) Start() {
|
||||||
|
go h.updateWorld()
|
||||||
http.HandleFunc("/ws", h.ws)
|
http.HandleFunc("/ws", h.ws)
|
||||||
|
log.Println("Server listen port 8080")
|
||||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
}
|
}
|
||||||
|
|||||||
43
controllers/players.go
Normal file
43
controllers/players.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"server/models"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Hub) handShake(msg []byte, conn *websocket.Conn) *models.Player {
|
||||||
|
reader := models.NewReader(msg)
|
||||||
|
newID := reader.ReadU32()
|
||||||
|
name := reader.ReadString()
|
||||||
|
|
||||||
|
player := &models.Player{
|
||||||
|
Entity: models.Entity{
|
||||||
|
Type: models.EntityPlayer,
|
||||||
|
ID: newID,
|
||||||
|
},
|
||||||
|
Name: name,
|
||||||
|
Conn: conn,
|
||||||
|
}
|
||||||
|
|
||||||
|
h.Mu.Lock()
|
||||||
|
h.Players[player.ID] = player
|
||||||
|
h.Mu.Unlock()
|
||||||
|
|
||||||
|
return player
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hub) updatePosition(x, y float32, player *models.Player) {
|
||||||
|
h.Mu.Lock()
|
||||||
|
defer h.Mu.Unlock()
|
||||||
|
|
||||||
|
player.X = x
|
||||||
|
player.Y = y
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hub) removePlayer(id uint32) {
|
||||||
|
h.Mu.Lock()
|
||||||
|
defer h.Mu.Unlock()
|
||||||
|
|
||||||
|
delete(h.Players, id)
|
||||||
|
}
|
||||||
7
controllers/protocol.go
Normal file
7
controllers/protocol.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
const (
|
||||||
|
MSG_WELCOME = 0
|
||||||
|
MSG_INPUT = 1
|
||||||
|
MSG_SNAPSHOT = 2
|
||||||
|
)
|
||||||
46
controllers/world.go
Normal file
46
controllers/world.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"server/models"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Hub) broadcastSnapshot() {
|
||||||
|
h.Mu.RLock()
|
||||||
|
defer h.Mu.RUnlock()
|
||||||
|
|
||||||
|
w := models.Writer{}
|
||||||
|
w.WriteU16(uint16(len(h.Players)))
|
||||||
|
|
||||||
|
for _, p := range h.Players {
|
||||||
|
w.WriteU32(p.ID)
|
||||||
|
w.WriteU8(uint8(models.EntityPlayer))
|
||||||
|
w.WriteF32(p.X)
|
||||||
|
w.WriteF32(p.Y)
|
||||||
|
w.WriteF32(p.Z)
|
||||||
|
w.WriteF32(p.Yaw)
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := models.Message{
|
||||||
|
Type: MSG_SNAPSHOT,
|
||||||
|
Version: 1,
|
||||||
|
Payload: w.Bytes(),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, p := range h.Players {
|
||||||
|
if p.Conn != nil {
|
||||||
|
_ = p.Conn.WriteMessage(websocket.BinaryMessage, msg.Encode())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hub) updateWorld() {
|
||||||
|
ticker := time.NewTicker(50 * time.Millisecond)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for range ticker.C {
|
||||||
|
h.broadcastSnapshot()
|
||||||
|
}
|
||||||
|
}
|
||||||
22
controllers/ws.go
Normal file
22
controllers/ws.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Hub) ws(w http.ResponseWriter, r *http.Request) {
|
||||||
|
upgrader := websocket.Upgrader{
|
||||||
|
CheckOrigin: func(r *http.Request) bool { return true },
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
go h.readLoop(conn)
|
||||||
|
}
|
||||||
2
main.go
2
main.go
@@ -1,12 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"server/controllers"
|
"server/controllers"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
hub := controllers.NewHub()
|
hub := controllers.NewHub()
|
||||||
log.Println("Server listening websocket on :8080")
|
|
||||||
hub.Start()
|
hub.Start()
|
||||||
}
|
}
|
||||||
|
|||||||
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
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
Type int8
|
Type uint16
|
||||||
Payload interface{}
|
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"
|
import "github.com/gorilla/websocket"
|
||||||
|
|
||||||
type Player struct {
|
type Player struct {
|
||||||
ID uint32
|
Entity
|
||||||
|
Name string
|
||||||
X float32
|
|
||||||
Y float32
|
|
||||||
Z float32
|
|
||||||
|
|
||||||
InputX float32
|
|
||||||
InputZ float32
|
|
||||||
|
|
||||||
Name string
|
|
||||||
PhotoURL string
|
|
||||||
|
|
||||||
Conn *websocket.Conn
|
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