2017-10-26 12:55:55 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2017-11-01 06:35:03 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
2017-10-26 12:55:55 +00:00
|
|
|
|
|
|
|
"git.loafle.net/commons_go/rpc"
|
2017-11-01 06:35:03 +00:00
|
|
|
"git.loafle.net/commons_go/rpc/protocol/json"
|
|
|
|
"git.loafle.net/commons_go/rpc/server"
|
2017-10-26 12:55:55 +00:00
|
|
|
)
|
|
|
|
|
2017-10-26 13:18:20 +00:00
|
|
|
func NewServerHandler(addr string, registry rpc.Registry) *ServerHandlers {
|
2017-10-31 10:27:26 +00:00
|
|
|
sh := &ServerHandlers{}
|
2017-11-01 06:35:03 +00:00
|
|
|
sh.RPCRegistry = registry
|
|
|
|
sh.addr = addr
|
|
|
|
|
|
|
|
sh.RegisterCodec(json.NewServerCodec(), "json")
|
2017-10-26 12:55:55 +00:00
|
|
|
|
|
|
|
return sh
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerHandlers struct {
|
2017-11-01 06:35:03 +00:00
|
|
|
server.ServerHandlers
|
2017-10-26 12:55:55 +00:00
|
|
|
|
2017-11-01 06:35:03 +00:00
|
|
|
addr string
|
2017-10-26 12:55:55 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 06:35:03 +00:00
|
|
|
func (sh *ServerHandlers) OnStart() {
|
|
|
|
// no op
|
|
|
|
}
|
2017-10-26 13:18:20 +00:00
|
|
|
|
2017-11-01 06:35:03 +00:00
|
|
|
func (sh *ServerHandlers) OnStop() {
|
|
|
|
// no op
|
|
|
|
}
|
2017-10-26 13:18:20 +00:00
|
|
|
|
2017-11-01 06:35:03 +00:00
|
|
|
func (sh *ServerHandlers) Listen() (net.Listener, error) {
|
|
|
|
os.Remove(sh.addr)
|
|
|
|
l, err := net.ListenUnix("unix", &net.UnixAddr{Name: sh.addr, Net: "unix"})
|
|
|
|
if nil == err {
|
|
|
|
os.Chmod(sh.addr, 0777)
|
2017-10-26 13:18:20 +00:00
|
|
|
}
|
2017-11-01 06:35:03 +00:00
|
|
|
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
|
|
|
|
}
|
2017-10-26 12:55:55 +00:00
|
|
|
|
2017-11-01 06:35:03 +00:00
|
|
|
func (sh *ServerHandlers) OnPostWriteError(w io.Writer, err error) {
|
|
|
|
// no op
|
2017-10-26 12:55:55 +00:00
|
|
|
}
|