private Message GetDeSerializedData(byte[] message) { var deCompressData = ByteCompressor.DeCompress(message); var deSerializedData = Serializer.DeSerialize(deCompressData); return(deSerializedData); }
private byte[] GetCompressMessage(Message message) { var serializedMessage = Serializer.Serialize(message); var compressMessage = ByteCompressor.Compress(serializedMessage); return(compressMessage); }
private byte[] CreateByteArray(byte[] bytes, string header, bool encrypt) { try { byte[] messageArray = null; byte[] headerArray = null; //Compress message bytes //The header is not worth compressing. bytes = ByteCompressor.CompressBytes(bytes); if (encrypt) { //Header byte[] encryptedPrefix = Encoding.UTF8.GetBytes("ENCRYPTED_"); byte[] encryptedHeader = MessageEncryption.EncryptStringToBytes(header); headerArray = new byte[encryptedHeader.Length + encryptedPrefix.Length]; encryptedPrefix.CopyTo(headerArray, 0); encryptedHeader.CopyTo(headerArray, 10); messageArray = MessageEncryption.EncryptBytes(bytes); } else { headerArray = Encoding.UTF8.GetBytes(header); messageArray = bytes; } //Message byte[] messageData = messageArray; byte[] headerBytes = headerArray; byte[] headerLen = BitConverter.GetBytes(headerBytes.Length); byte[] messageLength = BitConverter.GetBytes(messageData.Length); var data = new byte[4 + 4 + headerBytes.Length + messageData.Length]; messageLength.CopyTo(data, 0); headerLen.CopyTo(data, 4); headerBytes.CopyTo(data, 8); messageData.CopyTo(data, 8 + headerBytes.Length); return(data); } catch (Exception ex) { throw new Exception(ex.ToString()); } }
private static byte[] CalculateGrayScaleThumbnail(WritableLockBitImage image) { byte[] buffer = new byte[image.Width * image.Height]; for (int row = 0; row < image.Height; row++) { for (int col = 0; col < image.Width; col++) { buffer[row * image.Width + col] = image.GetPixel(col, row).R; } } return(ByteCompressor.Compress(buffer)); }
static void Main(string[] args) { IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 33333); Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { listenSocket.Bind(ipPoint); listenSocket.Listen(10); while (true) { Socket connection = listenSocket.Accept(); List <byte> bytes = new List <byte>(); byte[] buffer = new byte[256]; do { var countBytes = connection.Receive(buffer); var freeBytes = buffer.Take(countBytes); bytes.AddRange(freeBytes); }while (connection.Available > 0); var deCompressData = ByteCompressor.DeCompress(bytes.ToArray()); var deSerializedData = Utils.Serializer.DeSerialize(deCompressData); Message returnMessage = default; try { switch (deSerializedData) { case RegistrationMessage registrationMessage: Console.WriteLine(registrationMessage.ToString() + DateTime.Now); try { SqLiteDataBaseProvider.Registration(registrationMessage.Login); returnMessage = new SuccessfulMessage(); } catch (ExistingUserException) { returnMessage = new ErrorMessage() { Error = "User already exist" }; } break; case GetListOfConfigByPeriodMessage getListOfConfigByPeriodMessage: Console.WriteLine(getListOfConfigByPeriodMessage.ToString() + DateTime.Now); var configs = SqLiteDataBaseProvider.GetListOfConfig(getListOfConfigByPeriodMessage.SessionKey, getListOfConfigByPeriodMessage.From, getListOfConfigByPeriodMessage.To); returnMessage = new ConfigListMessage() { Configs = configs }; break; case LogOutMessage logOutMessage: Console.WriteLine(logOutMessage.ToString() + DateTime.Now); var logOut = SqLiteDataBaseProvider.LogOut(logOutMessage.SessionKey); if (logOut) { returnMessage = new SuccessfulMessage(); } else { returnMessage = new ErrorMessage(); } break; case SaveConfigMessage saveConfigMessage: Console.WriteLine(saveConfigMessage.ToString() + DateTime.Now); var saveConfig = SqLiteDataBaseProvider.SaveConfig(saveConfigMessage.SessionKey, saveConfigMessage.Config); if (saveConfig) { returnMessage = new SuccessfulMessage(); } else { returnMessage = new ErrorMessage(); } break; case LogInMessage logInMessage: Console.WriteLine(logInMessage.ToString() + DateTime.Now); try { var sessionKey = SqLiteDataBaseProvider.LogIn(logInMessage.Login); returnMessage = new SessionKeyMessage() { SessionKey = sessionKey }; } catch (NonExistExceptions) { returnMessage = new ErrorMessage() { Error = "User don`t exist" }; } break; default: throw new ArgumentOutOfRangeException(nameof(deSerializedData)); } } catch (Exception e) { returnMessage = new ErrorMessage() { Error = e.ToString() }; } if (returnMessage == default) { throw new Exception(); } var serializedMessage = Serializer.Serialize(returnMessage); var compressMessage = ByteCompressor.Compress(serializedMessage); connection.Send(compressMessage); connection.Shutdown(SocketShutdown.Both); connection.Close(); } } catch (Exception ex) { Console.WriteLine(ex); } }