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