From 763e9a497c058eac795f60fc454ec7a0236af9b0 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Fri, 7 Jun 2024 21:57:18 +0300 Subject: [PATCH] Changes in communication package --- communication/protocol.go | 63 +++++++++++++++++++++++++++++++++++---- communication/types.go | 5 ++-- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/communication/protocol.go b/communication/protocol.go index d2eceea..a3b2dc0 100644 --- a/communication/protocol.go +++ b/communication/protocol.go @@ -3,6 +3,7 @@ package communication import ( "bytes" "crypto/rand" + "encoding/base64" "encoding/gob" "git.qowevisa.me/Qowevisa/gotell/gmyerr" @@ -126,7 +127,10 @@ func ServerSendClientDecline() ([]byte, error) { return c.Bytes() } -func (r *RegisteredUser) GenerateLink(count uint32) (Link, error) { +func (r *RegisteredUser) GenerateLink(count uint16) (Link, error) { + if count == 0 { + return Link{}, ERROR_LINK_ZERO_COUNT + } var l Link buf := make([]byte, LINK_LEN_V1) @@ -134,11 +138,9 @@ func (r *RegisteredUser) GenerateLink(count uint32) (Link, error) { if err != nil { return Link{}, err } - if count == 0 { - return Link{}, ERROR_LINK_ZERO_COUNT - } + encoded := base64.StdEncoding.EncodeToString(buf) l.Status = LINK_STATUS_CREATED - l.Data = buf + l.Data = []byte(encoded) l.UseCount = count return l, nil @@ -154,6 +156,57 @@ func (l *Link) Bytes() ([]byte, error) { return buf.Bytes(), nil } +func DecodeLink(data []byte) (*Link, error) { + var l Link + buf := bytes.NewBuffer(data) + decoder := gob.NewDecoder(buf) + err := decoder.Decode(&l) + if err != nil { + return nil, err + } + return &l, nil +} + +func ClientSendServerLink(l Link) ([]byte, error) { + bb, err := l.Bytes() + if err != nil { + return nil, err + } + c := Message{ + Version: V1, + ID: ID_CLIENT_SEND_SERVER_LINK, + FromID: 0, + ToID: 0, + DataLen: uint16(len(bb)), + Data: bb, + } + return c.Bytes() +} + +func ServerApproveClientLink() ([]byte, error) { + c := Message{ + Version: V1, + ID: ID_SERVER_APPROVE_CLIENT_LINK, + FromID: 0, + ToID: 0, + DataLen: 0, + Data: []byte{}, + } + return c.Bytes() +} + +func ServerDeclineClientLink() ([]byte, error) { + c := Message{ + Version: V1, + ID: ID_SERVER_DECLINE_CLIENT_LINK, + FromID: 0, + ToID: 0, + DataLen: 0, + Data: []byte{}, + } + return c.Bytes() +} + func (r *RegisteredUser) GetIDFromLink(l Link) ([]byte, error) { bb, err := l.Bytes() if err != nil { diff --git a/communication/types.go b/communication/types.go index feecf05..a9eb858 100644 --- a/communication/types.go +++ b/communication/types.go @@ -6,7 +6,8 @@ type ClientForServer struct { } type RegisteredUser struct { - ID uint16 + IsRegistered bool + ID uint16 } const ( @@ -22,5 +23,5 @@ const ( type Link struct { Status uint8 Data []byte - UseCount uint32 + UseCount uint16 }