separate creating and running IPC server instance

networking
brl 10 years ago
parent 120dbd97a2
commit 81b18bc663

@ -29,25 +29,42 @@ type MsgConn struct {
onClose func() onClose func()
} }
func RunServer(address string, factory MsgFactory, log *logging.Logger, handlers ...interface{}) error { type MsgServer struct {
disp *msgDispatcher
factory MsgFactory
listener *net.UnixListener
done chan bool
idGen <- chan int
}
func NewServer(address string, factory MsgFactory, log *logging.Logger, handlers ...interface{}) (*MsgServer, error) {
md,err := createDispatcher(log, handlers...) md,err := createDispatcher(log, handlers...)
if err != nil { if err != nil {
return err return nil, err
} }
addr := &net.UnixAddr{address, "unix"} listener,err := net.ListenUnix("unix", &net.UnixAddr{address, "unix"})
listener,err := net.ListenUnix("unix", addr)
if err != nil { if err != nil {
md.close() md.close()
return err return nil, err
} }
done := make(chan bool) done := make(chan bool)
idGen := newIdGen(done) idGen := newIdGen(done)
return &MsgServer{
disp: md,
factory: factory,
listener: listener,
done: done,
idGen: idGen,
}, nil
}
func (s *MsgServer) Run() error {
for { for {
conn,err := listener.AcceptUnix() conn,err := s.listener.AcceptUnix()
if err != nil { if err != nil {
close(md.msgs) s.disp.close()
listener.Close() s.listener.Close()
return err return err
} }
if err := setPassCred(conn); err != nil { if err := setPassCred(conn); err != nil {
@ -55,10 +72,10 @@ func RunServer(address string, factory MsgFactory, log *logging.Logger, handlers
} }
mc := &MsgConn{ mc := &MsgConn{
conn: conn, conn: conn,
disp: md, disp: s.disp,
oob: createOobBuffer(), oob: createOobBuffer(),
factory: factory, factory: s.factory,
idGen: idGen, idGen: s.idGen,
respMan: newResponseManager(), respMan: newResponseManager(),
} }
go mc.readLoop() go mc.readLoop()

Loading…
Cancel
Save