github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/chapar/port.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package chapar 4 5 import ( 6 "bytes" 7 8 "../protocol" 9 ) 10 11 type port struct { 12 portNumber byte 13 mux *Multiplexer 14 physicalConnection protocol.NetworkPhysicalConnection 15 } 16 17 func (p *port) PortNumber() byte { return p.portNumber } 18 19 // Send send frame sync that block sender until frame send and sure received successfully! 20 // transmitting that must be non blocking and queue frames for congestion situations! 21 // A situation might be occur that a port available when a frame queued but when the time to send is come, the port broken and sender don't know about this! 22 func (p *port) Send(frame []byte) (err protocol.Error) { 23 err = p.physicalConnection.Send(frame) 24 return 25 } 26 27 // SendAsync send frame async that will not block sender but frame might not send successfully 28 // if port occur problem after port queued frame and caller can't notify about this situation! 29 func (pm *port) SendAsync(frame []byte) (err protocol.Error) { 30 err = pm.physicalConnection.SendAsync(frame) 31 return 32 } 33 34 // Receive read the frame and call upper layer handler! 35 func (p *port) Receive(frame []byte) { 36 var nexHeaderID = GetNextHeader(frame) 37 var path = GetPath(frame) 38 var payload = GetPayload(frame) 39 40 var conn = p.mux.connections.getConnectionByPath(path) 41 if conn == nil { 42 conn = p.mux.connections.newConnection(p, frame) 43 } else if !bytes.Equal(conn.pathFromPeer.Get(), path) { 44 // TODO::: receive frame on alternative path, Any action needed?? 45 } 46 47 p.mux.getTransportHandler(nextHeaderID).Receive(conn, payload) 48 return 49 }