Compare commits
2 Commits
e31a48c4cd
...
main
Author | SHA1 | Date | |
---|---|---|---|
ed52397ea9 | |||
5ab67999d3 |
@ -13,6 +13,16 @@ enum ServerPacketType {
|
|||||||
LEAVE = 6;
|
LEAVE = 6;
|
||||||
LIST = 7;
|
LIST = 7;
|
||||||
TOTAL = 8;
|
TOTAL = 8;
|
||||||
|
STATUS = 9;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ServerStatus {
|
||||||
|
FREE = 0;
|
||||||
|
WAITING = 1;
|
||||||
|
STARTING = 2;
|
||||||
|
PLAYING = 3;
|
||||||
|
ENDING = 4;
|
||||||
|
FULL = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
message ServerPacket {
|
message ServerPacket {
|
||||||
@ -49,4 +59,8 @@ message ServerListPacket {
|
|||||||
|
|
||||||
message ServerTotalPacket {
|
message ServerTotalPacket {
|
||||||
int32 total = 1;
|
int32 total = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message ServerStatusPacket {
|
||||||
|
ServerStatus status = 1;
|
||||||
}
|
}
|
@ -22,6 +22,7 @@ const (
|
|||||||
ServerTypeRoom ServerType = "ROOM"
|
ServerTypeRoom ServerType = "ROOM"
|
||||||
ServerTypeLimbo ServerType = "LIMBO"
|
ServerTypeLimbo ServerType = "LIMBO"
|
||||||
ServerTypeSystem ServerType = "SYSTEM"
|
ServerTypeSystem ServerType = "SYSTEM"
|
||||||
|
ServerTypeMega ServerType = "MAGA" // make america great again
|
||||||
)
|
)
|
||||||
|
|
||||||
func (typ ServerType) Prefix() string {
|
func (typ ServerType) Prefix() string {
|
||||||
@ -34,6 +35,8 @@ func (typ ServerType) Prefix() string {
|
|||||||
return ""
|
return ""
|
||||||
case ServerTypeSystem:
|
case ServerTypeSystem:
|
||||||
return "S"
|
return "S"
|
||||||
|
case ServerTypeMega:
|
||||||
|
return "mega"
|
||||||
case ServerTypeUnknown:
|
case ServerTypeUnknown:
|
||||||
default:
|
default:
|
||||||
return "U"
|
return "U"
|
||||||
@ -41,6 +44,17 @@ func (typ ServerType) Prefix() string {
|
|||||||
return "U"
|
return "U"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Status string
|
||||||
|
|
||||||
|
const (
|
||||||
|
ServerStatusFree Status = "FREE"
|
||||||
|
ServerStatusWaiting Status = "WAITING"
|
||||||
|
ServerStatusStarting Status = "STARTING"
|
||||||
|
ServerStatusFull Status = "FULL"
|
||||||
|
ServerStatusPlaying Status = "PLAYING"
|
||||||
|
ServerStatusEnding Status = "ENDING"
|
||||||
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@ -50,6 +64,7 @@ type Server struct {
|
|||||||
Type ServerType `json:"type"`
|
Type ServerType `json:"type"`
|
||||||
Motd string `json:"motd"`
|
Motd string `json:"motd"`
|
||||||
Players []string `json:"players"`
|
Players []string `json:"players"`
|
||||||
|
Status Status `json:"status"`
|
||||||
LastHB time.Time
|
LastHB time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,12 +165,14 @@ func ServerRegister(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
server := &Server{
|
server := &Server{
|
||||||
Id: generateServerId(data.Type.Prefix()),
|
Id: generateServerId(data.Type.Prefix()),
|
||||||
Host: data.Host,
|
Host: data.Host,
|
||||||
Port: data.Port,
|
Port: data.Port,
|
||||||
Type: data.Type,
|
Motd: data.Motd,
|
||||||
Group: data.Group,
|
Type: data.Type,
|
||||||
LastHB: time.Now(),
|
Group: data.Group,
|
||||||
|
Players: make([]string, 0),
|
||||||
|
LastHB: time.Now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if data.Name == "AUTO" {
|
if data.Name == "AUTO" {
|
||||||
@ -336,6 +353,32 @@ func serverReadLoop(conn *ws.Conn, serverId string) {
|
|||||||
conn.WriteChan <- ws.Binary(buf)
|
conn.WriteChan <- ws.Binary(buf)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
case ServerPacketType_STATUS:
|
||||||
|
var payload ServerStatusPacket
|
||||||
|
err := proto.Unmarshal(pkt.GetPayload(), &payload)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
switch payload.Status {
|
||||||
|
case ServerStatus_FREE:
|
||||||
|
server.Status = ServerStatusFull
|
||||||
|
break
|
||||||
|
case ServerStatus_WAITING:
|
||||||
|
server.Status = ServerStatusWaiting
|
||||||
|
break
|
||||||
|
case ServerStatus_STARTING:
|
||||||
|
server.Status = ServerStatusStarting
|
||||||
|
break
|
||||||
|
case ServerStatus_PLAYING:
|
||||||
|
server.Status = ServerStatusPlaying
|
||||||
|
break
|
||||||
|
case ServerStatus_ENDING:
|
||||||
|
server.Status = ServerStatusEnding
|
||||||
|
break
|
||||||
|
case ServerStatus_FULL:
|
||||||
|
server.Status = ServerStatusFull
|
||||||
|
break
|
||||||
|
}
|
||||||
case ServerPacketType_TOTAL:
|
case ServerPacketType_TOTAL:
|
||||||
total := 0
|
total := 0
|
||||||
servers.Range(func(_, value any) bool {
|
servers.Range(func(_, value any) bool {
|
||||||
@ -358,10 +401,8 @@ func serverReadLoop(conn *ws.Conn, serverId string) {
|
|||||||
}
|
}
|
||||||
conn.WriteChan <- ws.Binary(buf)
|
conn.WriteChan <- ws.Binary(buf)
|
||||||
}
|
}
|
||||||
default:
|
case <-conn.Context.Done():
|
||||||
if conn.IsClosed() {
|
return
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package ws
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Packet struct {
|
type Packet struct {
|
||||||
@ -27,16 +28,21 @@ func Binary(buf []byte) *Packet {
|
|||||||
type Conn struct {
|
type Conn struct {
|
||||||
WriteChan chan *Packet
|
WriteChan chan *Packet
|
||||||
ReadChan chan []byte
|
ReadChan chan []byte
|
||||||
ctx context.Context
|
Context context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
close bool
|
close bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func Wrap(conn *websocket.Conn) *Conn {
|
func Wrap(conn *websocket.Conn) *Conn {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
return &Conn{
|
return &Conn{
|
||||||
WriteChan: make(chan *Packet),
|
WriteChan: make(chan *Packet),
|
||||||
ReadChan: make(chan []byte),
|
ReadChan: make(chan []byte),
|
||||||
conn: conn,
|
conn: conn,
|
||||||
|
Context: ctx,
|
||||||
|
cancel: cancel,
|
||||||
|
close: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,6 +50,9 @@ func (conn *Conn) ReadLoop() {
|
|||||||
for !conn.close {
|
for !conn.close {
|
||||||
_, message, err := conn.conn.ReadMessage()
|
_, message, err := conn.conn.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if !websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseAbnormalClosure) {
|
||||||
|
log.Println("websocket read error:", err)
|
||||||
|
}
|
||||||
conn.Close()
|
conn.Close()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -63,16 +72,14 @@ func (conn *Conn) WriteLoop() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
default:
|
case <-conn.Context.Done():
|
||||||
if conn.close {
|
return
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Conn) Close() error {
|
func (conn *Conn) Close() error {
|
||||||
conn.ReadChan <- nil
|
conn.cancel()
|
||||||
conn.close = true
|
conn.close = true
|
||||||
return conn.conn.Close()
|
return conn.conn.Close()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user