Make Version Strings Constant
continuous-integration/drone/push Build is pending Details
continuous-integration/drone/pr Build is pending Details

This commit is contained in:
Sarah Jamie Lewis 2022-01-25 15:42:52 -08:00
parent ff4249e2bc
commit ea9cf5ca87
1 changed files with 10 additions and 7 deletions

View File

@ -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 {