Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Adding custom listener with StartWithListener #162

Merged
merged 2 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v0.7.1] - 2022-12-01

### Features

- Added `StartWithListener` function to start a server with a custom listener ([#155](https://github.com/loopholelabs/frisbee-go/issues/155))

### Fixes

- StreamHandlers would not be registered when calling `ServeConn` because they were created during a `server.Start`
Expand Down
19 changes: 16 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
OnClosedNil = errors.New("OnClosed cannot be nil")
PreWriteNil = errors.New("PreWrite cannot be nil")
StreamHandlerNil = errors.New("StreamHandler cannot be nil")
ListenerNil = errors.New("Listener cannot be nil")
)

var (
Expand Down Expand Up @@ -183,18 +184,30 @@ func (s *Server) SetConcurrency(concurrency uint64) {
// onClosed, OnShutdown, or preWrite functions have not been defined, it will
// use the default functions for these.
func (s *Server) Start(addr string) error {
var listener net.Listener
var err error
if s.options.TLSConfig != nil {
s.listener, err = tls.Listen("tcp", addr, s.options.TLSConfig)
listener, err = tls.Listen("tcp", addr, s.options.TLSConfig)
} else {
s.listener, err = net.Listen("tcp", addr)
listener, err = net.Listen("tcp", addr)
}
if err != nil {
return err
}
return s.StartWithListener(listener)
}

// StartWithListener will start the frisbee server and its reactor goroutines
// to receive and handle incoming connections with a given net.Listener. If the baseContext, ConnContext,
// onClosed, OnShutdown, or preWrite functions have not been defined, it will
// use the default functions for these.
func (s *Server) StartWithListener(listener net.Listener) error {
if listener == nil {
return ListenerNil
}
s.listener = listener
s.wg.Add(1)
close(s.startedCh)

return s.handleListener()
}

Expand Down
Loading