func (clnt *Clnt) recv() { var err error err = nil buf := make([]byte, clnt.Msize*8) pos := 0 for { if len(buf) < int(clnt.Msize) { b := make([]byte, clnt.Msize*8) copy(b, buf[0:pos]) buf = b b = nil } n, oerr := clnt.conn.Read(buf[pos:]) if oerr != nil || n == 0 { err = &ixp.Error{oerr.Error(), ixp.EIO} clnt.Lock() clnt.err = err clnt.Unlock() goto closed } pos += n for pos > 4 { sz, _ := ixp.Gint32(buf) if pos < int(sz) { if len(buf) < int(sz) { b := make([]byte, clnt.Msize*8) copy(b, buf[0:pos]) buf = b b = nil } break } fc, err, fcsize := ixp.Unpack(buf, clnt.Dotu) clnt.Lock() if err != nil { clnt.err = err clnt.conn.Close() clnt.Unlock() goto closed } if clnt.Debuglevel > 0 { clnt.logFcall(fc) if clnt.Debuglevel&DbgPrintPackets != 0 { log.Println("}-}", clnt.Id, fmt.Sprint(fc.Pkt)) } if clnt.Debuglevel&DbgPrintFcalls != 0 { log.Println("}}}", clnt.Id, fc.String()) } } var r *Req = nil for r = clnt.reqfirst; r != nil; r = r.next { if r.Tc.Tag == fc.Tag { break } } if r == nil { clnt.err = &ixp.Error{"unexpected response", ixp.EINVAL} clnt.conn.Close() clnt.Unlock() goto closed } r.Rc = fc if r.prev != nil { r.prev.next = r.next } else { clnt.reqfirst = r.next } if r.next != nil { r.next.prev = r.prev } else { clnt.reqlast = r.prev } clnt.Unlock() if r.Tc.Type != r.Rc.Type-1 { if r.Rc.Type != ixp.Rerror { r.Err = &ixp.Error{"invalid response", ixp.EINVAL} log.Println(fmt.Sprintf("TTT %v", r.Tc)) log.Println(fmt.Sprintf("RRR %v", r.Rc)) } else { if r.Err == nil { r.Err = &ixp.Error{r.Rc.Error, r.Rc.Errornum} } } } if r.Done != nil { r.Done <- r } pos -= fcsize buf = buf[fcsize:] } } closed: clnt.done <- true /* send error to all pending requests */ clnt.Lock() r := clnt.reqfirst clnt.reqfirst = nil clnt.reqlast = nil if err == nil { err = clnt.err } clnt.Unlock() for ; r != nil; r = r.next { r.Err = err if r.Done != nil { r.Done <- r } } }
func (conn *Conn) recv() { var err error var n int buf := make([]byte, conn.Msize*8) pos := 0 for { if len(buf) < int(conn.Msize) { b := make([]byte, conn.Msize*8) copy(b, buf[0:pos]) buf = b b = nil } n, err = conn.conn.Read(buf[pos:]) if err != nil || n == 0 { conn.close() return } pos += n for pos > 4 { sz, _ := ixp.Gint32(buf) if sz > conn.Msize { log.Println("bad client connection: ", conn.conn.RemoteAddr()) conn.conn.Close() conn.close() return } if pos < int(sz) { if len(buf) < int(sz) { b := make([]byte, conn.Msize*8) copy(b, buf[0:pos]) buf = b b = nil } break } fc, err, fcsize := ixp.Unpack(buf, conn.Dotu) if err != nil { log.Println(fmt.Sprintf("invalid packet : %v %v", err, buf)) conn.conn.Close() conn.close() return } tag := fc.Tag req := new(Req) select { case req.Rc = <-conn.rchan: break default: req.Rc = ixp.NewFcall(conn.Msize) } req.Conn = conn req.Tc = fc // req.Rc = rc if conn.Debuglevel > 0 { conn.logFcall(req.Tc) if conn.Debuglevel&DbgPrintPackets != 0 { log.Println(">->", conn.Id, fmt.Sprint(req.Tc.Pkt)) } if conn.Debuglevel&DbgPrintFcalls != 0 { log.Println(">>>", conn.Id, req.Tc.String()) } } conn.Lock() conn.nreqs++ conn.tsz += uint64(fc.Size) conn.npend++ if conn.npend > conn.maxpend { conn.maxpend = conn.npend } req.next = conn.reqs[tag] conn.reqs[tag] = req process := req.next == nil if req.next != nil { req.next.prev = req } conn.Unlock() if process { go req.process() } buf = buf[fcsize:] pos -= fcsize } } panic("unreached") }