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 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> /// 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; } } } }