github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/rpc/ipc.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package rpc
    13  
    14  import (
    15  	"context"
    16  	"fmt"
    17  	"net"
    18  
    19  	"github.com/Sberex/go-sberex/log"
    20  )
    21  
    22  // CreateIPCListener creates an listener, on Unix platforms this is a unix socket, on
    23  // Windows this is a named pipe
    24  func CreateIPCListener(endpoint string) (net.Listener, error) {
    25  	return ipcListen(endpoint)
    26  }
    27  
    28  // ServeListener accepts connections on l, serving JSON-RPC on them.
    29  func (srv *Server) ServeListener(l net.Listener) error {
    30  	for {
    31  		conn, err := l.Accept()
    32  		if err != nil {
    33  			return err
    34  		}
    35  		log.Trace(fmt.Sprint("accepted conn", conn.RemoteAddr()))
    36  		go srv.ServeCodec(NewJSONCodec(conn), OptionMethodInvocation|OptionSubscriptions)
    37  	}
    38  }
    39  
    40  // DialIPC create a new IPC client that connects to the given endpoint. On Unix it assumes
    41  // the endpoint is the full path to a unix socket, and Windows the endpoint is an
    42  // identifier for a named pipe.
    43  //
    44  // The context is used for the initial connection establishment. It does not
    45  // affect subsequent interactions with the client.
    46  func DialIPC(ctx context.Context, endpoint string) (*Client, error) {
    47  	return newClient(ctx, func(ctx context.Context) (net.Conn, error) {
    48  		return newIPCConnection(ctx, endpoint)
    49  	})
    50  }