示例#1
0
        public void Send(Buffers.WebSocket.Write buffer)
        {
            var parsed = buffer.Parsed;

            Socket.BeginSend(parsed, 0, parsed.Length, 0, beginSend, buffer);
            OnSending?.Invoke();
        }
示例#2
0
        private void beginSend(IAsyncResult result)
        {
            Buffers.WebSocket.Write buffer = (Buffers.WebSocket.Write)result.AsyncState;

            Socket.EndSend(result);

            if (!buffer.Type.HasFlag(Buffers.WebSocket.Write.DataType.PIN | Buffers.WebSocket.Write.DataType.PON))
            {
                OnSent?.Invoke();
            }
        }
示例#3
0
        private void beginReceive(IAsyncResult result)
        {
            Buffers.Read buffer = (Buffers.Read)result.AsyncState;

            if (!Socket.Connected)
            {
                OnDisconnect?.Invoke();
                return;
            }

            int read = Socket.EndReceive(result);

            if (read == 0)
            {
                Socket.Disconnect(true);
                OnDisconnect?.Invoke();
            }
            else
            {
                Buffers.Read next;

                if (buffer.GetType() == typeof(Buffers.HTTP.Read))
                {
                    Buffers.HTTP.Read current = buffer as Buffers.HTTP.Read;
                    current.Append(read);

                    if (read == buffer.Size)
                    {
                        Socket.BeginReceive(current.Buffer, 0, current.Size, 0, beginReceive, current);
                    }
                    else if (read < buffer.Size)
                    {
                        current.Parse();

                        // Upgrade to websocket on server request
                        if (current.HTTP_STATUS == 101 && current.Headers.Contains(new KeyValuePair <string, string>("Upgrade", "websocket")))
                        {
                            OnConnected?.Invoke();
                            next = new Buffers.WebSocket.Read(new byte[4096]);
                        }
                        else
                        {
                            string payload = current.Payload.Replace("\0", "").Trim();
                            if (!string.IsNullOrEmpty(payload))
                            {
                                OnReceive?.Invoke(payload);
                            }

                            Console.WriteLine("Unable to upgrade connection");
                            OnDisconnect?.Invoke();
                            return;
                        }

                        Socket.BeginReceive(next.Buffer, 0, next.Size, 0, beginReceive, next);
                    }
                }
                else if (buffer.GetType() == typeof(Buffers.WebSocket.Read))
                {
                    Buffers.WebSocket.Read current = buffer as Buffers.WebSocket.Read;
                    current.Append(read);

                    if (read == buffer.Size)
                    {
                        Socket.BeginReceive(current.Buffer, 0, current.Size, 0, beginReceive, current);
                    }
                    else if (read < buffer.Size)
                    {
                        current.Parse();

                        if (current.Type == Buffers.WebSocket.Read.DataType.PIN)   // Answer PON to PIN
                        {
                            Buffers.WebSocket.Write pong = new Buffers.WebSocket.Write(new byte[0], Buffers.WebSocket.Write.DataType.PON, false);
                            Socket.BeginSend(pong.Parsed, 0, pong.Parsed.Length, 0, beginSend, pong);
                        }
                        else
                        {
                            OnReceive?.Invoke(Encoding.UTF8.GetString(current.Payload));
                        }

                        next = new Buffers.WebSocket.Read(new byte[4096]);
                        Socket.BeginReceive(next.Buffer, 0, next.Size, 0, beginReceive, next);
                    }
                }
            }
        }