This commit is contained in:
crusader 2017-11-30 19:16:55 +09:00
parent ec15abc42d
commit 95adf7ec26
10 changed files with 100 additions and 53 deletions

View File

@ -1,15 +1,15 @@
package client package client
import ( import (
"git.loafle.net/commons_go/rpc" crc "git.loafle.net/commons_go/rpc/client"
"git.loafle.net/commons_go/rpc/client" crcrs "git.loafle.net/commons_go/rpc/client/rwc/socket"
csc "git.loafle.net/commons_go/server/client"
) )
func New(addr string, registry rpc.Registry) client.Client { func New(clientHandler ClientHandler, socketBuilder csc.SocketBuilder) crc.Client {
cRWCHandler := crcrs.New(socketBuilder)
ch := NewClientHandler(addr, registry) c := crc.New(clientHandler, cRWCHandler)
c := client.New(ch)
return c return c
} }

View File

@ -2,13 +2,12 @@ package client
import ( import (
"git.loafle.net/commons_go/rpc" "git.loafle.net/commons_go/rpc"
"git.loafle.net/commons_go/rpc/client" crc "git.loafle.net/commons_go/rpc/client"
"git.loafle.net/commons_go/rpc/protocol/json" "git.loafle.net/commons_go/rpc/protocol/json"
) )
func NewClientHandler(addr string, registry rpc.Registry) ClientHandler { func NewClientHandler(registry rpc.Registry) ClientHandler {
ch := &ClientHandlers{} ch := &ClientHandlers{}
ch.addr = addr
ch.RPCRegistry = registry ch.RPCRegistry = registry
ch.Codec = json.NewClientCodec() ch.Codec = json.NewClientCodec()
@ -16,7 +15,5 @@ func NewClientHandler(addr string, registry rpc.Registry) ClientHandler {
} }
type ClientHandlers struct { type ClientHandlers struct {
client.ClientHandlers crc.ClientHandlers
addr string
} }

View File

@ -1,9 +0,0 @@
package client
import (
"net"
)
func (ch *ClientHandlers) Connect() (net.Conn, error) {
return net.Dial("unix", ch.addr)
}

View File

@ -1,11 +0,0 @@
package client
import (
"net"
"gopkg.in/natefinch/npipe.v2"
)
func (ch *ClientHandlers) Connect() (net.Conn, error) {
return npipe.Dial(ch.addr)
}

17
client/socket_builders.go Normal file
View File

@ -0,0 +1,17 @@
package client
import (
csc "git.loafle.net/commons_go/server/client"
)
func NewSocketBuilder(address string) csc.SocketBuilder {
return newSocketBuilder(address)
}
type SocketBuilders struct {
csc.SocketBuilders
}
func (sb *SocketBuilders) SocketHandler() csc.SocketHandler {
return NewSocketHandler()
}

View File

@ -0,0 +1,13 @@
package client
import (
csc "git.loafle.net/commons_go/server/client"
)
func newSocketBuilder(address string) csc.SocketBuilder {
sb := &SocketBuilders{}
sb.Network = "unix"
sb.Address = address
return sb
}

View File

@ -0,0 +1,21 @@
package client
import (
"net"
csc "git.loafle.net/commons_go/server/client"
"gopkg.in/natefinch/npipe.v2"
)
func newSocketBuilder(address string) csc.SocketBuilder {
sb := &SocketBuilders{}
sb.Network = "pipe"
sb.Address = address
return sb
}
func (sb *SocketBuilders) Dial(dialer *net.Dialer, network, address string) (net.Conn, error) {
return npipe.DialTimeout(`\\.\pipe\`+address, dialer.Timeout)
}

28
client/socket_handlers.go Normal file
View File

@ -0,0 +1,28 @@
package client
import (
"log"
"net"
csc "git.loafle.net/commons_go/server/client"
)
type SocketHandlers struct {
csc.SocketHandlers
}
func (sh *SocketHandlers) OnConnect(socketContext csc.SocketContext, conn net.Conn) {
log.Printf("OnConnect res: %v \n", conn)
}
func (sh *SocketHandlers) OnDisconnect(soc csc.Socket) {
log.Printf("OnDisconnect \n")
}
func (sh *SocketHandlers) Validate() {
sh.SocketHandlers.Validate()
}
func NewSocketHandler() SocketHandler {
return &SocketHandlers{}
}

View File

@ -2,8 +2,6 @@ package server
import ( import (
"git.loafle.net/commons_go/rpc" "git.loafle.net/commons_go/rpc"
crcs "git.loafle.net/commons_go/rpc/connection/socket"
"git.loafle.net/commons_go/rpc/protocol"
"git.loafle.net/commons_go/rpc/server" "git.loafle.net/commons_go/rpc/server"
) )
@ -16,19 +14,6 @@ func newRPCServletHandler(rpcRegistry rpc.Registry) server.ServletHandler {
type RPCServletHandlers struct { type RPCServletHandlers struct {
server.ServletHandlers server.ServletHandlers
rpcIO crcs.ServletHandlers
}
func (sh *RPCServletHandlers) ReadRequest(servletCTX rpc.ServletContext, codec protocol.ServerCodec, conn interface{}) (protocol.ServerRequestCodec, error) {
return sh.rpcIO.ReadRequest(servletCTX, codec, conn)
}
func (sh *RPCServletHandlers) WriteResponse(servletCTX rpc.ServletContext, conn interface{}, requestCodec protocol.ServerRequestCodec, result interface{}, err error) error {
return sh.rpcIO.WriteResponse(servletCTX, conn, requestCodec, result, err)
}
func (sh *RPCServletHandlers) WriteNotification(servletCTX rpc.ServletContext, conn interface{}, codec protocol.ServerCodec, method string, args []interface{}) error {
return sh.rpcIO.WriteNotification(servletCTX, conn, codec, method, args)
} }
func (sh *RPCServletHandlers) Validate() { func (sh *RPCServletHandlers) Validate() {

View File

@ -5,14 +5,19 @@ import (
"sync" "sync"
cRPC "git.loafle.net/commons_go/rpc" cRPC "git.loafle.net/commons_go/rpc"
crsrs "git.loafle.net/commons_go/rpc/server/rwc/socket"
"git.loafle.net/commons_go/rpc/protocol/json" "git.loafle.net/commons_go/rpc/protocol/json"
"git.loafle.net/commons_go/server" "git.loafle.net/commons_go/server"
"git.loafle.net/overflow/overflow_discovery/discovery" "git.loafle.net/overflow/overflow_discovery/discovery"
) )
func newSocketHandler(rpcSH RPCServletHandler) SocketHandler { func newSocketHandler(rpcSH RPCServletHandler) SocketHandler {
rpcRWCSH := crsrs.New()
sh := &SocketHandlers{ sh := &SocketHandlers{
rpcSH: rpcSH, rpcSH: rpcSH,
rpcRWCSH: rpcRWCSH,
} }
return sh return sh
@ -21,6 +26,7 @@ func newSocketHandler(rpcSH RPCServletHandler) SocketHandler {
type SocketHandlers struct { type SocketHandlers struct {
server.SocketHandlers server.SocketHandlers
rpcRWCSH cRPC.ServletReadWriteCloseHandler
rpcSH RPCServletHandler rpcSH RPCServletHandler
} }
@ -45,7 +51,7 @@ func (sh *SocketHandlers) OnConnect(soc server.Socket) {
func (sh *SocketHandlers) Handle(soc server.Socket, stopChan <-chan struct{}, doneChan chan<- error) { func (sh *SocketHandlers) Handle(soc server.Socket, stopChan <-chan struct{}, doneChan chan<- error) {
var err error var err error
rpcServlet := retainRPCServlet(sh.rpcSH) rpcServlet := retainRPCServlet(sh.rpcSH, sh.rpcRWCSH)
discovery.RPCServlet = rpcServlet discovery.RPCServlet = rpcServlet
defer func() { defer func() {
@ -63,8 +69,8 @@ func (sh *SocketHandlers) Handle(soc server.Socket, stopChan <-chan struct{}, do
case err = <-rpcDoneChan: case err = <-rpcDoneChan:
case <-stopChan: case <-stopChan:
rpcServlet.Stop() rpcServlet.Stop()
<-rpcDoneChan
} }
} }
func (sh *SocketHandlers) OnDisconnect(soc server.Socket) { func (sh *SocketHandlers) OnDisconnect(soc server.Socket) {
@ -83,10 +89,10 @@ func (sh *SocketHandlers) Validate() {
var rpcServletPool sync.Pool var rpcServletPool sync.Pool
func retainRPCServlet(sh RPCServletHandler) cRPC.Servlet { func retainRPCServlet(sh RPCServletHandler, rpcRWCSH cRPC.ServletReadWriteCloseHandler) cRPC.Servlet {
v := rpcServletPool.Get() v := rpcServletPool.Get()
if v == nil { if v == nil {
return cRPC.NewServlet(sh) return cRPC.NewServlet(sh, rpcRWCSH)
} }
return v.(cRPC.Servlet) return v.(cRPC.Servlet)
} }