From ea9cf5ca872d036ba4f180a958e14e088be9e7aa Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Tue, 25 Jan 2022 15:42:52 -0800 Subject: [PATCH] Make Version Strings Constant --- protocol/connections/peerapp.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/protocol/connections/peerapp.go b/protocol/connections/peerapp.go index b4e3158..bb9b9b0 100644 --- a/protocol/connections/peerapp.go +++ b/protocol/connections/peerapp.go @@ -35,6 +35,9 @@ type peerRetVal struct { Exists bool } +const Version1 = 0x01 +const Version2 = 0x02 + // NewInstance should always return a new instantiation of the application. func (pa *PeerApp) NewInstance() tapir.Application { newApp := new(PeerApp) @@ -74,7 +77,7 @@ func (pa *PeerApp) Init(connection tapir.Connection) { pa.SendMessage(model2.PeerMessage{ ID: event.ContextVersion, Context: event.ContextGetVal, - Data: []byte{0x02}, + Data: []byte{Version1}, }) pa.OnAuth(connection.Hostname()) @@ -98,9 +101,9 @@ func (pa *PeerApp) listen() { var packet model2.PeerMessage var err error - if pa.version.Load() == 0x01 { + if pa.version.Load() == Version1 { err = json.Unmarshal(message, &packet) - } else if pa.version.Load() == 0x02 { + } else if pa.version.Load() == Version2 { parsePacket, parseErr := model2.ParsePeerMessage(message) // if all else fails...attempt to process this message as a version 1 message if parseErr != nil { @@ -120,9 +123,9 @@ func (pa *PeerApp) listen() { // we don't expose im.cwtch.version messages outside of PeerApp (ideally at some point in the future we // can remove this check all together) if packet.ID == event.ContextVersion { - if pa.version.Load() == 0x01 && len(packet.Data) == 1 && packet.Data[0] == 0x02 { - log.Debugf("switching to 2") - pa.version.Store(0x02) + if pa.version.Load() == Version1 && len(packet.Data) == 1 && packet.Data[0] == Version2 { + log.Debugf("switching to protocol version 2") + pa.version.Store(Version2) } } else { pa.MessageHandler(pa.connection.Hostname(), packet.ID, packet.Context, []byte(packet.Data)) @@ -141,7 +144,7 @@ func (pa *PeerApp) SendMessage(message model2.PeerMessage) error { var serialized []byte var err error - if pa.version.Load() == 0x02 { + if pa.version.Load() == Version2 { // treat data as a pre-serialized string, not as a byte array (which will be base64 encoded and bloat the packet size) serialized = message.Serialize() } else {