aboutsummaryrefslogtreecommitdiffstats
path: root/rpc
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2016-02-05 21:08:48 +0800
committerPéter Szilágyi <peterke@gmail.com>2016-02-05 22:53:47 +0800
commit7486904b92449c5955bb682f4ff98752906912b8 (patch)
tree87d1119581754ba411396c0c698fdab4bd65b253 /rpc
parenta13bc9d7a1bc96fab93ace40045c0f0fea4da836 (diff)
downloadgo-tangerine-7486904b92449c5955bb682f4ff98752906912b8.tar
go-tangerine-7486904b92449c5955bb682f4ff98752906912b8.tar.gz
go-tangerine-7486904b92449c5955bb682f4ff98752906912b8.tar.bz2
go-tangerine-7486904b92449c5955bb682f4ff98752906912b8.tar.lz
go-tangerine-7486904b92449c5955bb682f4ff98752906912b8.tar.xz
go-tangerine-7486904b92449c5955bb682f4ff98752906912b8.tar.zst
go-tangerine-7486904b92449c5955bb682f4ff98752906912b8.zip
cmd, node, rpc: move websockets into node, break singleton
Diffstat (limited to 'rpc')
-rw-r--r--rpc/ipc_windows.go6
-rw-r--r--rpc/websocket.go76
2 files changed, 17 insertions, 65 deletions
diff --git a/rpc/ipc_windows.go b/rpc/ipc_windows.go
index 1d4672ad2..09b01974e 100644
--- a/rpc/ipc_windows.go
+++ b/rpc/ipc_windows.go
@@ -239,6 +239,9 @@ func Dial(address string) (*PipeConn, error) {
for {
conn, err := dial(address, nmpwait_wait_forever)
if err == nil {
+ // Ugly hack working around some async connectivity issues
+ time.Sleep(100 * time.Millisecond)
+
return conn, nil
}
if isPipeNotReady(err) {
@@ -360,6 +363,9 @@ func Listen(address string) (*PipeListener, error) {
if err != nil {
return nil, err
}
+ // Ugly hack working around some async connectivity issues
+ time.Sleep(100 * time.Millisecond)
+
return &PipeListener{
addr: PipeAddr(address),
handle: handle,
diff --git a/rpc/websocket.go b/rpc/websocket.go
index b5bcbf4f6..548847602 100644
--- a/rpc/websocket.go
+++ b/rpc/websocket.go
@@ -17,13 +17,11 @@
package rpc
import (
- "errors"
"fmt"
- "net"
"net/http"
- "sync"
-
"os"
+ "strings"
+ "sync"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
@@ -31,12 +29,6 @@ import (
"gopkg.in/fatih/set.v0"
)
-var (
- wsServerMu sync.Mutex
- wsRPCServer *Server
- wsListener net.Listener
-)
-
// wsReaderWriterCloser reads and write payloads from and to a websocket connection.
type wsReaderWriterCloser struct {
c *websocket.Conn
@@ -57,14 +49,6 @@ func (rw *wsReaderWriterCloser) Close() error {
return rw.c.Close()
}
-// wsHandler accepts a websocket connection and handles incoming RPC requests.
-// Will return when the websocket connection is closed, either by the client or
-// server.
-func wsHandler(conn *websocket.Conn) {
- rwc := &wsReaderWriterCloser{conn}
- wsRPCServer.ServeCodec(NewJSONCodec(rwc))
-}
-
// wsHandshakeValidator returns a handler that verifies the origin during the
// websocket upgrade process. When a '*' is specified as an allowed origins all
// connections are accepted.
@@ -103,54 +87,16 @@ func wsHandshakeValidator(allowedOrigins []string) func(*websocket.Config, *http
return f
}
-// StartWS will start a websocket RPC server on the given address and port.
-func StartWS(address string, port int, corsdomains []string, apis []API) error {
- wsServerMu.Lock()
- defer wsServerMu.Unlock()
-
- if wsRPCServer != nil {
- return fmt.Errorf("WS RPC interface already started on %s", wsListener.Addr())
- }
-
- rpcServer := NewServer()
- for _, api := range apis {
- if err := rpcServer.RegisterName(api.Namespace, api.Service); err != nil {
- return err
- }
+// NewWSServer creates a new websocket RPC server around an API provider.
+func NewWSServer(cors string, handler *Server) *http.Server {
+ return &http.Server{
+ Handler: websocket.Server{
+ Handshake: wsHandshakeValidator(strings.Split(cors, ",")),
+ Handler: func(conn *websocket.Conn) {
+ handler.ServeCodec(NewJSONCodec(&wsReaderWriterCloser{conn}))
+ },
+ },
}
-
- listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, port))
- if err != nil {
- return err
- }
-
- wsServer := websocket.Server{Handshake: wsHandshakeValidator(corsdomains), Handler: wsHandler}
- wsHTTPServer := http.Server{Handler: wsServer}
-
- go wsHTTPServer.Serve(listener)
-
- wsListener = listener
- wsRPCServer = rpcServer
-
- return nil
-}
-
-// StopWS stops the running websocket RPC server.
-func StopWS() error {
- wsServerMu.Lock()
- defer wsServerMu.Unlock()
-
- if wsRPCServer == nil {
- return errors.New("HTTP RPC interface not started")
- }
-
- wsListener.Close()
- wsRPCServer.Stop()
-
- wsRPCServer = nil
- wsListener = nil
-
- return nil
}
// wsClient represents a RPC client that communicates over websockets with a