ref: f9e840758f42671f2e465c869342ce47047ee56c
parent: ea5858d425d540f5f7113e165db709d74554a811
author: Yaroslav Kolomiiets <yarikos@gmail.com>
date: Fri Aug 5 07:23:08 EDT 2016
split scanvc()
--- a/vchan.c
+++ b/vchan.c
@@ -62,8 +62,8 @@
return nil;
}
-void
-scanvc(Rdp* c, Msg* m)
+static int
+defragvc(Rdp*, Msg* m)
{
Vchan* vc;
int n;
@@ -70,14 +70,16 @@
vc = lookupvc(m->chanid);
if(vc == nil)
- return;
+ return -1;
if(m->flags&First)
vc->pos = 0;
- vc->buf = erealloc(vc->buf, m->len);
- vc->nb = m->len;
- n = vc->nb - vc->pos;
+ if(m->len > vc->nb){
+ vc->buf = erealloc(vc->buf, m->len);
+ vc->nb = m->len;
+ }
+ n = m->len - vc->pos;
if(n > m->ndata)
n = m->ndata;
memcpy(vc->buf+vc->pos, m->data, n);
@@ -84,11 +86,28 @@
vc->pos += n;
if(m->flags&Last){
- vc->fn(c, vc->buf, vc->nb);
- free(vc->buf);
- vc->buf = nil;
- vc->nb = 0;
+ m->data = vc->buf;
+ m->ndata = m->len;
+ return m->len;
}
+ return 0;
+}
+
+static void
+callvcfunc(Rdp *c, Msg* m)
+{
+ Vchan* vc;
+ vc = lookupvc(m->chanid);
+ if(vc == nil)
+ return;
+ vc->fn(c, m->data, m->ndata);
+}
+
+void
+scanvc(Rdp* c, Msg* m)
+{
+ if(defragvc(c, m) > 0)
+ callvcfunc(c, m);
}
int