//2012.01.26 refactored clamp into clampDT, for Date.DateTime() //2012.01.25 rz() clamp datetime to valid range //2010.11.17 Block sending new timetypes to version of kdb+ prior to v2.6 (use prior release of c.cs for older kdb+ versions) // Max buffer size (default 64kB) used for reading is now a parameter to the c constructor // Date, Month, Minute, Second, KTimeSpan are now serializable, implement IComparable // and have default constructors for xml serialization. // Added NULL(Type t) //2010.08.05 Added KException for exceptions due to server error, authentication fail and func decode //2010.01.14 Exposed static var e (Encoding) as public //2010.01.12 Added support for unicode encoding, defaults to ASCII //2010.01.11 Added null checks for qn for UDTs Date,Month,Minute,Second,KTimespan //2010.01.04 Added new time types (timespan->KTimespan,timestamp->DateTime), drop writing kdb+ datetime //2009.10.19 Limit reads to blocks of 64kB to reduce load on kernel memory //2007.09.26 0Wz to MaxValue //2006.10.05 truncate string at null //2006.09.29 NULL c.Date class(sync with c.java) using System;using System.IO;using System.Net.Sockets; //csc c.cs given >q trade.q -p 5001 namespace kx{ public class c:TcpClient{ /*public static void Main(string[]args){ //c.ReceiveTimeout=1000; //c c=new c("localhost",5010);c.k(".u.sub[`trade;`MSFT.O`IBM.N]");while(true){object r=c.k();O(n(at(r,2)));} c c=new c("localhost",5001); //c.e=System.Text.Encoding.UTF8; //O("Unicode "+c.k("`$\"c\"$0x52616e627920426ac3b6726b6c756e64204142")); //object[]x=new object[4];x[0]=t();x[1]="xx";x[2]=(double)93.5;x[3]=300;tm();for(int i=0;i<1000;++i)c.k("insert","trade",x);tm(); //Flip r=td(c.k("select sum price by sym from trade"));O("cols: "+n(r.x));O("rows: "+n(r.y[0])); c.Close(); }*/ private const int DefaultMaxBufferSize=65536; private readonly int _maxBufferSize=DefaultMaxBufferSize; public static System.Text.Encoding e=System.Text.Encoding.ASCII; byte[]b,B;int j,J;bool a,v6;Stream s;public new void Close(){s.Close();base.Close();} public c(string h,int p):this(h,p,Environment.UserName){} public c(string h,int p,string u):this(h,p,u,DefaultMaxBufferSize){} public c(string h,int p,string u,int maxBufferSize){_maxBufferSize=maxBufferSize;Connect(h,p);s=this.GetStream();B=new byte[2+u.Length];J=0;w(u+"\x1");s.Write(B,0,J);if(1!=s.Read(B,0,1)){B=new byte[1+u.Length];Connect(h,p);s=this.GetStream();J=0;w(u);s.Write(B,0,J);if(1!=s.Read(B,0,1))throw new KException("access");}v6=B[0]==1;} static int ns(string s){int i=s.IndexOf('\0');i=-14&&x.Equals(NU[t]);} private void u(){int n=0,r=0,f=0,s=8,p=s;short i=0;j=0;byte[]dst=new byte[ri()];int d=j;int[]aa=new int[256]; while(s>8);}short rh(){int x=b[j++],y=b[j++];return(short)(a?x&0xff|y<<8:x<<8|y&0xff);} void w(int i){w((short)i);w((short)(i>>16));}int ri(){int x=rh(),y=rh();return a?x&0xffff|y<<16:x<<16|y&0xffff;} void w(long j){w((int)j);w((int)(j>>32));}long rj(){int x=ri(),y=ri();return a?x&0xffffffffL|(long)y<<32:(long)x<<32|y&0xffffffffL;} void w(float e){byte[]b=BitConverter.GetBytes(e);foreach(byte i in b)w(i);}float re(){byte c;float e; if(!a){c=b[j];b[j]=b[j+3];b[j+3]=c;c=b[j+1];b[j+1]=b[j+2];b[j+2]=c;}e=BitConverter.ToSingle(b,j);j+=4;return e;} void w(double f){w(BitConverter.DoubleToInt64Bits(f));}double rf(){return BitConverter.Int64BitsToDouble(rj());} void w(char c){w((byte)c);}char rc(){return (char)(b[j++]&0xff);} void w(string s){byte[]b=e.GetBytes(s);foreach(byte i in b)w(i);B[J++]=0;} string rs(){int k=j;for(;b[j]!=0;++j);string s=e.GetString(b,k,j-k);j++;return s;} void w(Date d){w(d.i);}Date rd(){return new Date(ri());} void w(Minute u){w(u.i);}Minute ru(){return new Minute(ri());} void w(Month m){w(m.i);}Month rm(){return new Month(ri());}void w(Second v){w(v.i);}Second rv(){return new Second(ri());} void w(TimeSpan t){if(!v6)throw new KException("Timespan not valid pre kdb+2.6");w(qn(t)?ni:(int)(t.Ticks/10000));}TimeSpan rt(){int i=ri();return new TimeSpan(qn(i)?ni:10000L*i);} void w(DateTime p){if(!v6)throw new KException("Timestamp not valid pre kdb+2.6");w(qn(p)?nj:(100*(p.Ticks-o)));} DateTime rz(){double f=rf();return Double.IsInfinity(f)?(f<0?za:zw):new DateTime(qn(f)?0:clampDT(10000*(long)Math.Round(8.64e7*f)+o));} void w(KTimespan t){w(qn(t)?nj:(t.t.Ticks*100));} KTimespan rn(){return new KTimespan(rj());} DateTime rp(){long j=rj(),d=j<0?(j+1)/100-1:j/100;DateTime p=new DateTime(j==nj?0:o+d);return p;} void w(object x){int t=c.t(x);w((byte)t);if(t<0)switch(t){case-1:w((bool)x);return;case-4:w((byte)x);return; case-5:w((short)x);return;case-6:w((int)x);return;case-7:w((long)x);return;case-8:w((float)x);return;case-9:w((double)x);return; case-10:w((char)x);return;case-11:w((string)x);return;case-12:w((DateTime)x);return;case-13:w((Month)x);return; case-14:w((Date)x);return;case-15:w((DateTime)x);return;case-16:w((KTimespan)x);return;case-17:w((Minute)x);return;case-18:w((Second)x);return; case-19:w((TimeSpan)x);return;} if(t==99){Dict r=(Dict)x;w(r.x);w(r.y);return;}B[J++]=0;if(t==98){Flip r=(Flip)x;B[J++]=99;w(r.x);w(r.y);return;} w(c.n(x));switch(t){ case 0:foreach(object o in(object[])x)w(o);return; case 1:foreach(bool o in(bool[])x)w(o);return; case 4:foreach(byte o in(byte[])x)w(o);return; case 5:foreach(short o in(short[])x)w(o);return; case 6:foreach(int o in(int[])x)w(o);return; case 7:foreach(long o in(long[])x)w(o);return; case 8:foreach(float o in(float[])x)w(o);return; case 9:foreach(double o in(double[])x)w(o);return; case 10:foreach(byte b in e.GetBytes((char[])x))w(b);return; case 11:foreach(string o in(string[])x)w(o);return; case 12:foreach(DateTime o in(DateTime[])x)w(o);return; case 13:foreach(Month o in(Month[])x)w(o);return; case 14:foreach(Date o in(Date[])x)w(o);return; case 15:foreach(DateTime o in(DateTime[])x)w(o);return; case 16:foreach(KTimespan o in(KTimespan[])x)w(o);return; case 17:foreach(Minute o in(Minute[])x)w(o);return; case 18:foreach(Second o in(Second[])x)w(o);return; case 19:foreach(TimeSpan o in(TimeSpan[])x)w(o);return;} } object r(){int i=0,n,t=(sbyte)b[j++];if(t<0)switch(t){case-1:return rb();case-4:return b[j++]; case-5:return rh(); case-6:return ri();case-7:return rj();case-8:return re();case-9:return rf();case-10:return rc();case-11:return rs(); case -12: return rp();case-13:return rm();case-14:return rd();case-15:return rz();case -16:return rn();case-17:return ru(); case-18:return rv();case-19:return rt();} if(t>99){if(t==101&&b[j++]==0)return null;throw new KException("func");}if(t==99)return new Dict(r(),r());j++;if(t==98)return new Flip((Dict)r());n=ri();switch(t){ case 0:object[]L=new object[n];for(;i