Skip to content

Commit

Permalink
feat: add listen loop (#3)
Browse files Browse the repository at this point in the history
Signed-off-by: ZhangJian He <shoothzj@gmail.com>
  • Loading branch information
shoothzj authored Sep 8, 2024
1 parent 1cf2ccd commit 8104242
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions opcua/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package opcua
import (
"fmt"
"net"
"sync"
)

type ServerConfig struct {
Expand All @@ -17,11 +18,15 @@ func (s *ServerConfig) addr() string {
type Server struct {
config *ServerConfig
listener net.Listener

mutex sync.RWMutex
quit chan bool
}

func NewServer(config *ServerConfig) *Server {
server := &Server{
config: config,
quit: make(chan bool),
}
return server
}
Expand All @@ -41,12 +46,45 @@ func (s *Server) Run() (int, error) {
s.config.Port = actualAddr.Port
}

s.mutex.Lock()
s.listener = listener
s.mutex.Unlock()

go s.listenLoop()

return actualAddr.Port, nil
}

func (s *Server) listenLoop() {
s.mutex.RLock()
listener := s.listener
s.mutex.RUnlock()
if listener == nil {
return
}
for {
netConn, err := listener.Accept()
if err != nil {
select {
case <-s.quit:
return
default:
continue
}
}
go func() {
s.handleConn(netConn)
}()
}
}

func (s *Server) handleConn(conn net.Conn) {
}

func (s *Server) Close() error {
close(s.quit)
s.mutex.Lock()
defer s.mutex.Unlock()
if s.listener == nil {
return nil
}
Expand Down

0 comments on commit 8104242

Please sign in to comment.