Add a handshaker option to server

This commit is contained in:
iamqizhao
2015-04-30 16:30:48 -07:00
parent 58e0450a3c
commit c6f6e4701d

View File

@ -85,6 +85,7 @@ type Server struct {
} }
type options struct { type options struct {
handshaker func(net.Conn) error
codec Codec codec Codec
maxConcurrentStreams uint32 maxConcurrentStreams uint32
} }
@ -92,6 +93,12 @@ type options struct {
// A ServerOption sets options. // A ServerOption sets options.
type ServerOption func(*options) type ServerOption func(*options)
func Handshaker(f func(net.Conn) error) ServerOption {
return func(o *options) {
o.handshaker = f
}
}
func CustomCodec(codec Codec) ServerOption { func CustomCodec(codec Codec) ServerOption {
return func(o *options) { return func(o *options) {
o.codec = codec o.codec = codec
@ -185,7 +192,14 @@ func (s *Server) Serve(lis net.Listener) error {
if err != nil { if err != nil {
return err return err
} }
// Perform handshaking if it is required.
if s.opts.handshaker != nil {
if err := s.opts.handshaker(c); err != nil {
log.Println("grpc: Server.Serve failed to complete handshake.")
c.Close()
continue
}
}
s.mu.Lock() s.mu.Lock()
if s.conns == nil { if s.conns == nil {
s.mu.Unlock() s.mu.Unlock()