CSI-ES-2324/Projs/PD1/internal/protocol/protocol.go
2024-04-16 12:23:00 +01:00

68 lines
1.1 KiB
Go

package protocol
import (
"encoding/json"
"fmt"
"time"
)
type PacketType int
const (
ReqPK PacketType = iota
ReqAllMsg
ReqMsg
SendPK
UsrMsg
)
type PacketBody interface {}
type Packet struct {
Flag PacketType
Body PacketBody
}
// Client --> Server: Ask for a user's public key
type RequestPubKey struct {
FromUID string
KeyUID string
}
// Client --> Server: Ask for all the client's messages in the queue
type RequestAllMsg struct {
FromUID string
}
// Client --> Server: Ask for a specific message in the queue
type RequestMsg struct {
Num uint16
}
// Server --> Client: Send the client the requested public key
type SendPubKey struct {
Key []byte
}
// Bidirectional: Send messages between server and clients
type SendMessage struct {
ToUID string
Subject []byte
Body []byte
}
type Message struct {
FromUID string
ToUID string
Subject []byte
Body []byte
Timestamp time.Time
}
func (p Packet) Marshal() ([]byte, error) {
return json.Marshal(p)
}
func (p *Packet) Unmarshal(data []byte) error {
return json.Unmarshal(data, p)
}