Ejemplo n.º 1
0
 private byte[] prepOutgoingEvent(Event ev)
 {
     byte[] data      = ev.ToBytes();
     byte[] encrypted = Encryption2.Encrypt(data, Config.rawKey);
     byte[] preamble  = BitConverter.GetBytes(encrypted.LongLength);
     byte[] package   = new byte[encrypted.Length + 8];
     Buffer.BlockCopy(preamble, 0, package, 0, 8);
     Buffer.BlockCopy(encrypted, 0, package, 8, encrypted.Length);
     return(package);
 }
Ejemplo n.º 2
0
        private void handleConnection(object so)
        {
            string logPrefix = "";

            try
            {
                Socket s        = (Socket)so;
                string endpoint = s.RemoteEndPoint.ToString();
                allEndpoints.Add(endpoint);
                logPrefix = " < " + endpoint + " > ";
                log(logPrefix + "Socket accepted.");
                string st  = string.Empty;
                string _ep = s.RemoteEndPoint.ToString();
                if (object.Equals(null, outQueueRoot[endpoint]))
                {
                    outQueueRoot[endpoint] = new ArrayList();
                }
                s.ReceiveBufferSize = 1000000;
                long   packageLength = 0;
                byte[] mainBuffer    = new byte[0];
                byte[] tempBuffer    = new byte[0];
                while (!shutDownListener)
                {
                    checkInSortQueue();
                    //send an event if there are any
                    if (((ArrayList)outQueueRoot[endpoint]).Count > 0)
                    {
                        Event ev = (Event)((ArrayList)outQueueRoot[endpoint])[0];
                        ((ArrayList)outQueueRoot[endpoint]).RemoveAt(0);
                        ev._WhenSent = Time.Now;
                        byte[] ba = prepOutgoingEvent(ev);
                        if (!s.Connected)
                        {
                            outQueueRoot[endpoint] = null; log("Client Disconnected"); allEndpoints.Remove(endpoint); break;
                        }
                        s.Send(ba, ba.Length, SocketFlags.None);
                    }
                    //read some data if there is any
                    if (s.Available > 0)
                    {
                        byte[] roundBuffer        = new byte[32768]; //32k chunks
                        int    bytesReadThisRound = s.Receive(roundBuffer);
                        tempBuffer = new byte[mainBuffer.Length];
                        Buffer.BlockCopy(mainBuffer, 0, tempBuffer, 0, mainBuffer.Length);
                        mainBuffer = new byte[bytesReadThisRound + tempBuffer.Length];
                        Buffer.BlockCopy(tempBuffer, 0, mainBuffer, 0, tempBuffer.Length);
                        Buffer.BlockCopy(roundBuffer, 0, mainBuffer, tempBuffer.Length, bytesReadThisRound);
                    }
                    //retrieve preamble if needed
                    if (packageLength == 0 && mainBuffer.Length >= 8)
                    {
                        byte[] header = new byte[8];
                        Buffer.BlockCopy(mainBuffer, 0, header, 0, 8);
                        packageLength = BitConverter.ToInt64(header, 0) + 8;
                    }
                    //derive an event if there are any
                    if (packageLength != 0 && mainBuffer.Length >= packageLength)
                    {
                        byte[] packageCrypt = new byte[packageLength - 8];
                        Buffer.BlockCopy(mainBuffer, 8, packageCrypt, 0, (int)packageLength - 8);
                        tempBuffer = new byte[mainBuffer.Length - packageLength];
                        Buffer.BlockCopy(mainBuffer, (int)packageLength, tempBuffer, 0, mainBuffer.Length - (int)packageLength);
                        mainBuffer = new byte[tempBuffer.Length];
                        Buffer.BlockCopy(tempBuffer, 0, mainBuffer, 0, tempBuffer.Length);
                        packageLength = 0;
                        byte[] packageClear = Encryption2.Decrypt(packageCrypt, Config.rawKey);
                        Event  ev           = Event.FromBytes(packageClear);
                        ev._WhenRcvd = Time.Now;
                        ev._Endpoint = endpoint;
                        receiveEvent(ev);
                    }
                    //limit CPU usage
                    Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                log(logPrefix + ex.Message);
            }
        }
Ejemplo n.º 3
0
        public void connect(string host, string port)
        {
            bool x = false;

            try
            {
                disconnect();
                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                client.Connect(host, int.Parse(port));
                client.ReceiveBufferSize = 1000000;
                long   packageLength = 0;
                byte[] mainBuffer    = new byte[0];
                byte[] tempBuffer    = new byte[0];
                byte[] roundBuffer   = new byte[32768]; //32k chunks
                Thread.Sleep(500);
                if (connected)
                {
                    connectCompleted(host, port); x = true;
                }
                while (connected)
                {
                    checkInSortQueue();
                    //send an event if there are any
                    if (outQueueRoot.Count > 0)
                    {
                        Event ev = (Event)outQueueRoot[0];
                        ev._WhenSent = Time.Now;
                        byte[] ba = prepOutgoingEvent(ev);
                        outQueueRoot.RemoveAt(0);
                        client.Send(ba, ba.Length, SocketFlags.None);
                    }
                    //read some data if there is any
                    if (client.Available > 0)
                    {
                        int bytesReadThisRound = client.Receive(roundBuffer);
                        tempBuffer = new byte[mainBuffer.Length];
                        Buffer.BlockCopy(mainBuffer, 0, tempBuffer, 0, mainBuffer.Length);
                        mainBuffer = new byte[bytesReadThisRound + tempBuffer.Length];
                        Buffer.BlockCopy(tempBuffer, 0, mainBuffer, 0, tempBuffer.Length);
                        Buffer.BlockCopy(roundBuffer, 0, mainBuffer, tempBuffer.Length, bytesReadThisRound);
                    }
                    //retrieve preamble if needed
                    if (packageLength == 0 && mainBuffer.Length >= 8)
                    {
                        byte[] header = new byte[8];
                        Buffer.BlockCopy(mainBuffer, 0, header, 0, 8);
                        packageLength = BitConverter.ToInt64(header, 0) + 8;
                    }
                    //derive an event if there are any
                    if (packageLength != 0 && mainBuffer.Length >= packageLength)
                    {
                        byte[] packageCrypt = new byte[packageLength - 8];
                        Buffer.BlockCopy(mainBuffer, 8, packageCrypt, 0, (int)packageLength - 8);
                        tempBuffer = new byte[mainBuffer.Length - packageLength];
                        Buffer.BlockCopy(mainBuffer, (int)packageLength, tempBuffer, 0, mainBuffer.Length - (int)packageLength);
                        mainBuffer = new byte[tempBuffer.Length];
                        Buffer.BlockCopy(tempBuffer, 0, mainBuffer, 0, tempBuffer.Length);
                        packageLength = 0;
                        byte[] packageClear = Encryption2.Decrypt(packageCrypt, Config.rawKey);
                        Event  ev           = Event.FromBytes(packageClear);
                        ev._WhenRcvd = Time.Now;
                        receiveEvent(ev);
                    }
                    //limit CPU usage
                    Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                log(ex.Message);
            }
            finally
            {
                Thread.Sleep(500);
                if (!connected && x)
                {
                    _onDisconnected();
                }
            }
        }