Пример #1
0
        /* 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);
            }
        }
Пример #2
0
 public NetClient(string ip, int port)
 {
     _client             = new TcpClient();
     _client.NoDelay     = true;
     _client.LingerState = new LingerOption(true, 0);
     this.ip             = ip;
     this.port           = port;
     _packetizer         = new FramingProtocol(4573600);
 }
Пример #3
0
 public NetServer(string ip, int port)
 {
     _clients    = new ConcurrentDictionary <Guid, TcpClient>();
     _server     = new TcpListener(IPAddress.Parse(ip), port);
     _packetizer = new FramingProtocol(4573600);
 }