bool IJakSQL.Select(string _name, out byte _type, out object _data) { if (!connected_) { _type = 0; _data = null; lastError_ = NOT_CONNECTED; return(false); } try { ___select.Parameters.Add(new SQLiteParameter("name", _name)); SQLiteDataReader dr = ___select.ExecuteReader(); if (!(dr.HasRows)) { _type = 0; _data = null; clearParas(); lastError_ = NAME_NOT_FOUND; return(false); } _type = 0; _data = null; while (dr.Read()) { //_type = (byte)(dr["type"]); Byte.TryParse(dr["type"].ToString(), out _type); if (_type == CommunicationsCommon.TYPEOF_NULL) { _data = null; } else if (_type != CommunicationsCommon.TYPEOF_COMPLEX && _type != CommunicationsCommon.TYPEOF_TYPE && _type != CommunicationsCommon.TYPEOF_INTPTR && _type != CommunicationsCommon.TYPEOF_UINTPTR) { Type theType = CommunicationsCommon.switchTypeByte(_type); MethodInfo mi = typeof(SQLiteDataReader).GetMethod("Get" + theType.ToString().Substring(theType.ToString().LastIndexOf(".") + 1)); if (mi == null) { mi = typeof(SQLiteDataReader).GetMethod("Get" + theType.ToString().Substring(theType.ToString().LastIndexOf(".") + 2)); if (mi == null) { mi = typeof(SQLiteDataReader).GetMethod("GetValue"); Console.WriteLine(_name + " got null"); } } _data = mi.Invoke(dr, new object[] { 2 }); } else { _data = dr["data"]; } } dr.Close(); } catch (Exception ex) { clearParas(); lastError_ = "Unknown DB error"; _type = 0; _data = null; return(false); } return(clearParas()); }
void thirdPhase() { long then = DateTime.Now.Ticks; Console.WriteLine("Phase Third:::::"); //sqlite_jaksync a = new sqlite_jaksync (); IJakSQL a = new SQLite_JakSync(); a.User = "******"; a.Password = "******"; a.Connect(); a.UpdateOrInsert("A", CommunicationsCommon.TYPEOF_STRING, (object)"yomamma"); a.UpdateOrInsert("B.a", CommunicationsCommon.TYPEOF_INT, 1); a.UpdateOrInsert("B.b", CommunicationsCommon.TYPEOF_INT, 1); a.DeleteData("B"); a.UpdateOrInsert("A.j", CommunicationsCommon.TYPEOF_DECIMAL, (object)3e-10); a.UpdateOrInsert("A.q", CommunicationsCommon.TYPEOF_ULONG, (object)Int64.MaxValue); a.UpdateOrInsert("A.nullVal", CommunicationsCommon.TYPEOF_NULL, null); Type test = typeof(Window); MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms, test); byte[] bsz = ms.ToArray(); a.UpdateOrInsert("A.TypeTest", CommunicationsCommon.TYPEOF_TYPE, bsz); byte b; object o; a.Select("A", out b, out o); Console.WriteLine("Recieved: a string: {0} ; and the data is /{1}/", (b == CommunicationsCommon.TYPEOF_STRING), o); a.Select("A.j", out b, out o); Console.WriteLine("Recieved: a decimal: {0} ; and the data is {1}", (b == CommunicationsCommon.TYPEOF_DECIMAL), o); a.Select("A.q", out b, out o); Console.WriteLine("Recieved: a long: {0} ; and the data is {1} = {2}", (b == CommunicationsCommon.TYPEOF_ULONG), o.GetType(), UInt64.Parse(o.ToString())); a.Select("A.nullVal", out b, out o); Console.WriteLine("Recieved: a null: {0} ; and the data is {1}", (b == CommunicationsCommon.TYPEOF_NULL), (o == null) ? "NULL" : "\"" + o.ToString() + "\""); a.Select("A.TypeTest", out b, out o); bsz = (byte[])o; test = (Type)(bf.Deserialize(new MemoryStream(bsz))); Console.WriteLine("Recieved: a type: {0} ; and the data is {1} = {2}", (b == CommunicationsCommon.TYPEOF_TYPE), o.GetType(), test.ToString()); Console.WriteLine("\nSelect all:"); Console.WriteLine("{0, -10}{1, -20}{2, -20}", "Name", "Type", "Data"); foreach (object[] i in a.SelectAll()) { Console.WriteLine("{0, -10}{1, -20}{2, -20}", i[0], CommunicationsCommon.switchTypeByte((byte)(i[1])), (i[2] != null) ? i[2] : "NULL"); } //stress test #if STRESS string sts = ""; int x = 10000; //x * (5 + 1) select statements //on a phenom 3x2.3GHz ; 800MB free RAM, no swap ; sqlite3 ; // linux 2.6.35 32bit //x = 100: ~300ms //x = 1000: ~1s //x = 10000: ~33s for (int st = 0; st < x; ++st) { foreach (object[] i in a.SelectAll()) { sts += i[0].ToString(); } } #endif Console.WriteLine("\nAttempting illicit access..."); a.Disconnect(); a.User = "******"; a.Password = ""; Console.WriteLine(a.Connect() + " " + a.LastError); a.User = "******"; a.Password = "******"; Console.WriteLine(a.Connect() + " " + a.LastError); Console.WriteLine("Done. Phase 3 took {0}00 ns", DateTime.Now.Ticks - then); #if STRESS Console.Error.Write(sts.Substring(0, sts.IndexOf("."))); #endif }
/// <summary> /// retrieves everything /// </summary> /// <returns> /// A <see cref="System.Boolean"/> /// </returns> public bool Get() { if (tcpclnt_ == null || !(tcpclnt_.Connected)) { #if DEBUG Console.WriteLine("tcpclnt connected? {0}", (tcpclnt_ != null) ? tcpclnt_.Connected : false); #endif return(false); } try { int bb; int count = 0; byte b; byte[] bytes; int lenName; byte[] name; string nameStr; int lenData; byte[] data; List = null; // tell server to send me everything stream_.WriteByte(CommunicationsCommon.MSG_RECIEVE); // user and password bytes = Encoding.UTF8.GetBytes(user_); byte[] lenBytes = BitConverter.GetBytes(bytes.Length); stream_.Write(lenBytes, 0, lenBytes.Length); //stream_.Write (bytes.Length); stream_.Write(bytes, 0, bytes.Length); bytes = Encoding.UTF8.GetBytes(pass_); lenBytes = BitConverter.GetBytes(bytes.Length); stream_.Write(lenBytes, 0, lenBytes.Length); //stream_.Write (bytes.Length); stream_.Write(bytes, 0, bytes.Length); // read okay / error // read count bb = stream_.ReadByte(); if ((byte)bb != CommunicationsCommon.MSG_ACCEPTED || bb == -1) { return(false); } bytes = BitConverter.GetBytes(new int()); stream_.Read(bytes, 0, bytes.Length); count = BitConverter.ToInt32(bytes, 0); for (int ij = 0; ij < count; ++ij) { bb = stream_.ReadByte(); b = (byte)bb; //bytes = BitConverter.GetBytes (new int ()); stream_.Read(bytes, 0, bytes.Length); lenName = BitConverter.ToInt32(bytes, 0); name = new byte[lenName]; // for (int i = 0; i < lenName; ++i) { // bb = stream_.ReadByte (); // b = (byte)bb; // name[i] = b; // } stream_.Read(name, 0, lenName); nameStr = Encoding.UTF8.GetString(name); // bb = stream_.ReadByte(); // b = (byte)bb; if (b == CommunicationsCommon.TYPEOF_NULL) { list_.Add(new ClassElement(nameStr, null, typeof(object))); continue; } if (b != CommunicationsCommon.TYPEOF_STRING && b != CommunicationsCommon.TYPEOF_COMPLEX && b != CommunicationsCommon.TYPEOF_TYPE) { int size = System.Runtime.InteropServices .Marshal.SizeOf(CommunicationsCommon.switchTypeByte(b)); data = new byte[size]; stream_.Read(data, 0, size); Type theType = CommunicationsCommon.switchTypeByte(b); MethodInfo mi = typeof(BitConverter).GetMethod("To" + theType.ToString().Substring(theType.ToString() .LastIndexOf(".") + 1), BindingFlags.Static | BindingFlags.Public); object theData = mi.Invoke(null, new object[] { data, 0 }); list_.Add(new ClassElement(nameStr, theData, CommunicationsCommon.switchTypeByte(b))); continue; } if (b == CommunicationsCommon.TYPEOF_TYPE) { stream_.Read(bytes, 0, bytes.Length); lenData = BitConverter.ToInt32(bytes, 0); data = new byte[lenData]; stream_.Read(data, 0, lenData); MemoryStream ms = new MemoryStream(data); BinaryFormatter bf = new BinaryFormatter(); object theType = bf.Deserialize(ms); list_.Add(new ClassElement(nameStr, theType, typeof(Type))); continue; } if (b == CommunicationsCommon.TYPEOF_STRING) { stream_.Read(bytes, 0, bytes.Length); lenData = BitConverter.ToInt32(bytes, 0); data = new byte[lenData]; stream_.Read(data, 0, lenData); list_.Add(new ClassElement(nameStr, Encoding.UTF8.GetString(data), typeof(string))); continue; } if (b == CommunicationsCommon.TYPEOF_COMPLEX) { stream_.Read(bytes, 0, bytes.Length); lenData = BitConverter.ToInt32(bytes, 0); data = new byte[lenData]; stream_.Read(data, 0, lenData); list_.Add(new ClassElement(nameStr, data, typeof(ISerializable))); continue; } } // read data // read type:1 lenname:lengthof(int) name:lenname // null // data:switch(type) // lendata:lengthof(int) data:lendata return(true); } catch (IOException ex) { System.Console.WriteLine("error happened: {0}", ex.ToString()); } return(false); }
/// <summary> /// Send whatever data is in List /// </summary> /// <returns> /// True if successful, false or exception for failure /// A <see cref="System.Boolean"/> /// </returns> public bool Send() { if (tcpclnt_ == null || !(tcpclnt_.Connected)) { #if DEBUG Console.WriteLine("tcpclnt connected? {0}", (tcpclnt_ != null) ? tcpclnt_.Connected : false); #endif return(false); } try { byte msg; byte type; int lenName; int lenData = 0; byte[] buffer = new byte[CommunicationsCommon.BUFFER_SIZE]; byte[] bytes; byte[] data = new byte[0]; int bufferJ; #region send number of entries msg = CommunicationsCommon.MSG_SEND; stream_.WriteByte(msg); // user and password bytes = Encoding.UTF8.GetBytes(user_); byte[] lenBytes = BitConverter.GetBytes(bytes.Length); stream_.Write(lenBytes, 0, lenBytes.Length); //stream_.Write (bytes.Length); stream_.Write(bytes, 0, bytes.Length); bytes = Encoding.UTF8.GetBytes(pass_); lenBytes = BitConverter.GetBytes(bytes.Length); stream_.Write(lenBytes, 0, lenBytes.Length); //stream_.Write (bytes.Length); stream_.Write(bytes, 0, bytes.Length); //wait for reply int reply = stream_.ReadByte(); if (reply == -1 || (byte)reply != CommunicationsCommon.MSG_ACCEPTED) { return(false); } bytes = BitConverter.GetBytes(list_.Count); stream_.Write(bytes, 0, bytes.Length); #endregion foreach (ClassElement i in list_) { type = CommunicationsCommon.TYPEOF_NULL; if (i.Payload != null) { type = CommunicationsCommon.switchType(i.MyType); } byte[] name = Encoding.UTF8.GetBytes(i.Name); lenName = name.Length; #region message header (action,type,lenName,name) bufferJ = 0; //buffer[bufferJ++] = msg; //buffer[bufferJ++] = type; stream_.WriteByte(type); bytes = BitConverter.GetBytes(lenName); /*for (int k = 0; k < bytes.Length; ++k) { * buffer[bufferJ++] = bytes[k]; * }*/ stream_.Write(bytes, 0, bytes.Length); stream_.Write(name, 0, lenName); /*for (int k = 0; k < lenName; ++k) { * while (k < lenName && bufferJ < * CommunicationsCommon.BUFFER_SIZE) { * buffer[bufferJ++] = name[k]; * } * * stream_.Write (buffer, 0, bufferJ); * bufferJ = 0; * }*/ #endregion #region message body #region simple data if (type == CommunicationsCommon.TYPEOF_NULL) { // send msg,type,lenname,name // write nothing continue; } else if (type != CommunicationsCommon.TYPEOF_STRING && type != CommunicationsCommon.TYPEOF_COMPLEX && type != CommunicationsCommon.TYPEOF_TYPE) { // get and call the correct GetBytes method that corresponds to i.MyType Type bitConverterType = typeof(BitConverter); MethodInfo getBytes = bitConverterType.GetMethod("GetBytes", BindingFlags.Static | BindingFlags.Public, null, new Type[] { i.MyType }, null); //new Type[] {typeof(int)}, null); Console.WriteLine(i); //BitConverter.GetBytes((int)(i.Payload)); bytes = (byte[])(getBytes.Invoke(null, new object[] { i.Payload })); stream_.Write(bytes, 0, bytes.Length); continue; } #endregion #region string or complex else if (type == CommunicationsCommon.TYPEOF_TYPE) { BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, i.Payload); data = ms.ToArray(); lenData = data.Length; // send msg,type,lenname,name,lendata,data } else if (type == CommunicationsCommon.TYPEOF_STRING) { data = Encoding.UTF8.GetBytes((string)(i.Payload)); lenData = data.Length; // send msg,type,lenname,name,lendata,data } else if (type == CommunicationsCommon.TYPEOF_COMPLEX) { data = (byte[])(i.Payload); lenData = data.Length; // send msg,type,lenname,name,lendata,data } bytes = BitConverter.GetBytes(lenData); stream_.Write(bytes, 0, bytes.Length); stream_.Write(data, 0, lenData); // for(int k = 0; k < lenData; ++k) // { // while(k < lenData && bufferJ < CommunicationsCommon.BUFFER_SIZE) // { // bytes[bufferJ++] = data[k]; // } // // stream_.Write(bytes, 0, bufferJ); // bufferJ = 0; // } #endregion #endregion } } catch (IOException ex) { Console.WriteLine("Something happened to your connection: {0}", ex.ToString()); Disconnect(); return(false); } return(true); }
public void handleClient(object _data) { ClientInfo ci = (ClientInfo)_data; byte b; int bb; NetworkStream stream = ci.socket_.GetStream(); /*while (!stopThread_) */ { bool continue__; while (true) { continue__ = false; lock (clients_) { IDictionaryEnumerator i = clients_.GetEnumerator(); i.Reset(); while (i.MoveNext()) { ClientInfo ci2 = (ClientInfo)(i.Value); if (((int)(i.Key) != ci.id_) && ((IPEndPoint)(ci2.socket_.Client.RemoteEndPoint)).Address.ToString() == ((IPEndPoint)(ci.socket_.Client.RemoteEndPoint)).Address.ToString()) { Console.WriteLine("{0}:{2} waiting on {1}:{3}", ci.id_, ci2.id_, ((IPEndPoint)(ci2.socket_.Client.RemoteEndPoint)).Address, ((IPEndPoint)(ci.socket_.Client.RemoteEndPoint)).Address); Thread.Sleep(1000); continue__ = true; break; } } } if (!continue__) { break; } } if (ci.socket_.Connected) { try { bb = stream.ReadByte(); if (bb == -1) { //break; return; } using (ci.sql_ = (IJakSQL)(Activator.CreateInstance(sql))) { b = (byte)bb; byte[] bytes = new byte[0]; byte[] intBytes = BitConverter.GetBytes(new int()); int count; switch (b) { case CommunicationsCommon.MSG_SEND: case CommunicationsCommon.MSG_RECIEVE: // read user and pass //byte[] bytes; //byte[] intBytes = BitConverter.GetBytes (new int ()); stream.Read(intBytes, 0, intBytes.Length); int len = BitConverter.ToInt32(intBytes, 0); bytes = new byte[len]; stream.Read(bytes, 0, len); string usr = System.Text.Encoding.UTF8.GetString(bytes); stream.Read(intBytes, 0, intBytes.Length); len = BitConverter.ToInt32(intBytes, 0); bytes = new byte[len]; stream.Read(bytes, 0, len); string pass = System.Text.Encoding.UTF8.GetString(bytes); // validate or deny ci.sql_.User = usr; ci.sql_.Password = pass; if (ci.sql_.Connect()) { stream.WriteByte(CommunicationsCommon.MSG_ACCEPTED); } else { stream.WriteByte(CommunicationsCommon.MSG_DENIED); return; } break; default: stream.WriteByte(CommunicationsCommon.MSG_DENIED); return; break; } switch (b) { case CommunicationsCommon.MSG_SEND: // read count stream.Read(intBytes, 0, intBytes.Length); count = BitConverter.ToInt32(intBytes, 0); int size; string name; byte type; for (int i = 0; i < count; ++i) { type = (byte)(stream.ReadByte()); stream.Read(intBytes, 0, intBytes.Length); size = BitConverter.ToInt32(intBytes, 0); bytes = new byte[size]; stream.Read(bytes, 0, size); name = System.Text.Encoding.UTF8.GetString(bytes); switch (type) { case CommunicationsCommon.TYPEOF_NULL: ci.sql_.UpdateOrInsert(name, type, null); break; case CommunicationsCommon.TYPEOF_STRING: stream.Read(intBytes, 0, intBytes.Length); size = BitConverter.ToInt32(intBytes, 0); bytes = new byte[size]; stream.Read(bytes, 0, size); ci.sql_.UpdateOrInsert(name, type, System.Text.Encoding.UTF8.GetString(bytes)); break; case CommunicationsCommon.TYPEOF_COMPLEX: case CommunicationsCommon.TYPEOF_TYPE: stream.Read(intBytes, 0, intBytes.Length); size = BitConverter.ToInt32(intBytes, 0); bytes = new byte[size]; stream.Read(bytes, 0, size); ci.sql_.UpdateOrInsert(name, type, bytes); break; case CommunicationsCommon.TYPEOF_INTPTR: case CommunicationsCommon.TYPEOF_UINTPTR: Console.WriteLine("(U)IntPtr not yet supported: {0} ; {1}", name, ci.id_); break; default: Type theType = CommunicationsCommon.switchTypeByte(type); size = System.Runtime.InteropServices.Marshal.SizeOf(theType); byte[] data = new byte[size]; stream.Read(data, 0, size); MethodInfo mi = typeof(BitConverter).GetMethod("To" + theType.ToString().Substring(theType.ToString().LastIndexOf(".") + 1), BindingFlags.Static | BindingFlags.Public); object theData = mi.Invoke(null, new object[] { data, 0 }); ci.sql_.UpdateOrInsert(name, type, theData); break; } } break; case CommunicationsCommon.MSG_RECIEVE: // write count //int count; ci.sql_.Count(out count); //byte[] bytes; //byte[] intBytes; bytes = BitConverter.GetBytes(count); stream.Write(bytes, 0, bytes.Length); int len; foreach (object[] i in ci.sql_.SelectAll()) { stream.WriteByte((byte)(i[1])); bytes = System.Text.Encoding.UTF8.GetBytes((string)i[0]); intBytes = BitConverter.GetBytes(bytes.Length); stream.Write(intBytes, 0, intBytes.Length); stream.Write(bytes, 0, bytes.Length); switch ((byte)(i[1])) { case CommunicationsCommon.TYPEOF_NULL: break; case CommunicationsCommon.TYPEOF_STRING: bytes = System.Text.Encoding.UTF8.GetBytes((string)(i[2])); intBytes = BitConverter.GetBytes(bytes.Length); stream.Write(intBytes, 0, intBytes.Length); stream.Write(bytes, 0, bytes.Length); break; case CommunicationsCommon.TYPEOF_COMPLEX: case CommunicationsCommon.TYPEOF_TYPE: bytes = (byte[])(i[2]); intBytes = BitConverter.GetBytes(bytes.Length); stream.Write(intBytes, 0, intBytes.Length); stream.Write(bytes, 0, bytes.Length); break; case CommunicationsCommon.TYPEOF_INTPTR: case CommunicationsCommon.TYPEOF_UINTPTR: Console.WriteLine("(U)IntPtr not yet supported: {0} ; {1}", i[0], ci.id_); break; default: // get and call the correct GetBytes method that corresponds to i.MyType Type bitConverterType = typeof(BitConverter); MethodInfo getBytes = bitConverterType.GetMethod("GetBytes", BindingFlags.Static | BindingFlags.Public, null, new Type[] { CommunicationsCommon.switchTypeByte(((byte)(i[1]))) }, null); bytes = (byte[])(getBytes.Invoke(null, new object[] { i[2] })); //bytes = BitConverter.GetBytes (i[2]); stream.Write(bytes, 0, bytes.Length); break; } } break; case CommunicationsCommon.MSG_RECIEVE_SOME: Console.WriteLine("Receiving some not yet supported {0}", ci.id_); break; } ci.sql_.Disconnect(); } }catch (Exception ex) { Console.WriteLine("{0}: {1}", ci.id_, ex.ToString()); //break; goto breaked; } } else { //break; goto breaked; } } breaked: //Thread.Sleep(5000); Console.WriteLine("Lost client {0}:{1}", ci.id_, ci.socket_.Client.RemoteEndPoint); clients_.Remove(ci.id_); }