Skip to content

Commit 824ed3d

Browse files
authored
Merge pull request #42 from cloudstruct/feature/message-is-response
Pass along whether message is a response to the handler
2 parents 0d5faa2 + 57fec52 commit 824ed3d

File tree

6 files changed

+12
-9
lines changed

6 files changed

+12
-9
lines changed

protocol/blockfetch/blockfetch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func New(options protocol.ProtocolOptions, callbackConfig *BlockFetchCallbackCon
102102
return b
103103
}
104104

105-
func (b *BlockFetch) messageHandler(msg protocol.Message) error {
105+
func (b *BlockFetch) messageHandler(msg protocol.Message, isResponse bool) error {
106106
var err error
107107
switch msg.Type() {
108108
case MESSAGE_TYPE_START_BATCH:

protocol/chainsync/chainsync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func New(options protocol.ProtocolOptions, callbackConfig *ChainSyncCallbackConf
137137
return c
138138
}
139139

140-
func (c *ChainSync) messageHandler(msg protocol.Message) error {
140+
func (c *ChainSync) messageHandler(msg protocol.Message, isResponse bool) error {
141141
var err error
142142
switch msg.Type() {
143143
case MESSAGE_TYPE_AWAIT_REPLY:

protocol/handshake/handshake.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func New(options protocol.ProtocolOptions, allowedVersions []uint16) *Handshake
7575
return h
7676
}
7777

78-
func (h *Handshake) handleMessage(msg protocol.Message) error {
78+
func (h *Handshake) handleMessage(msg protocol.Message, isResponse bool) error {
7979
var err error
8080
switch msg.Type() {
8181
case MESSAGE_TYPE_PROPOSE_VERSIONS:

protocol/keepalive/keepalive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func New(options protocol.ProtocolOptions, callbackConfig *KeepAliveCallbackConf
8585
return k
8686
}
8787

88-
func (k *KeepAlive) messageHandler(msg protocol.Message) error {
88+
func (k *KeepAlive) messageHandler(msg protocol.Message, isResponse bool) error {
8989
var err error
9090
switch msg.Type() {
9191
case MESSAGE_TYPE_KEEP_ALIVE:

protocol/localtxsubmission/localtxsubmission.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func New(options protocol.ProtocolOptions, callbackConfig *CallbackConfig) *Loca
8282
return l
8383
}
8484

85-
func (l *LocalTxSubmission) messageHandler(msg protocol.Message) error {
85+
func (l *LocalTxSubmission) messageHandler(msg protocol.Message, isResponse bool) error {
8686
var err error
8787
switch msg.Type() {
8888
case MESSAGE_TYPE_SUBMIT_TX:

protocol/protocol.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type ProtocolOptions struct {
5454
Role ProtocolRole
5555
}
5656

57-
type MessageHandlerFunc func(Message) error
57+
type MessageHandlerFunc func(Message, bool) error
5858
type MessageFromCborFunc func(uint, []byte) (Message, error)
5959

6060
func New(config ProtocolConfig) *Protocol {
@@ -108,6 +108,7 @@ func (p *Protocol) SendError(err error) {
108108

109109
func (p *Protocol) recvLoop() {
110110
leftoverData := false
111+
isResponse := false
111112
for {
112113
var err error
113114
// Don't grab the next segment from the muxer if we still have data in the buffer
@@ -116,6 +117,8 @@ func (p *Protocol) recvLoop() {
116117
segment := <-p.recvChan
117118
// Add segment payload to buffer
118119
p.recvBuffer.Write(segment.Payload)
120+
// Save whether it's a response
121+
isResponse = segment.IsResponse()
119122
}
120123
leftoverData = false
121124
// Decode message into generic list until we can determine what type of message it is
@@ -140,7 +143,7 @@ func (p *Protocol) recvLoop() {
140143
p.config.ErrorChan <- fmt.Errorf("%s: received unknown message type: %#v", p.config.Name, tmpMsg)
141144
}
142145
// Handle message
143-
if err := p.handleMessage(msg); err != nil {
146+
if err := p.handleMessage(msg, isResponse); err != nil {
144147
p.config.ErrorChan <- err
145148
}
146149
if numBytesRead < p.recvBuffer.Len() {
@@ -183,7 +186,7 @@ func (p *Protocol) getNewState(msg Message) (State, error) {
183186
return newState, nil
184187
}
185188

186-
func (p *Protocol) handleMessage(msg Message) error {
189+
func (p *Protocol) handleMessage(msg Message, isResponse bool) error {
187190
// Lock the state to prevent collisions
188191
p.stateMutex.Lock()
189192
if err := p.checkCurrentState(); err != nil {
@@ -197,5 +200,5 @@ func (p *Protocol) handleMessage(msg Message) error {
197200
p.state = newState
198201
p.stateMutex.Unlock()
199202
// Call handler function
200-
return p.config.MessageHandlerFunc(msg)
203+
return p.config.MessageHandlerFunc(msg, isResponse)
201204
}

0 commit comments

Comments
 (0)