Example #1
0
        private void beginConnect(IAsyncResult result)
        {
            Socket.EndConnect(result);

            var hello = Encoding.UTF8.GetBytes(Configuration.HelloServer.Header("GET", Configuration.Path));

            Socket.Send(hello, hello.Length, 0);

            Buffers.HTTP.Read buffer = new Buffers.HTTP.Read(new byte[4096]);
            Socket.BeginReceive(buffer.Buffer, 0, buffer.Size, 0, beginReceive, buffer);
        }
Example #2
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);
                    }
                }
            }
        }