116 lines
1.8 KiB
Go
116 lines
1.8 KiB
Go
package protocol
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
type PacketType int
|
|
|
|
const (
|
|
ReqPK PacketType = iota
|
|
ReqAllMsg
|
|
ReqMsg
|
|
SubmitMsg
|
|
SendPK
|
|
Msg
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
func NewRequestPubKey(fromUID, keyUID string) Packet {
|
|
return Packet{
|
|
Flag: ReqPK,
|
|
Body: RequestPubKey{
|
|
FromUID: fromUID,
|
|
KeyUID: keyUID,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Client --> Server: Ask for all the client's messages in the queue
|
|
type RequestAllMsg struct {
|
|
FromUID string
|
|
}
|
|
|
|
func NewRequestAllMsg(fromUID string) Packet {
|
|
return Packet{
|
|
Flag: ReqAllMsg,
|
|
Body: RequestAllMsg{
|
|
FromUID: fromUID,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Client --> Server: Ask for a specific message in the queue
|
|
type RequestMsg struct {
|
|
Num uint16
|
|
}
|
|
|
|
func NewRequestMsg(num uint16) Packet {
|
|
return Packet{
|
|
Flag: ReqMsg,
|
|
Body: RequestMsg{
|
|
Num: num,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Client --> Server: Send message from client to server
|
|
type SubmitMessage struct {
|
|
ToUID string
|
|
Content []byte
|
|
}
|
|
|
|
func NewSubmitMessage(toUID string, content []byte) Packet {
|
|
return Packet{
|
|
Flag: SubmitMsg,
|
|
Body: SubmitMessage{
|
|
ToUID: toUID,
|
|
Content: content},
|
|
}
|
|
}
|
|
|
|
// Server --> Client: Send the client the requested public key
|
|
type SendPubKey struct {
|
|
Key []byte
|
|
}
|
|
|
|
func NewSendPubKey(key []byte) Packet {
|
|
return Packet{
|
|
Flag: SendPK,
|
|
Body: SendPubKey{
|
|
Key: key,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Server --> Client: Send the client a message
|
|
type ServerMessage struct {
|
|
FromUID string
|
|
ToUID string
|
|
Content []byte
|
|
Timestamp time.Time
|
|
}
|
|
|
|
func NewMessage(fromUID, toUID string, content []byte, timestamp time.Time) Packet {
|
|
return Packet{
|
|
Flag: Msg,
|
|
Body: ServerMessage{
|
|
FromUID: fromUID,
|
|
ToUID: toUID,
|
|
Content: content,
|
|
Timestamp: timestamp,
|
|
},
|
|
}
|
|
}
|