@@ -3,12 +3,14 @@ package protocol
33import (
44 "bytes"
55 "fmt"
6- "github.com/cloudstruct/go-ouroboros-network/muxer"
7- "github.com/cloudstruct/go-ouroboros-network/utils"
8- "github.com/fxamacker/cbor/v2"
96 "io"
107 "reflect"
118 "sync"
9+ "time"
10+
11+ "github.com/cloudstruct/go-ouroboros-network/muxer"
12+ "github.com/cloudstruct/go-ouroboros-network/utils"
13+ "github.com/fxamacker/cbor/v2"
1214)
1315
1416const (
@@ -17,17 +19,18 @@ const (
1719)
1820
1921type Protocol struct {
20- config ProtocolConfig
21- muxerSendChan chan * muxer.Segment
22- muxerRecvChan chan * muxer.Segment
23- state State
24- stateMutex sync.Mutex
25- recvBuffer * bytes.Buffer
26- sendQueueChan chan Message
27- sendStateQueueChan chan Message
28- recvReadyChan chan bool
29- sendReadyChan chan bool
30- doneChan chan bool
22+ config ProtocolConfig
23+ muxerSendChan chan * muxer.Segment
24+ muxerRecvChan chan * muxer.Segment
25+ state State
26+ stateMutex sync.Mutex
27+ recvBuffer * bytes.Buffer
28+ sendQueueChan chan Message
29+ sendStateQueueChan chan Message
30+ recvReadyChan chan bool
31+ sendReadyChan chan bool
32+ doneChan chan bool
33+ stateTransitionTimer * time.Timer
3134}
3235
3336type ProtocolConfig struct {
@@ -316,6 +319,13 @@ func (p *Protocol) getNewState(msg Message) (State, error) {
316319}
317320
318321func (p * Protocol ) setState (state State ) {
322+ // Disable any previous state transition timer
323+ if p .stateTransitionTimer != nil {
324+ if ! p .stateTransitionTimer .Stop () {
325+ <- p .stateTransitionTimer .C
326+ }
327+ p .stateTransitionTimer = nil
328+ }
319329 // Set the new state
320330 p .state = state
321331 // Mark protocol as ready to send/receive based on role and agency of the new state
@@ -335,6 +345,15 @@ func (p *Protocol) setState(state State) {
335345 p .recvReadyChan <- true
336346 }
337347 }
348+ // Set timeout for state transition
349+ if p .config .StateMap [p .state ].Timeout > 0 {
350+ p .stateTransitionTimer = time .AfterFunc (
351+ p .config .StateMap [p .state ].Timeout ,
352+ func () {
353+ p .SendError (fmt .Errorf ("%s: timeout waiting on transition from protocol state %s" , p .config .Name , p .state ))
354+ },
355+ )
356+ }
338357}
339358
340359func (p * Protocol ) handleMessage (msg Message , isResponse bool ) error {
0 commit comments