Ejemplo n.º 1
0
        internal Client(Socket s)
        {
            Connection  = s;
            IsConnected = true;

            IPEndPoint loc = Connection.LocalEndPoint as IPEndPoint;
            IPEndPoint rem = Connection.RemoteEndPoint as IPEndPoint;

            connectionInfo = new ConnectionInfo(loc.Address, Dns.GetHostName(), rem.Address, Dns.GetHostEntry(rem.Address).HostName);

            Reciever = RecieveCoroutine();

            if (!GlobalDefaults.RunServerClientsOnOneThread)
            {
                BackgroundWorker = new Thread(() =>
                {
                    ManagementLoop();
                });
                BackgroundWorker.IsBackground = true;
                BackgroundWorker.Start();
            }
            if (GlobalDefaults.UseEncryption)
            {
                RSAParameters PublicKey;
                CryptoServices.GenerateKeyPair(out PublicKey, out RSAKey);
                SendObject(PublicKey);
            }
        }
Ejemplo n.º 2
0
        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);
            }
        }
Ejemplo n.º 3
0
        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);
            }
        }
Ejemplo n.º 4
0
 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
             });
         }
     }
 }
Ejemplo n.º 5
0
        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
                        });
                    }
                }
        }