219 lines
5.1 KiB
Go
219 lines
5.1 KiB
Go
package protocol
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type PacketType int
|
|
|
|
const (
|
|
ReqUserCertPkt PacketType = iota
|
|
ReqMsgsQueue
|
|
ReqMsgPkt
|
|
SubmitMsgPkt
|
|
SendUserCertPkt
|
|
ServerMsgInfoPkt
|
|
ServerMsgPkt
|
|
)
|
|
|
|
type (
|
|
RequestUserCertPacket struct {
|
|
UID string `json:"uid"`
|
|
}
|
|
|
|
RequestMsgsQueuePacket struct {
|
|
}
|
|
|
|
RequestMsgPacket struct {
|
|
Num int `json:"num"`
|
|
}
|
|
|
|
SubmitMessagePacket struct {
|
|
ToUID string `json:"to_uid"`
|
|
Subject []byte `json:"subject"`
|
|
Body []byte `json:"body"`
|
|
}
|
|
|
|
SendUserCertPacket struct {
|
|
UID string `json:"uid"`
|
|
Certificate []byte `json:"certificate"`
|
|
}
|
|
|
|
ServerMessageInfoPacket struct {
|
|
Num int `json:"num"`
|
|
FromUID string `json:"from_uid"`
|
|
Subject []byte `json:"subject"`
|
|
Timestamp time.Time `json:"timestamp"`
|
|
Last bool `json:"last"`
|
|
}
|
|
ServerMessagePacket struct {
|
|
FromUID string `json:"from_uid"`
|
|
ToUID string `json:"to_uid"`
|
|
Subject []byte `json:"subject"`
|
|
Body []byte `json:"body"`
|
|
Timestamp time.Time `json:"timestamp"`
|
|
}
|
|
)
|
|
|
|
type PacketBody interface{}
|
|
|
|
type Packet struct {
|
|
Flag PacketType `json:"flag"`
|
|
Body PacketBody `json:"body"`
|
|
}
|
|
|
|
func NewRequestUserCertPacket(UID string) Packet {
|
|
return Packet{
|
|
Flag: ReqUserCertPkt,
|
|
Body: RequestUserCertPacket{
|
|
UID: UID,
|
|
},
|
|
}
|
|
}
|
|
|
|
func NewRequestUnreadMsgsQueuePacket() Packet {
|
|
return Packet{
|
|
Flag: ReqMsgsQueue,
|
|
Body: RequestMsgsQueuePacket{},
|
|
}
|
|
}
|
|
|
|
func NewRequestMsgPacket(num int) Packet {
|
|
return Packet{
|
|
Flag: ReqMsgPkt,
|
|
Body: RequestMsgPacket{
|
|
Num: num,
|
|
},
|
|
}
|
|
}
|
|
|
|
func NewSubmitMessagePacket(toUID string, subject []byte, body []byte) Packet {
|
|
return Packet{
|
|
Flag: SubmitMsgPkt,
|
|
Body: SubmitMessagePacket{
|
|
ToUID: toUID,
|
|
Subject: subject,
|
|
Body: body,
|
|
},
|
|
}
|
|
}
|
|
|
|
func NewSendUserCertPacket(uid string, certificate []byte) Packet {
|
|
return Packet{
|
|
Flag: SendUserCertPkt,
|
|
Body: SendUserCertPacket{
|
|
UID: uid,
|
|
Certificate: certificate,
|
|
},
|
|
}
|
|
}
|
|
func NewServerMessageInfoPacket(num int, fromUID string, subject []byte, timestamp time.Time, last bool) Packet {
|
|
return Packet{
|
|
Flag: ServerMsgInfoPkt,
|
|
Body: ServerMessageInfoPacket{
|
|
Num: num,
|
|
FromUID: fromUID,
|
|
Subject: subject,
|
|
Timestamp: timestamp,
|
|
Last: last,
|
|
},
|
|
}
|
|
}
|
|
|
|
func NewServerMessagePacket(fromUID, toUID string, subject []byte, body []byte, timestamp time.Time, last bool) Packet {
|
|
return Packet{
|
|
Flag: ServerMsgPkt,
|
|
Body: ServerMessagePacket{
|
|
FromUID: fromUID,
|
|
ToUID: toUID,
|
|
Subject: subject,
|
|
Body: body,
|
|
Timestamp: timestamp,
|
|
},
|
|
}
|
|
}
|
|
|
|
func UnmarshalRequestUserCertPacket(data PacketBody) RequestUserCertPacket {
|
|
jsonData, err := json.Marshal(data)
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to marshal data: %v", err))
|
|
}
|
|
var packet RequestUserCertPacket
|
|
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
|
panic(fmt.Errorf("failed to unmarshal into RequestUserCertPacket: %v", err))
|
|
}
|
|
return packet
|
|
}
|
|
|
|
func UnmarshalRequestMsgsQueuePacket(data PacketBody) RequestMsgsQueuePacket {
|
|
jsonData, err := json.Marshal(data)
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to marshal data: %v", err))
|
|
}
|
|
var packet RequestMsgsQueuePacket
|
|
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
|
panic(fmt.Errorf("failed to unmarshal into RequestMsgsQueuePacket: %v", err))
|
|
}
|
|
return packet
|
|
}
|
|
|
|
func UnmarshalRequestMsgPacket(data PacketBody) RequestMsgPacket {
|
|
jsonData, err := json.Marshal(data)
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to marshal data: %v", err))
|
|
}
|
|
var packet RequestMsgPacket
|
|
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
|
panic(fmt.Errorf("failed to unmarshal into RequestMsgPacket: %v", err))
|
|
}
|
|
return packet
|
|
}
|
|
|
|
func UnmarshalSubmitMessagePacket(data PacketBody) SubmitMessagePacket {
|
|
jsonData, err := json.Marshal(data)
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to marshal data: %v", err))
|
|
}
|
|
var packet SubmitMessagePacket
|
|
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
|
panic(fmt.Errorf("failed to unmarshal into SubmitMessagePacket: %v", err))
|
|
}
|
|
return packet
|
|
}
|
|
|
|
func UnmarshalSendUserCertPacket(data PacketBody) SendUserCertPacket {
|
|
jsonData, err := json.Marshal(data)
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to marshal data: %v", err))
|
|
}
|
|
var packet SendUserCertPacket
|
|
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
|
panic(fmt.Errorf("failed to unmarshal into SendUserCertPacket: %v", err))
|
|
}
|
|
return packet
|
|
}
|
|
func UnmarshalServerMessageInfoPacket(data PacketBody) ServerMessageInfoPacket {
|
|
jsonData, err := json.Marshal(data)
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to marshal data: %v", err))
|
|
}
|
|
var packet ServerMessageInfoPacket
|
|
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
|
panic(fmt.Errorf("failed to unmarshal into ServerMessageInfoPacket: %v", err))
|
|
}
|
|
return packet
|
|
}
|
|
|
|
func UnmarshalServerMessagePacket(data PacketBody) ServerMessagePacket {
|
|
jsonData, err := json.Marshal(data)
|
|
if err != nil {
|
|
panic(fmt.Errorf("failed to marshal data: %v", err))
|
|
}
|
|
var packet ServerMessagePacket
|
|
if err := json.Unmarshal(jsonData, &packet); err != nil {
|
|
panic(fmt.Errorf("failed to unmarshal into ServerMessagePacket: %v", err))
|
|
}
|
|
return packet
|
|
}
|