public void Send(WebSocketData data)
 {
     Frame f = new Frame();
     f.FIN = true;
     f.payloadLength = (ulong)data.bytes.Length;
     f.opcode = (data.data == WebSocketData.Type.Text) ? Frame.Opcode.Text : Frame.Opcode.Binary;
     f.payload = data.bytes;
     byte[] toSend = EncodeFrame(f);
     client.GetStream().Write(toSend, 0, toSend.Length);
 }
        internal void Feed(byte[] data)
        {
            bool success = DecodeFrame(data, f =>
            {

                fragmentsSoFar.Enqueue(f);

                if (f.FIN)
                {
                    if (f.opcode == Frame.Opcode.Close)
                    {
                        Frame closeframe = new Frame();
                        closeframe.FIN = true;
                        closeframe.opcode = Frame.Opcode.Close;
                        closeframe.payloadLength = 0;
                        closeframe.payload = new byte[0];
                        byte[] toSend = EncodeFrame(closeframe);
                        client.GetStream().Write(toSend, 0, toSend.Length);
                        client.Close();

                        return;
                    }


                    WebSocketData.Type type = (fragmentsSoFar.Peek().opcode == Frame.Opcode.Text) ? WebSocketData.Type.Text : WebSocketData.Type.Binary;
                    byte[] bytes = new byte[0];

                    while (fragmentsSoFar.Count != 0)
                    {
                        Frame curr = fragmentsSoFar.Dequeue();
                        bytes = ArrayUtils.Concat(bytes, curr.GetUnmaskedPayload());
                    }

                    WebSocketData wsd = new WebSocketData(type, bytes);
                    onData(this, wsd);
                }

                incompleteFrame = new byte[0];

            });

            if (!success)
            {
                incompleteFrame = ArrayUtils.Concat(incompleteFrame, data);
            }
        }
Exemple #3
0
        internal void Feed(byte[] data)
        {
            bool success = DecodeFrame(data, f =>
            {
                fragmentsSoFar.Enqueue(f);

                if (f.FIN)
                {
                    if (f.opcode == Frame.Opcode.Close)
                    {
                        Frame closeframe         = new Frame();
                        closeframe.FIN           = true;
                        closeframe.opcode        = Frame.Opcode.Close;
                        closeframe.payloadLength = 0;
                        closeframe.payload       = new byte[0];
                        byte[] toSend            = EncodeFrame(closeframe);
                        client.GetStream().Write(toSend, 0, toSend.Length);
                        client.Close();

                        return;
                    }


                    WebSocketData.Type type = (fragmentsSoFar.Peek().opcode == Frame.Opcode.Text) ? WebSocketData.Type.Text : WebSocketData.Type.Binary;
                    byte[] bytes            = new byte[0];

                    while (fragmentsSoFar.Count != 0)
                    {
                        Frame curr = fragmentsSoFar.Dequeue();
                        bytes      = ArrayUtils.Concat(bytes, curr.GetUnmaskedPayload());
                    }

                    WebSocketData wsd = new WebSocketData(type, bytes);
                    onData(this, wsd);
                }

                incompleteFrame = new byte[0];
            });

            if (!success)
            {
                incompleteFrame = ArrayUtils.Concat(incompleteFrame, data);
            }
        }