github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/rpc/ipc_unix.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:42</date>
    10  //</624450109043642368>
    11  
    12  
    13  //+构建darwin dragonfly freebsd linux nacl netbsd openbsd solaris
    14  
    15  package rpc
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"net"
    21  	"os"
    22  	"path/filepath"
    23  
    24  	"github.com/ethereum/go-ethereum/log"
    25  )
    26  
    27  /*
    28  包括<sys/un.h>
    29  
    30  int max_socket_path_size()
    31  结构sockaddr-un;
    32  返回sizeof(s.sun_path);
    33  }
    34  **/
    35  
    36  import "C"
    37  
    38  //ipclisten将在给定的端点上创建一个Unix套接字。
    39  func ipcListen(endpoint string) (net.Listener, error) {
    40  	if len(endpoint) > int(C.max_socket_path_size()) {
    41  		log.Warn(fmt.Sprintf("The ipc endpoint is longer than %d characters. ", C.max_socket_path_size()),
    42  			"endpoint", endpoint)
    43  	}
    44  
    45  //确保存在IPC路径,并删除以前的任何剩余部分
    46  	if err := os.MkdirAll(filepath.Dir(endpoint), 0751); err != nil {
    47  		return nil, err
    48  	}
    49  	os.Remove(endpoint)
    50  	l, err := net.Listen("unix", endpoint)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	os.Chmod(endpoint, 0600)
    55  	return l, nil
    56  }
    57  
    58  //newipcconnection将连接到给定端点上的UNIX套接字。
    59  func newIPCConnection(ctx context.Context, endpoint string) (net.Conn, error) {
    60  	return dialContext(ctx, "unix", endpoint)
    61  }
    62