[PD1] Fixed almost everything
This commit is contained in:
parent
39a0e5c01f
commit
7b3172a850
13 changed files with 534 additions and 192 deletions
|
@ -1,114 +1,219 @@
|
|||
package protocol
|
||||
|
||||
import (
|
||||
"time"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PacketType int
|
||||
|
||||
const (
|
||||
ReqUserCertPkt PacketType = iota
|
||||
ReqAllMsgPkt
|
||||
ReqMsgPkt
|
||||
SubmitMsgPkt
|
||||
SendUserCertPkt
|
||||
ServerMsgPkt
|
||||
ReqUserCertPkt PacketType = iota
|
||||
ReqMsgsQueue
|
||||
ReqMsgPkt
|
||||
SubmitMsgPkt
|
||||
SendUserCertPkt
|
||||
ServerMsgInfoPkt
|
||||
ServerMsgPkt
|
||||
)
|
||||
|
||||
// Define interfaces for packet bodies
|
||||
type (
|
||||
RequestUserCertPacket struct {
|
||||
UID string `json:"uid"`
|
||||
}
|
||||
RequestUserCertPacket struct {
|
||||
UID string `json:"uid"`
|
||||
}
|
||||
|
||||
RequestAllMsgPacket struct {
|
||||
FromUID string `json:"from_uid"`
|
||||
}
|
||||
RequestMsgsQueuePacket struct {
|
||||
}
|
||||
|
||||
RequestMsgPacket struct {
|
||||
Num uint16 `json:"num"`
|
||||
}
|
||||
RequestMsgPacket struct {
|
||||
Num int `json:"num"`
|
||||
}
|
||||
|
||||
SubmitMessagePacket struct {
|
||||
ToUID string `json:"to_uid"`
|
||||
Content []byte `json:"content"`
|
||||
}
|
||||
SubmitMessagePacket struct {
|
||||
ToUID string `json:"to_uid"`
|
||||
Subject []byte `json:"subject"`
|
||||
Body []byte `json:"body"`
|
||||
}
|
||||
|
||||
SendUserCertPacket struct {
|
||||
UID string `json:"uid"`
|
||||
Key []byte `json:"key"`
|
||||
}
|
||||
SendUserCertPacket struct {
|
||||
UID string `json:"uid"`
|
||||
Certificate []byte `json:"certificate"`
|
||||
}
|
||||
|
||||
ServerMessagePacket struct {
|
||||
FromUID string `json:"from_uid"`
|
||||
ToUID string `json:"to_uid"`
|
||||
Content []byte `json:"content"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
}
|
||||
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"`
|
||||
Flag PacketType `json:"flag"`
|
||||
Body PacketBody `json:"body"`
|
||||
}
|
||||
|
||||
func NewRequestUserCertPacket(UID string) Packet {
|
||||
return Packet{
|
||||
Flag: ReqUserCertPkt,
|
||||
Body: RequestUserCertPacket{
|
||||
UID: UID,
|
||||
},
|
||||
}
|
||||
return Packet{
|
||||
Flag: ReqUserCertPkt,
|
||||
Body: RequestUserCertPacket{
|
||||
UID: UID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func NewRequestAllMsgPacket(fromUID string) Packet {
|
||||
return Packet{
|
||||
Flag: ReqAllMsgPkt,
|
||||
Body: RequestAllMsgPacket{
|
||||
FromUID: fromUID,
|
||||
},
|
||||
}
|
||||
func NewRequestUnreadMsgsQueuePacket() Packet {
|
||||
return Packet{
|
||||
Flag: ReqMsgsQueue,
|
||||
Body: RequestMsgsQueuePacket{},
|
||||
}
|
||||
}
|
||||
|
||||
func NewRequestMsgPacket(num uint16) Packet {
|
||||
return Packet{
|
||||
Flag: ReqMsgPkt,
|
||||
Body: RequestMsgPacket{
|
||||
Num: num,
|
||||
},
|
||||
}
|
||||
func NewRequestMsgPacket(num int) Packet {
|
||||
return Packet{
|
||||
Flag: ReqMsgPkt,
|
||||
Body: RequestMsgPacket{
|
||||
Num: num,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func NewSubmitMessagePacket(toUID string, content []byte) Packet {
|
||||
return Packet{
|
||||
Flag: SubmitMsgPkt,
|
||||
Body: SubmitMessagePacket{
|
||||
ToUID: toUID,
|
||||
Content: content,
|
||||
},
|
||||
}
|
||||
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, key []byte) Packet {
|
||||
return Packet{
|
||||
Flag: SendUserCertPkt,
|
||||
Body: SendUserCertPacket{
|
||||
UID: uid,
|
||||
Key: key,
|
||||
},
|
||||
}
|
||||
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, content []byte, timestamp time.Time) Packet {
|
||||
return Packet{
|
||||
Flag: ServerMsgPkt,
|
||||
Body: ServerMessagePacket{
|
||||
FromUID: fromUID,
|
||||
ToUID: toUID,
|
||||
Content: content,
|
||||
Timestamp: timestamp,
|
||||
},
|
||||
}
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue