2024-03-22 18:38:19 +00:00
|
|
|
package common
|
|
|
|
|
2024-03-24 13:09:36 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
)
|
2024-03-22 18:38:19 +00:00
|
|
|
|
2024-03-24 16:10:02 +00:00
|
|
|
// Constants
|
2024-03-24 13:09:36 +00:00
|
|
|
|
|
|
|
const (
|
2024-03-29 14:05:42 +00:00
|
|
|
EchoReqID = 1
|
|
|
|
EchoResID = 128 + EchoReqID
|
|
|
|
ListPeersReqID = 2
|
|
|
|
ListPeersResID = 128 + ListPeersReqID
|
|
|
|
AuthReqID = 3
|
|
|
|
AuthResID = 128 + AuthReqID
|
2024-03-24 13:09:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Requests & responses subtypes
|
2024-03-22 18:38:19 +00:00
|
|
|
|
|
|
|
type PeerInfo struct {
|
2024-03-29 15:53:10 +00:00
|
|
|
ID int `json:"id"`
|
|
|
|
Addr string `json:"addr"`
|
|
|
|
HasNickname bool `json:"hasNickname"`
|
|
|
|
Nickname string `json:"nickname"`
|
2024-03-22 18:38:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-24 13:09:36 +00:00
|
|
|
// Requests & responses:
|
|
|
|
|
2024-03-29 14:05:42 +00:00
|
|
|
type RFrame struct {
|
2024-03-24 13:09:36 +00:00
|
|
|
ID int `json:"id"`
|
2024-03-29 14:05:42 +00:00
|
|
|
Rest json.RawMessage `json:"r"`
|
2024-03-24 13:09:36 +00:00
|
|
|
}
|
|
|
|
|
2024-03-29 15:53:10 +00:00
|
|
|
func (rf RFrame) IsRequest() bool {
|
|
|
|
return rf.ID <= 128
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rf RFrame) IsResponse() bool {
|
|
|
|
return rf.ID > 128
|
|
|
|
}
|
|
|
|
|
2024-03-29 14:05:42 +00:00
|
|
|
func RequestFrameFrom(req Request) (RFrame, error) {
|
2024-03-24 13:09:36 +00:00
|
|
|
jsonBytes, err := json.Marshal(req)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-03-29 14:05:42 +00:00
|
|
|
return *new(RFrame), err
|
2024-03-24 13:09:36 +00:00
|
|
|
}
|
|
|
|
|
2024-03-29 14:05:42 +00:00
|
|
|
return RFrame{req.ID(), jsonBytes}, nil
|
2024-03-24 13:09:36 +00:00
|
|
|
}
|
|
|
|
|
2024-03-29 14:05:42 +00:00
|
|
|
func RequestFromFrame[T Request](reqFrame RFrame) (T, error) {
|
2024-03-24 13:09:36 +00:00
|
|
|
var req T
|
|
|
|
err := json.Unmarshal(reqFrame.Rest, &req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return *new(T), err
|
|
|
|
}
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2024-03-29 14:05:42 +00:00
|
|
|
func ResponseFrameFrom(res Response) (RFrame, error) {
|
2024-03-24 13:09:36 +00:00
|
|
|
jsonBytes, err := json.Marshal(res)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-03-29 14:05:42 +00:00
|
|
|
return *new(RFrame), err
|
2024-03-24 13:09:36 +00:00
|
|
|
}
|
|
|
|
|
2024-03-29 14:05:42 +00:00
|
|
|
return RFrame{res.ID(), jsonBytes}, nil
|
2024-03-24 13:09:36 +00:00
|
|
|
}
|
|
|
|
|
2024-03-29 14:05:42 +00:00
|
|
|
func ResponseFromFrame[T Response](resFrame RFrame) (T, error) {
|
2024-03-24 13:09:36 +00:00
|
|
|
var res T
|
|
|
|
err := json.Unmarshal(resFrame.Rest, &res)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return *new(T), err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Request interface {
|
2024-03-29 14:05:42 +00:00
|
|
|
ID() int
|
2024-03-24 13:09:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Response Request
|
|
|
|
|
2024-03-22 18:38:19 +00:00
|
|
|
type EchoRequest struct {
|
|
|
|
EchoByte byte `json:"echoByte"`
|
|
|
|
}
|
|
|
|
|
2024-03-29 14:05:42 +00:00
|
|
|
func (EchoRequest) ID() int {
|
|
|
|
return EchoReqID
|
2024-03-24 13:09:36 +00:00
|
|
|
}
|
|
|
|
|
2024-03-22 18:38:19 +00:00
|
|
|
type EchoResponse struct {
|
|
|
|
EchoByte byte `json:"echoByte"`
|
|
|
|
}
|
|
|
|
|
2024-03-29 14:05:42 +00:00
|
|
|
func (EchoResponse) ID() int {
|
|
|
|
return EchoResID
|
2024-03-22 18:38:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-24 13:09:36 +00:00
|
|
|
type ListPeersRequest struct {
|
2024-03-22 18:38:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-29 14:05:42 +00:00
|
|
|
func (ListPeersRequest) ID() int {
|
|
|
|
return ListPeersReqID
|
2024-03-22 18:38:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-24 13:09:36 +00:00
|
|
|
type ListPeersResponse struct {
|
|
|
|
PeersInfo []PeerInfo `json:"peers"`
|
2024-03-22 18:38:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-29 14:05:42 +00:00
|
|
|
func (ListPeersResponse) ID() int {
|
|
|
|
return ListPeersResID
|
2024-03-22 18:38:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-24 13:09:36 +00:00
|
|
|
type AuthRequest struct {
|
|
|
|
Nickname string `json:"nickname"`
|
|
|
|
Password string `json:"password"`
|
2024-03-22 18:38:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-29 14:05:42 +00:00
|
|
|
func (AuthRequest) ID() int {
|
|
|
|
return AuthReqID
|
2024-03-22 18:38:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-24 13:09:36 +00:00
|
|
|
type AuthResponse struct {
|
|
|
|
IsSuccess bool
|
2024-03-22 18:38:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-29 14:05:42 +00:00
|
|
|
func (AuthResponse) ID() int {
|
|
|
|
return AuthResID
|
2024-03-22 18:38:19 +00:00
|
|
|
}
|