Ejemplo n.º 1
0
        private bool processPacket(ref NetworkStream stream)
        {
            try
            {
                string data = string.Empty;

                for (int i = 0; i < packets.Count; i++)
                {
                    SocketPacket packet = packets[i];

                    if (packet.operation == 1)
                    {
                        if (packet.isMasked)
                        {
                            byte[] decoded = setMask(packet.payload, packet.mask);
                            data = data + Encoding.UTF8.GetString(decoded);
                        }
                    }
                }

                packetCallback?.Invoke(sessionID, data);

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            isRunning = true;

            NetworkStream stream = client.GetStream();

            while (true)
            {
                if (stream.DataAvailable)
                {
                    if (isHandShake)
                    {
                        if (makeHandShake(ref stream))
                        {
                            isHandShake = false;
                        }
                    }
                    else
                    {
                        SocketPacket packet = new SocketPacket();

                        if (readPacket(ref stream, ref packet))
                        {
                            packets.Add(packet);

                            if (packet.fin)
                            {
                                processPacket(ref stream);
                                packets.Clear();
                            }
                        }
                    }
                }
                else
                {
                    Thread.Sleep(100);
                }
            }
        }
Ejemplo n.º 3
0
        private bool readPacket(ref NetworkStream stream, ref SocketPacket packet)
        {
            try
            {
                //first byte handling
                int firstByte = stream.ReadByte();

                packet.fin = (firstByte & (1 << 7)) != 0;

                packet.rsv1 = (firstByte & (1 << 6)) != 0;
                packet.rsv2 = (firstByte & (1 << 5)) != 0;
                packet.rsv3 = (firstByte & (1 << 4)) != 0;

                packet.operation = firstByte & ((1 << 4) - 1);

                int secondByte = stream.ReadByte();

                packet.isMasked = (secondByte & (1 << 7)) != 0;

                int length = secondByte;

                if (packet.isMasked)
                {
                    length = length - SocketPacket.OFFSET_128;
                }

                if (length > 0 && length < SocketPacket.EXTENDED_PAYLOAD_LENGTH_126)
                {
                    packet.length = (ulong)length;
                }
                else
                {
                    byte[] tempLength = null;
                    //big packet
                    if (length == SocketPacket.EXTENDED_PAYLOAD_LENGTH_126)
                    {
                        tempLength = new byte[2];
                        stream.Read(tempLength, 0, tempLength.Length);
                        Array.Reverse(tempLength);
                        packet.length = (ulong)BitConverter.ToInt16(tempLength, 0);
                    }
                    //biggest packet
                    else if (length == SocketPacket.EXTENDED_PAYLOAD_LENGTH_127)
                    {
                        tempLength = new byte[8];
                        stream.Read(tempLength, 0, tempLength.Length);
                        Array.Reverse(tempLength);
                        packet.length = (ulong)BitConverter.ToInt64(tempLength, 0);
                    }
                }

                packet.mask = new byte[4];
                if (packet.isMasked)
                {
                    stream.Read(packet.mask, 0, packet.mask.Length);
                }

                packet.payload = new byte[packet.length];
                stream.Read(packet.payload, 0, packet.payload.Length);

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }