github.com/goplus/gop@v1.2.6/x/jsonrpc2/stdio/stdio.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 "errors" 22 "io" 23 "sync/atomic" 24 25 "github.com/goplus/gop/x/fakenet" 26 "github.com/goplus/gop/x/jsonrpc2" 27 ) 28 29 var ( 30 ErrTooManyConnections = errors.New("too many connections") 31 ) 32 33 const ( 34 client = iota 35 server 36 ) 37 38 var ( 39 connCnt [2]int32 40 ) 41 42 // ----------------------------------------------------------------------------- 43 44 type dialer struct { 45 in io.ReadCloser 46 out io.WriteCloser 47 } 48 49 // Dial returns a new communication byte stream to a listening server. 50 func (p *dialer) Dial(ctx context.Context) (io.ReadWriteCloser, error) { 51 dailCnt := &connCnt[client] 52 if atomic.AddInt32(dailCnt, 1) != 1 { 53 atomic.AddInt32(dailCnt, -1) 54 return nil, ErrTooManyConnections 55 } 56 return fakenet.NewConn("stdio.dialer", p.in, p.out), nil 57 } 58 59 // Dialer returns a jsonrpc2.Dialer based on in and out. 60 func Dialer(in io.ReadCloser, out io.WriteCloser) jsonrpc2.Dialer { 61 return &dialer{in: in, out: out} 62 } 63 64 // Dial makes a new connection based on in and out, wraps the returned 65 // reader and writer using the framer to make a stream, and then builds a 66 // connection on top of that stream using the binder. 67 // 68 // The returned Connection will operate independently using the Preempter and/or 69 // Handler provided by the Binder, and will release its own resources when the 70 // connection is broken, but the caller may Close it earlier to stop accepting 71 // (or sending) new requests. 72 func Dial(in io.ReadCloser, out io.WriteCloser, binder jsonrpc2.Binder, onDone func()) (*jsonrpc2.Connection, error) { 73 return jsonrpc2.Dial(context.Background(), Dialer(in, out), binder, onDone) 74 } 75 76 // -----------------------------------------------------------------------------