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