func (cli *LspClient) iRead() ([]byte, error) { lsplog.Vlogf(2, "[Client] Application is attempting to read") if cli.connectionAlive { element := cli.read.Remove() if element == nil { return nil, lsplog.MakeErr("Connection is lost with server") } return element.([]byte), nil } return nil, lsplog.MakeErr("Connection is lost. Cannot read.") }
func (cli *LspClient) iWrite(payload []byte) error { lsplog.Vlogf(2, "[Client] Application is attempting to write") if cli.connectionAlive { go cli.iWriteHandler(payload) // Defers reading to the write handler so the application don't get blocked. return nil } return lsplog.MakeErr("Connection is no alive. Payload is ignored.") }
// Write message to specified client func (srv *LspServer) iWrite(connId uint16, payload []byte) error { lsplog.Vlogf(2, "[Server] Application is writing") for _, v := range srv.clients { // Looks for the client to send if v.connId == connId { m := CreateDataPacket(connId, v.serverSeqNum, payload) go srv.iWriteHandler(m, v) return nil } } return lsplog.MakeErr("Could not find specified connId") }
func iNewLspClient(hostport string, params *LspParams) (*LspClient, error) { cli := new(LspClient) if params == nil { // Insert default parameters params = &LspParams{5, 2000} } cli.params = params addr, err := lspnet.ResolveUDPAddr("udp", hostport) if lsplog.CheckReport(1, err) { return nil, err } cli.lspConn = newConn(addr, 0, 0) // Client's first received message will be data message. cli.lspConn.nextRecvSeqNum = NextSeqNum(0) cli.udpConn, err = lspnet.DialUDP("udp", nil, addr) if lsplog.CheckReport(1, err) { return nil, err } // Need enough room to recycle close messages at end cli.appReadChan = make(LspMessageChan, 2) cli.readBuf = NewBuf() cli.appWriteChan = make(LspMessageChan, 1) cli.netInChan = make(LspMessageChan, 1) cli.epochChan = make(chan int) cli.closeReplyChan = make(chan error, 2) cli.writeReplyChan = make(chan error, 2) go cli.clientLoop() go cli.udpReader() go epochTrigger(cli.params.EpochMilliseconds, cli.epochChan, &cli.lspConn.stopNetworkFlag) // Send connection request to server nm := GenConnectMessage() cli.udpWrite(nm) cli.lspConn.pendingMsg = nm cli.lspConn.nextSendSeqNum = NextSeqNum(0) cm := <-cli.appReadChan if cm.Type == MsgCONNECT { return cli, nil } return nil, lsplog.MakeErr("Connection failed") }