github.com/goplus/gop@v1.2.6/x/jsonrpc2/stdio/server.go (about)

     1  /*
     2   * Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package stdio
    18  
    19  import (
    20  	"context"
    21  	"io"
    22  	"os"
    23  	"sync/atomic"
    24  
    25  	"github.com/goplus/gop/x/fakenet"
    26  	"github.com/goplus/gop/x/jsonrpc2"
    27  	"github.com/goplus/gop/x/jsonrpc2/jsonrpc2test"
    28  )
    29  
    30  // -----------------------------------------------------------------------------
    31  
    32  type listener struct {
    33  }
    34  
    35  func (p *listener) Close() error {
    36  	return nil
    37  }
    38  
    39  // Accept blocks waiting for an incoming connection to the listener.
    40  func (p *listener) Accept(context.Context) (io.ReadWriteCloser, error) {
    41  	connCnt := &connCnt[server]
    42  	if atomic.AddInt32(connCnt, 1) != 1 {
    43  		atomic.AddInt32(connCnt, -1)
    44  		return nil, ErrTooManyConnections
    45  	}
    46  	return fakenet.NewConn("stdio", os.Stdin, os.Stdout), nil
    47  }
    48  
    49  func (p *listener) Dialer() jsonrpc2.Dialer {
    50  	return nil
    51  }
    52  
    53  // Listener returns a jsonrpc2.Listener based on stdin and stdout.
    54  func Listener(allowLocalDail bool) jsonrpc2.Listener {
    55  	if allowLocalDail {
    56  		return jsonrpc2test.NetPipeListener()
    57  	}
    58  	return &listener{}
    59  }
    60  
    61  // -----------------------------------------------------------------------------