func (srv *LspServer) iRead() (uint16, []byte, error) { m := <-srv.appReadChan switch m.Type { case MsgDATA: return m.ConnId, m.Payload, nil case MsgINVALID: // Indicates that read should fail. return m.ConnId, nil, lsplog.ConnectionClosed() } return m.ConnId, nil, lsplog.ConnectionClosed() }
func (cli *LspClient) iRead() ([]byte, error) { m := <-cli.appReadChan switch m.Type { case MsgDATA: return m.Payload, nil case MsgINVALID: // Indicates that read should fail // Recycle message so that future reads will also fail cli.appReadChan <- m return nil, lsplog.ConnectionClosed() } return nil, lsplog.ConnectionClosed() }
func (cli *LspClient) readPacket() (m *LspMessage, addr *lspnet.UDPAddr, e error) { b := make([]byte, 1000) n, addr, err := cli.udpConn.ReadFromUDP(b) if addr == nil || n == 0 { lsplog.Vlogf(3, "[Client] Recieved a terminating packet.") return nil, addr, lsplog.ConnectionClosed() } else { lsplog.Vlogf(5, "[Client] Recieved a packet from %s:%d", addr.IP.String(), addr.Port) } if err != nil { lsplog.CheckFatal(err) return nil, nil, err } recieved := CreateEmptyPacket() err = json.Unmarshal(b[0:n], recieved) if err != nil { lsplog.CheckReport(3, err) return nil, addr, err } lsplog.Vlogf(6, "[Client] Recieved packet %+v", recieved) return recieved, addr, err }
// Main client loop func (cli *LspClient) clientLoop() { for !(cli.stopAppFlag && cli.lspConn.stopNetworkFlag) { if cli.readBuf.Empty() { select { case netm := <-cli.netInChan: cli.handleNetMessage(netm) case appm := <-cli.appWriteChan: cli.handleAppWrite(appm) case <-cli.epochChan: cli.handleEpoch() } } else { v := cli.readBuf.Front() rm := v.(*LspMessage) if rm.Type == MsgINVALID { // Have completed all reads. Stop applications cli.stopAppFlag = true } select { case netm := <-cli.netInChan: cli.handleNetMessage(netm) case appm := <-cli.appWriteChan: cli.handleAppWrite(appm) case <-cli.epochChan: cli.handleEpoch() case cli.appReadChan <- rm: cli.readBuf.Remove() } } cli.checkToSend() } // Make sure any subsequent operations fail cli.closeReplyChan <- nil cli.writeReplyChan <- lsplog.ConnectionClosed() cm := GenInvalidMessage(0, 0) cli.appReadChan <- cm }
func (srv *LspServer) readPacket() (m *LspMessage, addr *lspnet.UDPAddr, e error) { b := make([]byte, 1000) n, addr, err := srv.udpConn.ReadFromUDP(b) if addr == nil { lsplog.Vlogf(3, "[Server] Recieved a terminating packet.") return nil, addr, lsplog.ConnectionClosed() } else { lsplog.Vlogf(4, "[Server] Recieved a packet from %s:%d with length %d", addr.IP.String(), addr.Port, n) } if err != nil { lsplog.Vlogf(3, "[Server] Error occured while reading from UDP.") lsplog.CheckFatal(err) return nil, nil, err } recieved := CreateEmptyPacket() err = json.Unmarshal(b[0:n], recieved) if err != nil { lsplog.Vlogf(6, "[Server] Error while unmarshalling %X", b) lsplog.CheckReport(4, err) return nil, addr, err } lsplog.Vlogf(5, "[Server] Recieved packet %+v", recieved) return recieved, addr, err }