mirror of
https://github.com/tiennm99/gomoku.git
synced 2026-05-14 04:58:38 +00:00
9e14f3a9b3
- Add server/network/{server,codec,reader,writer,dispatch,handlers_stateless}.go
- Single /gomoku WS endpoint; binary protobuf frames only (no JSON/base64)
- Per-connection reader + writer goroutines; write mutex serialises WS writes
- Dispatch type-switch: stateless (heartbeat, set_nickname, get_rooms,
set_client_info, client_exit) inline; stateful requests → player.CmdCh
- Add Player.SendCh, CmdCh, LastHeartbeat, ClientVersion, Send() to database.Player
- Rewire main.go: NewServer(":1999").Serve() + database.StartCleanup()
- Empty wss.go (old shim superseded by server.go)
29 lines
729 B
Go
29 lines
729 B
Go
package network
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"github.com/tiennm99/gomoku/server/protocol"
|
|
)
|
|
|
|
// DecodeRequest deserializes a binary WS frame into a typed Request.
|
|
// Returns a descriptive error on malformed input.
|
|
func DecodeRequest(data []byte) (*protocol.Request, error) {
|
|
req := &protocol.Request{}
|
|
if err := proto.Unmarshal(data, req); err != nil {
|
|
return nil, fmt.Errorf("decode request: %w", err)
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
// EncodeResponse serializes a Response to binary for transmission as a WS frame.
|
|
func EncodeResponse(resp *protocol.Response) ([]byte, error) {
|
|
data, err := proto.Marshal(resp)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("encode response: %w", err)
|
|
}
|
|
return data, nil
|
|
}
|