github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/util/socket/socket.go (about) 1 // Copyright 2020 Asim Aslam 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Original source: github.com/micro/go-micro/v3/util/socket/socket.go 16 17 // Package socket provides a pseudo socket 18 package socket 19 20 import ( 21 "io" 22 23 "github.com/tickoalcantara12/micro/v3/service/network/transport" 24 ) 25 26 // Socket is our pseudo socket for transport.Socket 27 type Socket struct { 28 id string 29 // closed 30 closed chan bool 31 // remote addr 32 remote string 33 // local addr 34 local string 35 // send chan 36 send chan *transport.Message 37 // recv chan 38 recv chan *transport.Message 39 } 40 41 func (s *Socket) SetLocal(l string) { 42 s.local = l 43 } 44 45 func (s *Socket) SetRemote(r string) { 46 s.remote = r 47 } 48 49 // Accept passes a message to the socket which will be processed by the call to Recv 50 func (s *Socket) Accept(m *transport.Message) error { 51 select { 52 case s.recv <- m: 53 return nil 54 case <-s.closed: 55 return io.EOF 56 } 57 } 58 59 // Process takes the next message off the send queue created by a call to Send 60 func (s *Socket) Process(m *transport.Message) error { 61 select { 62 case msg := <-s.send: 63 *m = *msg 64 case <-s.closed: 65 // see if we need to drain 66 select { 67 case msg := <-s.send: 68 *m = *msg 69 return nil 70 default: 71 return io.EOF 72 } 73 } 74 return nil 75 } 76 77 func (s *Socket) Remote() string { 78 return s.remote 79 } 80 81 func (s *Socket) Local() string { 82 return s.local 83 } 84 85 func (s *Socket) Send(m *transport.Message) error { 86 // send a message 87 select { 88 case s.send <- m: 89 case <-s.closed: 90 return io.EOF 91 } 92 93 return nil 94 } 95 96 func (s *Socket) Recv(m *transport.Message) error { 97 // receive a message 98 select { 99 case msg := <-s.recv: 100 // set message 101 *m = *msg 102 case <-s.closed: 103 return io.EOF 104 } 105 106 // return nil 107 return nil 108 } 109 110 // Close closes the socket 111 func (s *Socket) Close() error { 112 select { 113 case <-s.closed: 114 // no op 115 default: 116 close(s.closed) 117 } 118 return nil 119 } 120 121 // New returns a new pseudo socket which can be used in the place of a transport socket. 122 // Messages are sent to the socket via Accept and receives from the socket via Process. 123 // SetLocal/SetRemote should be called before using the socket. 124 func New(id string) *Socket { 125 return &Socket{ 126 id: id, 127 closed: make(chan bool), 128 local: "local", 129 remote: "remote", 130 send: make(chan *transport.Message, 128), 131 recv: make(chan *transport.Message, 128), 132 } 133 }