~fnux/yggdrasil-go-coap

482247b399702f38d5db48075c5661e4c90dff12 — Jozef Kralik 2 years ago 953b051
fix interface of NotifySessionNewFunc, NotifySessionEndFunc
4 files changed, 21 insertions(+), 16 deletions(-)

M client.go
M networksession.go
M server.go
M server_test.go
M client.go => client.go +1 -1
@@ 146,7 146,7 @@ func (c *Client) DialWithContext(ctx context.Context, address string) (clientCon
			BlockWiseTransfer:        &BlockWiseTransfer,
			BlockWiseTransferSzx:     &BlockWiseTransferSzx,
			DisableTCPSignalMessages: c.DisableTCPSignalMessages,
			NotifySessionEndFunc: func(s *ClientCommander, err error) {
			NotifySessionEndFunc: func(s *ClientConn, err error) {
				if c.NotifySessionEndFunc != nil {
					c.NotifySessionEndFunc(err)
				}

M networksession.go => networksession.go +4 -2
@@ 221,7 221,8 @@ func (s *sessionUDP) closeWithError(err error) error {
	s.srv.sessionUDPMapLock.Lock()
	delete(s.srv.sessionUDPMap, s.sessionUDPData.Key())
	s.srv.sessionUDPMapLock.Unlock()
	s.srv.NotifySessionEndFunc(&ClientCommander{s}, err)
	c := ClientConn{commander: &ClientCommander{s}}
	s.srv.NotifySessionEndFunc(&c, err)

	return err
}


@@ 278,7 279,8 @@ func (s *sessionUDP) Close() error {

func (s *sessionTCP) closeWithError(err error) error {
	if s.connection != nil {
		s.srv.NotifySessionEndFunc(&ClientCommander{s}, err)
		c := ClientConn{commander: &ClientCommander{s}}
		s.srv.NotifySessionEndFunc(&c, err)
		e := s.connection.Close()
		//s.connection = nil
		if e == nil {

M server.go => server.go +10 -7
@@ 146,9 146,9 @@ type Server struct {
	// If newSessionUDPFunc is set it is called when session TCP want to be created
	newSessionTCPFunc func(connection *kitNet.Conn, srv *Server) (networkSession, error)
	// If NotifyNewSession is set it is called when new TCP/UDP session was created.
	NotifySessionNewFunc func(w *ClientCommander)
	NotifySessionNewFunc func(w *ClientConn)
	// If NotifyNewSession is set it is called when TCP/UDP session was ended.
	NotifySessionEndFunc func(w *ClientCommander, err error)
	NotifySessionEndFunc func(w *ClientConn, err error)
	// The interfaces that will be used for udp-mcast (default uses the system assigned for multicast)
	UDPMcastInterfaces []net.Interface
	// Use blockWise transfer for transfer payload (default for UDP it's enabled, for TCP it's disable)


@@ 359,11 359,11 @@ func (srv *Server) ActivateAndServe() error {
	}

	if srv.NotifySessionNewFunc == nil {
		srv.NotifySessionNewFunc = func(w *ClientCommander) {}
		srv.NotifySessionNewFunc = func(w *ClientConn) {}
	}

	if srv.NotifySessionEndFunc == nil {
		srv.NotifySessionEndFunc = func(w *ClientCommander, err error) {}
		srv.NotifySessionEndFunc = func(w *ClientConn, err error) {}
	}

	if pConn != nil {


@@ 426,7 426,8 @@ func (srv *Server) serveTCPconnection(ctx *shutdownContext, netConn net.Conn) er
	if err != nil {
		return err
	}
	srv.NotifySessionNewFunc(&ClientCommander{session})
	c := ClientConn{commander: &ClientCommander{session}}
	srv.NotifySessionNewFunc(&c)

	sessCtx, cancel := context.WithCancel(context.Background())
	defer cancel()


@@ 493,7 494,8 @@ func (srv *Server) closeSessions(err error) {
	srv.sessionUDPMap = make(map[string]networkSession)
	srv.sessionUDPMapLock.Unlock()
	for _, v := range tmp {
		srv.NotifySessionEndFunc(&ClientCommander{v}, err)
		c := ClientConn{commander: &ClientCommander{v}}
		srv.NotifySessionEndFunc(&c, err)
	}
}



@@ 526,7 528,8 @@ func (srv *Server) serveUDP(ctx *shutdownContext, conn *net.UDPConn) error {
			if err != nil {
				return err
			}
			srv.NotifySessionNewFunc(&ClientCommander{session})
			c := ClientConn{commander: &ClientCommander{session}}
			srv.NotifySessionNewFunc(&c)
			srv.sessionUDPMap[s.Key()] = session
			srv.sessionUDPMapLock.Unlock()
		} else {

M server_test.go => server_test.go +6 -6
@@ 78,10 78,10 @@ func RunLocalServerUDPWithHandler(lnet, laddr string, BlockWiseTransfer bool, Bl
		return nil, "", nil, err
	}
	server := &Server{Conn: pc, ReadTimeout: time.Hour, WriteTimeout: time.Hour,
		NotifySessionNewFunc: func(s *ClientCommander) {
		NotifySessionNewFunc: func(s *ClientConn) {
			fmt.Printf("networkSession start %v\n", s.RemoteAddr())
		},
		NotifySessionEndFunc: func(w *ClientCommander, err error) {
		NotifySessionEndFunc: func(w *ClientConn, err error) {
			fmt.Printf("networkSession end %v: %v\n", w.RemoteAddr(), err)
		},
		Handler:              handler,


@@ 119,9 119,9 @@ func RunLocalServerTCPWithHandler(laddr string, BlockWiseTransfer bool, BlockWis
	}

	server := &Server{Listener: l, ReadTimeout: time.Second * 3600, WriteTimeout: time.Second * 3600,
		NotifySessionNewFunc: func(s *ClientCommander) {
		NotifySessionNewFunc: func(s *ClientConn) {
			fmt.Printf("networkSession start %v\n", s.RemoteAddr())
		}, NotifySessionEndFunc: func(w *ClientCommander, err error) {
		}, NotifySessionEndFunc: func(w *ClientConn, err error) {
			fmt.Printf("networkSession end %v: %v\n", w.RemoteAddr(), err)
		}, Handler: handler,
		BlockWiseTransfer:    &BlockWiseTransfer,


@@ 157,9 157,9 @@ func RunLocalTLSServer(laddr string, config *tls.Config) (*Server, string, chan 
	}

	server := &Server{Listener: l, ReadTimeout: time.Hour, WriteTimeout: time.Hour,
		NotifySessionNewFunc: func(s *ClientCommander) {
		NotifySessionNewFunc: func(s *ClientConn) {
			fmt.Printf("networkSession start %v\n", s.RemoteAddr())
		}, NotifySessionEndFunc: func(w *ClientCommander, err error) {
		}, NotifySessionEndFunc: func(w *ClientConn, err error) {
			fmt.Printf("networkSession end %v: %v\n", w.RemoteAddr(), err)
		}}