private async Task SendFileSegmentAsync(byte[] bytes, string name, long length)
        {
            Task t = null;

            lock (LockObject)
            {
                if (GlobalDefaults.UseEncryption)
                {
                    bytes = CryptoServices.EncryptAES(bytes, Key);
                }
                try
                {
                    bytes = ObjectContainer.Encapsulate(bytes, name, length);

                    t = Connection.SendAsync(bytes);
                }
                catch (SocketException ex)
                {
                    DisconnectedFrom(new DisconnectionContext {
                        type = DisconnectionContext.DisconnectionType.FORCIBLE
                    });
                }
            }
            if (t != null)
            {
                await t.ConfigureAwait(false);
            }
        }
        public async Task SendObjectAsync <T>(T obj)
        {
            if (GlobalDefaults.UseEncryption && !RecivedKey && Key == null && !typeof(T).Equals(typeof(RSAParameters)))
            {
                await Task.Run(() =>
                {
                    while (!RecivedKey)
                    {
                        ;
                    }
                }).ConfigureAwait(false);
            }
            Task t = null;

            lock (LockObject)
                if (IsConnected)
                {
                    byte[] bytes = ObjectParser.ObjectToBytes(obj);
                    if (GlobalDefaults.UseEncryption)
                    {
                        if (!RecivedKey)
                        {
                            if (Key != null)
                            {
                                bytes = CryptoServices.EncryptRSA(bytes, RSAKey);
                            }
                        }
                        else
                        {
                            bytes = CryptoServices.EncryptAES(bytes, Key);
                        }
                    }
                    try
                    {
                        byte[] b = ObjectContainer.Encapsulate(bytes, typeof(T).Name);

                        t = Connection.SendAsync(b);
                    }
                    catch (SocketException ex)
                    {
                        DisconnectedFrom(new DisconnectionContext {
                            type = DisconnectionContext.DisconnectionType.FORCIBLE
                        });
                    }
                }
            if (t != null)
            {
                await t.ConfigureAwait(false);
            }
        }
 private void SendFileSegment(byte[] bytes, string name, long length)
 {
     lock (LockObject)
     {
         if (GlobalDefaults.UseEncryption)
         {
             bytes = CryptoServices.EncryptAES(bytes, Key);
         }
         try
         {
             Connection.Send(ObjectContainer.Encapsulate(bytes, name, length));
         }
         catch (SocketException ex)
         {
             DisconnectedFrom(new DisconnectionContext {
                 type = DisconnectionContext.DisconnectionType.FORCIBLE
             });
         }
     }
 }
        public void SendObject <T>(T obj)
        {
            if (GlobalDefaults.UseEncryption && !RecivedKey && Key == null && !typeof(T).Equals(typeof(RSAParameters)))
            {
                while (!RecivedKey)
                {
                    ;
                }
            }

            lock (LockObject)
                if (IsConnected)
                {
                    byte[] bytes = ObjectParser.ObjectToBytes(obj);
                    if (GlobalDefaults.UseEncryption)
                    {
                        if (!RecivedKey)
                        {
                            if (Key != null)
                            {
                                bytes = CryptoServices.EncryptRSA(bytes, RSAKey);
                            }
                        }
                        else
                        {
                            bytes = CryptoServices.EncryptAES(bytes, Key);
                        }
                    }
                    try
                    {
                        Connection.Send(ObjectContainer.Encapsulate(bytes, typeof(T).Name));
                    }
                    catch (SocketException ex)
                    {
                        DisconnectedFrom(new DisconnectionContext {
                            type = DisconnectionContext.DisconnectionType.FORCIBLE
                        });
                    }
                }
        }