/* ASYNC METHODS TO FIX * public async Task<bool> ASYNC_send(Guid client_id, byte[] data) { * try { * TcpClient client; * if(AcquireConnectedClient(client_id, out client)) { * logger.Information("Invoked..."); * data=LZ4Compressor.Compress(data); * data=FramingProtocol.WrapMessage(data); * await client.GetStream().WriteAsync(data,0,data.Length).ConfigureAwait(false); * logger.Information("... returned"); * return true;//return await Task.FromResult<bool>(true); * } else { * return false;//await Task.FromResult<bool>(false); * } * } catch(Exception e) { * logger.Information("Exception occurred while sending data as byte[] stream: "+e.Message); * logger.Information(e.StackTrace); * return false;// await Task.FromResult<bool>(false); * } * } * public async Task<bool> ASYNC_send_bytes(Guid client_id, byte[] data) { * return await ASYNC_send(client_id, data); * } * public async Task<bool> ASYNC_send_integers(Guid client_id, int[] data) { * byte[] bytes_data = ConvertArraysIntToByte(data); * return await ASYNC_send(client_id, bytes_data); * } * public async Task<bool[]> ASYNC_sendToAll_bytes(byte[] data) { * bool status = true; * List<Task<bool>> tasks = new List<Task<bool>>(); * foreach(KeyValuePair<Guid,TcpClient> client in _clients) { * Task<bool> task_sendclient = ASYNC_send_bytes(client.Key,data); * status=(!(await task_sendclient) ? false : status); * } * return await Task.WhenAll(tasks.ToArray()); * } */ public bool Send(Guid client_id, byte[] data) { try { TcpClient client; if (AcquireConnectedClient(client_id, out client)) { logger.Information("Data length to compress: " + data.Length + " bytes; compressing and wrapping..."); data = LZ4Compressor.Compress(data); data = FramingProtocol.WrapMessage(data); logger.Information("Data length after processing: " + data.Length + " bytes (Payload size=4 bytes). Writing on stream."); client.GetStream().Write(data, 0, data.Length); logger.Information(data.Length + " bytes wrote on stream!"); return(true); } else { return(false); } } catch (Exception e) { if (e is IOException || e is InvalidOperationException || e is ObjectDisposedException) { logger.Information("Socket connection lost with client with guid: " + client_id + ". Client detached from server."); DisconnectClient(client_id); } else { logger.Information("An error has occurred while sending data: " + e.Message + "\n"); logger.Information(e.StackTrace); } return(false); } }
private byte[] ProcessData(byte[] buffer, int bytes) { if (bytes > 0) { try { logger.Information("It's time to process " + buffer.Length + " bytes. Trying decompressing..."); byte[] data = LZ4Compressor.Decompress(buffer); logger.Information("LZ4 decompression done from " + buffer.Length + " bytes to: " + data.Length + " bytes."); return(data); }catch (ArgumentException) { logger.Information("LZ4 decompression error, skipping frame!"); return(null); } } else { return(null); } }