/// <summary> /// This login uses a flat file system, data/users/{0}.user - the users name is the file. /// </summary> /// <param name="data"></param> /// <param name="client"></param> /// <returns></returns> public bool Login(byte[] data, Client client) { try { BinaryBuffer buff = new BinaryBuffer(data, true); client.Username = buff.ReadString(buff.ReadInt()); // Username client.Password = buff.ReadString(buff.ReadInt()); // Password buff.EndRead(); buff = new BinaryBuffer(File.ReadAllBytes(string.Format(@"data/users/{0}.svpref", client.Username)), true); LoadClientArguments(ref buff, ref client); // does pass equal? string arg; client.GetArgument("Password", out arg); if (arg != client.Password) return false; buff.EndRead(); } catch (Exception) { return false; } return true; }
public static void RegisterUser(string username, string password) { BinaryBuffer buff = new BinaryBuffer(); buff.BeginWrite(); buff.Write(1); buff.WriteField("Password"); buff.Write((byte)ClientArgument.ClientArgumentTypes._String); buff.WriteField(password); buff.EndWrite(); File.WriteAllBytes(string.Format(@"data/users/{0}.svpref", username), buff.ByteBuffer); }
/// <summary> /// This function will run not matter if it errors... /// </summary> /// <param name="data"></param> /// <param name="client"></param> public void LoadClientArguments(ref BinaryBuffer data, ref Client client) { //how many arguments do we have stored? try { int Total = data.ReadInt(); for (int i = 0; i < Total; i++) client.Arguments.Add(new ClientArgument(data.ReadString(data.ReadInt()), data.ReadByte(), data.ReadByteArray(data.ReadInt()))); } catch (Exception) { // If the file is out of order, they lose there data. // because if they can login then the file has been changed manually. } }
/// <summary> /// This will be called before a client Interface gets called. /// </summary> /// <param name="stream"></param> /// <returns></returns> public bool ProessRequest(NetworkStream stream) { try { BinaryBuffer buff = new BinaryBuffer(Database.ReadBuffer(stream)); buff.BeginRead(); int count = buff.ReadInt(); BinaryBuffer writeBuff = new BinaryBuffer(); writeBuff.BeginWrite(); for (int i = 0; i < count;i++) { IClientRequest Request = CurrentServer.GetRequest(buff.ReadByte()); // if a IClientRequest to close. we need to handle by not reading and writing back! if (!this.TcpConnection.Connected) return false; byte[] data = buff.ReadByteArray(buff.ReadInt()); if(Request != null) { data = Request.Process(new BinaryBuffer(data), this); writeBuff.Write(data.Length); writeBuff.Write(data); } else { writeBuff.Write(0); // length; } } writeBuff.EndWrite(); Database.WriteBuffer(writeBuff.ByteBuffer, stream); return true; } catch (Exception) { } return false; }
/// <summary> /// Read the data from the client. /// </summary> /// <param name="stream"></param> /// <returns></returns> public static byte[] ReadBuffer(NetworkStream stream) { BinaryBuffer buff = new BinaryBuffer(); buff.BeginWrite(); int read = -1; int i = 0; while ((read = stream.ReadByte()) != -1) { buff.Write((byte)read); if (i++ == 3) break; } buff.EndWrite(); if (buff.ByteBuffer.Length != 4) return new byte[] { }; int Contentlength = BitConverter.ToInt32(buff.ByteBuffer, 0); buff = new BinaryBuffer(); int bytesRead = 1; byte[] buffer = new byte[8192]; using (MemoryStream ms = new MemoryStream()) { while (Contentlength > 0 && bytesRead > 0) { bytesRead = stream.Read(buffer, 0, Math.Min(Contentlength, buffer.Length)); ms.Write(buffer, 0, bytesRead); Contentlength -= bytesRead; } stream.Flush(); return ms.ToArray(); } }
/// <summary> /// Datatable to Buffer /// </summary> /// <param name="result"></param> /// <param name="buff"></param> public static void ConvertDataTableToBuffer(DataTable result, ref BinaryBuffer buff) { DataTable dt = (DataTable)result; buff.Write(dt.Columns.Count); TypeCode[] types = new TypeCode[dt.Columns.Count]; for (int i = 0; i < dt.Columns.Count; i++) { buff.WriteField(dt.Columns[i].ColumnName); types[i] = Type.GetTypeCode(dt.Columns[i].DataType); buff.Write((byte)types[i]); } buff.Write(dt.Rows.Count); for (int y = 0; y < dt.Rows.Count; y++) { DataRow dr = dt.Rows[y]; for (int i = 0; i < dt.Columns.Count; i++) { // we need to get the typecode, depending on what type code we set the bytes... object value = dr[i]; TypeCode type = ((IConvertible)value).GetTypeCode(); buff.Write((byte)type); switch (type) { case TypeCode.Byte: case TypeCode.Boolean: buff.Write((byte)value); break; case TypeCode.String: buff.WriteField((string)value); break; case TypeCode.Int16: buff.Write(BitConverter.GetBytes((short)value)); break; case TypeCode.Int32: buff.Write((int)value); break; case TypeCode.Int64: buff.Write((long)value); break; case TypeCode.Single: buff.Write((float)value); break; case TypeCode.Double: buff.Write(BitConverter.GetBytes((double)value)); break; case TypeCode.Decimal: buff.Write((decimal)value); break; case TypeCode.DBNull: //buff.Write((double)value); break; case TypeCode.DateTime: buff.Write(((DateTime)value).ToBinary()); break; } } } }
public static DataTable ConvertBlobToDataTable(ref byte[] data) { DataTable dt = null; if (data == null || data.Length == 0) { return(dt); } BinaryBuffer buff = new BinaryBuffer(data); buff.BeginRead(); buff.ReadInt(); int ColumnCount = buff.ReadInt(); if (ColumnCount > 0) { dt = new DataTable(); TypeCode[] types = new TypeCode[ColumnCount]; for (int i = 0; i < ColumnCount; i++) { string colName = buff.ReadString(buff.ReadInt()); TypeCode type = (TypeCode)buff.ReadByte(); types[i] = type; switch (type) { case TypeCode.Boolean: dt.Columns.Add(colName, typeof(Boolean)); break; case TypeCode.Byte: dt.Columns.Add(colName, typeof(byte)); break; case TypeCode.Char: dt.Columns.Add(colName, typeof(char)); break; case TypeCode.DateTime: dt.Columns.Add(colName, typeof(DateTime)); break; case TypeCode.DBNull: dt.Columns.Add(colName); break; case TypeCode.Decimal: dt.Columns.Add(colName, typeof(decimal)); break; case TypeCode.Double: dt.Columns.Add(colName, typeof(double)); break; case TypeCode.Empty: dt.Columns.Add(colName); break; case TypeCode.Int16: dt.Columns.Add(colName, typeof(short)); break; case TypeCode.Int32: dt.Columns.Add(colName, typeof(int)); break; case TypeCode.Int64: dt.Columns.Add(colName, typeof(long)); break; case TypeCode.Single: dt.Columns.Add(colName, typeof(float)); break; case TypeCode.String: dt.Columns.Add(colName, typeof(string)); break; default: dt.Columns.Add(colName); break; } } int RowCount = buff.ReadInt(); dt.BeginLoadData(); for (int i = 0; i < RowCount; i++) { //DataRow dr = dt.NewRow(); object[] obj = new object[ColumnCount]; for (int y = 0; y < ColumnCount; y++) { TypeCode type = (TypeCode)buff.ReadByte(); switch (type) { case TypeCode.Byte: case TypeCode.Boolean: obj[y] = buff.ReadByte(); break; case TypeCode.String: obj[y] = buff.ReadString(buff.ReadInt()); break; case TypeCode.Int16: obj[y] = BitConverter.ToInt16(buff.ReadByteArray(2), 0); break; case TypeCode.Int32: obj[y] = buff.ReadInt(); break; case TypeCode.Int64: obj[y] = buff.ReadLong(); break; case TypeCode.Single: obj[y] = buff.ReadFloat(); break; case TypeCode.Double: obj[y] = BitConverter.ToDouble(buff.ReadByteArray(8), 0); break; case TypeCode.Decimal: obj[y] = buff.ReadDecimal(); break; case TypeCode.DBNull: obj[y] = DBNull.Value; break; case TypeCode.DateTime: obj[y] = DateTime.FromBinary(buff.ReadLong()); break; default: obj[y] = null; break; } } dt.Rows.Add(obj); } dt.EndLoadData(); if (RowCount > 0) { dt.AcceptChanges(); } } return(dt); }
public static DataTable ConvertBlobToDataTable(ref byte[] data) { DataTable dt = null; if (data == null || data.Length == 0) return dt; BinaryBuffer buff = new BinaryBuffer(data); buff.BeginRead(); buff.ReadInt(); int ColumnCount = buff.ReadInt(); if (ColumnCount > 0) { dt = new DataTable(); TypeCode[] types = new TypeCode[ColumnCount]; for (int i = 0; i < ColumnCount; i++) { string colName = buff.ReadString(buff.ReadInt()); TypeCode type = (TypeCode)buff.ReadByte(); types[i] = type; switch (type) { case TypeCode.Boolean: dt.Columns.Add(colName, typeof(Boolean)); break; case TypeCode.Byte: dt.Columns.Add(colName, typeof(byte)); break; case TypeCode.Char: dt.Columns.Add(colName, typeof(char)); break; case TypeCode.DateTime: dt.Columns.Add(colName, typeof(DateTime)); break; case TypeCode.DBNull: dt.Columns.Add(colName); break; case TypeCode.Decimal: dt.Columns.Add(colName, typeof(decimal)); break; case TypeCode.Double: dt.Columns.Add(colName, typeof(double)); break; case TypeCode.Empty: dt.Columns.Add(colName); break; case TypeCode.Int16: dt.Columns.Add(colName, typeof(short)); break; case TypeCode.Int32: dt.Columns.Add(colName, typeof(int)); break; case TypeCode.Int64: dt.Columns.Add(colName, typeof(long)); break; case TypeCode.Single: dt.Columns.Add(colName, typeof(float)); break; case TypeCode.String: dt.Columns.Add(colName, typeof(string)); break; default: dt.Columns.Add(colName); break; } } int RowCount = buff.ReadInt(); dt.BeginLoadData(); for (int i = 0; i < RowCount; i++) { //DataRow dr = dt.NewRow(); object[] obj = new object[ColumnCount]; for (int y = 0; y < ColumnCount; y++) { TypeCode type = (TypeCode)buff.ReadByte(); switch (type) { case TypeCode.Byte: case TypeCode.Boolean: obj[y] = buff.ReadByte(); break; case TypeCode.String: obj[y] = buff.ReadString(buff.ReadInt()); break; case TypeCode.Int16: obj[y] = BitConverter.ToInt16(buff.ReadByteArray(2), 0); break; case TypeCode.Int32: obj[y] = buff.ReadInt(); break; case TypeCode.Int64: obj[y] = buff.ReadLong(); break; case TypeCode.Single: obj[y] = buff.ReadFloat(); break; case TypeCode.Double: obj[y] = BitConverter.ToDouble(buff.ReadByteArray(8), 0); break; case TypeCode.Decimal: obj[y] = buff.ReadDecimal(); break; case TypeCode.DBNull: obj[y] = DBNull.Value; break; case TypeCode.DateTime: obj[y] = DateTime.FromBinary(buff.ReadLong()); break; default: obj[y] = null; break; } } dt.Rows.Add(obj); } dt.EndLoadData(); if (RowCount > 0) dt.AcceptChanges(); } return dt; }