This commit is contained in:
crusader 2017-11-01 15:35:03 +09:00
parent 87d41a5c4f
commit c4e482999b
4 changed files with 55 additions and 89 deletions

View File

@ -1,21 +0,0 @@
package rpc
import (
"git.loafle.net/commons_go/rpc"
"git.loafle.net/commons_go/rpc/protocol/json"
rpcServer "git.loafle.net/commons_go/rpc/server"
)
func New(registry rpc.Registry) Server {
sh := NewServerHandler(registry)
sh.RegisterCodec(json.NewServerCodec(), "json")
s := rpcServer.New(sh)
return s
}
type Server interface {
rpcServer.Server
}

View File

@ -1,47 +0,0 @@
package rpc
import (
"io"
"git.loafle.net/commons_go/rpc"
rpcServer "git.loafle.net/commons_go/rpc/server"
)
func NewServerHandler(registry rpc.Registry) *ServerHandlers {
sh := &ServerHandlers{}
sh.RPCRegistry = registry
return sh
}
type ServerHandlers struct {
rpcServer.ServerHandlers
}
func (sh *ServerHandlers) GetContentType(r io.Reader) string {
return "json"
}
func (sh *ServerHandlers) OnPreRead(r io.Reader) {
// no op
}
func (sh *ServerHandlers) OnPostRead(r io.Reader) {
// no op
}
func (sh *ServerHandlers) OnPreWriteResult(w io.Writer, result interface{}) {
// no op
}
func (sh *ServerHandlers) OnPostWriteResult(w io.Writer, result interface{}) {
// no op
}
func (sh *ServerHandlers) OnPreWriteError(w io.Writer, err error) {
// no op
}
func (sh *ServerHandlers) OnPostWriteError(w io.Writer, err error) {
// no op
}

View File

@ -9,7 +9,7 @@ func New(addr string, registry rpc.Registry) server.Server {
sh := NewServerHandler(addr, registry) sh := NewServerHandler(addr, registry)
s := server.NewServer(sh) s := server.New(sh)
return s return s
} }

View File

@ -2,41 +2,75 @@ package server
import ( import (
"io" "io"
"net"
"os"
"git.loafle.net/commons_go/rpc" "git.loafle.net/commons_go/rpc"
"git.loafle.net/commons_go/server/ipc" "git.loafle.net/commons_go/rpc/protocol/json"
rpcServer "git.loafle.net/overflow/overflow_discovery/server/rpc" "git.loafle.net/commons_go/rpc/server"
) )
func NewServerHandler(addr string, registry rpc.Registry) *ServerHandlers { func NewServerHandler(addr string, registry rpc.Registry) *ServerHandlers {
sh := &ServerHandlers{} sh := &ServerHandlers{}
sh.Addr = addr sh.RPCRegistry = registry
sh.rpcServer = rpcServer.New(registry) sh.addr = addr
sh.RegisterCodec(json.NewServerCodec(), "json")
return sh return sh
} }
type ServerHandlers struct { type ServerHandlers struct {
ipc.ServerHandlers server.ServerHandlers
rpcServer rpcServer.Server addr string
} }
func (sh *ServerHandlers) Handle(remoteAddr string, rwc io.ReadWriteCloser, stopChan chan struct{}) { func (sh *ServerHandlers) OnStart() {
// no op
Loop: }
for {
if err := sh.rpcServer.Handle(rwc, rwc); nil != err && sh.IsClientDisconnect(err) { func (sh *ServerHandlers) OnStop() {
stopChan <- struct{}{} // no op
break Loop }
}
func (sh *ServerHandlers) Listen() (net.Listener, error) {
select { os.Remove(sh.addr)
case <-stopChan: l, err := net.ListenUnix("unix", &net.UnixAddr{Name: sh.addr, Net: "unix"})
rwc.Close() if nil == err {
return os.Chmod(sh.addr, 0777)
default: }
} return l, err
} }
func (sh *ServerHandlers) OnAccept(conn net.Conn) (net.Conn, error) {
return conn, nil
}
func (sh *ServerHandlers) GetContentType(r io.Reader) string {
return "json"
}
func (sh *ServerHandlers) OnPreRead(r io.Reader) {
// no op
}
func (sh *ServerHandlers) OnPostRead(r io.Reader) {
// no op
}
func (sh *ServerHandlers) OnPreWriteResult(w io.Writer, result interface{}) {
// no op
}
func (sh *ServerHandlers) OnPostWriteResult(w io.Writer, result interface{}) {
// no op
}
func (sh *ServerHandlers) OnPreWriteError(w io.Writer, err error) {
// no op
}
func (sh *ServerHandlers) OnPostWriteError(w io.Writer, err error) {
// no op
} }