websocket-based lrcproto server

add initialID option

+23 -13
+13 -5
options.go
··· 9 ) 10 11 type options struct { 12 - welcome *string 13 - writer *io.Writer 14 - verbose bool 15 - pubChan chan PubEvent 16 - initChan chan lrcpb.Event_Init 17 } 18 19 type Option func(option *options) error ··· 24 return errors.New("welcome must be at most 50 runes") 25 } 26 options.welcome = &welcome 27 return nil 28 } 29 }
··· 9 ) 10 11 type options struct { 12 + welcome *string 13 + writer *io.Writer 14 + verbose bool 15 + pubChan chan PubEvent 16 + initChan chan lrcpb.Event_Init 17 + initialID *uint32 18 } 19 20 type Option func(option *options) error ··· 25 return errors.New("welcome must be at most 50 runes") 26 } 27 options.welcome = &welcome 28 + return nil 29 + } 30 + } 31 + 32 + func WithInitialID(id uint32) Option { 33 + return func(options *options) error { 34 + options.initialID = &id 35 return nil 36 } 37 }
+10 -8
server.go
··· 6 "github.com/gorilla/websocket" 7 "github.com/rachel-mp4/lrcproto/gen/go" 8 "google.golang.org/protobuf/proto" 9 - "unicode/utf16" 10 "log" 11 "net/http" 12 "sync" 13 ) 14 15 type Server struct { ··· 84 if options.pubChan != nil { 85 s.pubChan = options.pubChan 86 } 87 88 s.clients = make(map[*client]bool) 89 s.clientsMu = sync.Mutex{} 90 s.idmapsMu = sync.Mutex{} 91 s.clientToID = make(map[*client]*uint32) 92 s.idToClient = make(map[uint32]*client) 93 - s.lastID = 0 94 s.eventBus = make(chan clientEvent, 100) 95 return &s, nil 96 } ··· 117 } 118 119 // Stop stops a server if it has started, and returns an error if it is already stopped 120 - func (s *Server) Stop() error { 121 if s.ctx == nil { 122 - return nil 123 } 124 select { 125 case <-s.ctx.Done(): 126 - return errors.New("cannot stop already stopped server") 127 default: 128 s.cancel() 129 s.logDebug("Goodbye world :c") 130 - return nil 131 } 132 } 133 ··· 404 405 insertRunes := []rune(insert) 406 insertUTF16Units := utf16.Encode(insertRunes) 407 - result := make([]uint16, 0, len(baseUTF16Units) + len(insertUTF16Units)) 408 result = append(result, baseUTF16Units[:index]...) 409 result = append(result, insertUTF16Units...) 410 result = append(result, baseUTF16Units[index:]...) ··· 436 if uint32(len(baseUTF16Units)) < end { 437 return "", errors.New("index out of range") 438 } 439 - result := make([]uint16, 0, uint32(len(baseUTF16Units)) + start - end) 440 result = append(result, baseUTF16Units[:start]...) 441 result = append(result, baseUTF16Units[end:]...) 442 resultRunes := utf16.Decode(result)
··· 6 "github.com/gorilla/websocket" 7 "github.com/rachel-mp4/lrcproto/gen/go" 8 "google.golang.org/protobuf/proto" 9 "log" 10 "net/http" 11 "sync" 12 + "unicode/utf16" 13 ) 14 15 type Server struct { ··· 84 if options.pubChan != nil { 85 s.pubChan = options.pubChan 86 } 87 + if options.initialID != nil { 88 + s.lastID = *options.initialID 89 + } 90 91 s.clients = make(map[*client]bool) 92 s.clientsMu = sync.Mutex{} 93 s.idmapsMu = sync.Mutex{} 94 s.clientToID = make(map[*client]*uint32) 95 s.idToClient = make(map[uint32]*client) 96 s.eventBus = make(chan clientEvent, 100) 97 return &s, nil 98 } ··· 119 } 120 121 // Stop stops a server if it has started, and returns an error if it is already stopped 122 + func (s *Server) Stop() (uint32, error) { 123 if s.ctx == nil { 124 + return s.lastID, nil 125 } 126 select { 127 case <-s.ctx.Done(): 128 + return s.lastID, errors.New("cannot stop already stopped server") 129 default: 130 s.cancel() 131 s.logDebug("Goodbye world :c") 132 + return s.lastID, nil 133 } 134 } 135 ··· 406 407 insertRunes := []rune(insert) 408 insertUTF16Units := utf16.Encode(insertRunes) 409 + result := make([]uint16, 0, len(baseUTF16Units)+len(insertUTF16Units)) 410 result = append(result, baseUTF16Units[:index]...) 411 result = append(result, insertUTF16Units...) 412 result = append(result, baseUTF16Units[index:]...) ··· 438 if uint32(len(baseUTF16Units)) < end { 439 return "", errors.New("index out of range") 440 } 441 + result := make([]uint16, 0, uint32(len(baseUTF16Units))+start-end) 442 result = append(result, baseUTF16Units[:start]...) 443 result = append(result, baseUTF16Units[end:]...) 444 resultRunes := utf16.Decode(result)