private void WriteData(WebSocketFragment fragment)
        {
            var fragmentData = fragment.GetBuffer();

            try
            {
                _stream.BeginWrite(fragmentData, 0, fragmentData.Length, AsyncWriteHandler, _stream);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error while starting write to network stream: {0}", ex);
            }
        }
 public WebSocketFragmentRecievedEventArgs(WebSocketFragment fragment)
 {
     Fragment = fragment;
 }
 protected void SendPongFrame(WebSocketFragment fragment)
 {
     var pongFragment = new WebSocketFragment(true, OpCode.Pong, fragment.PayloadString);
     SendFragment(pongFragment);
 }
        private void CheckForFrame()
        {
            if (_dataBuffer.Length == 0) return;

            byte[] buffer = _dataBuffer.GetBuffer();
            long dataLength = _dataBuffer.Length;
            var fragmentStart = 0;
            while (fragmentStart < dataLength)
            {
                if (_currentFragmentLength == 0)
                {
                    if (dataLength >= 2)
                    {
                        _currentFragmentLength = 2;

                        var payloadLength = buffer[fragmentStart + 1] & 0x7F;
                        if (payloadLength <= 125)
                        {
                            _currentFragmentLength += payloadLength;
                        }
                        if (payloadLength == 126 && dataLength >= 4)
                        {
                            _currentFragmentLength += buffer[fragmentStart + 2] << 8 | buffer[fragmentStart + 3];
                            _currentFragmentLength += 2;
                        }
                        if (payloadLength == 127 && dataLength >= 10)
                        {
                            _currentFragmentLength +=
                                (buffer[fragmentStart + 2] << 56 |
                                 buffer[fragmentStart + 3] << 48 |
                                 buffer[fragmentStart + 4] << 40 |
                                 buffer[fragmentStart + 5] << 32 |
                                 buffer[fragmentStart + 6] << 24 |
                                 buffer[fragmentStart + 7] << 16 |
                                 buffer[fragmentStart + 8] << 8 |
                                 buffer[fragmentStart + 9]);
                            _currentFragmentLength += 8;
                        }

                        if ((buffer[fragmentStart + 1] & 0x80) != 0) _currentFragmentLength += 4;
                    }
                    else
                    {
                        return;
                    }
                }

                if ((dataLength - fragmentStart) < _currentFragmentLength)
                {
                    _dataBuffer = new MemoryStream();
                    _dataBuffer.Write(buffer, fragmentStart, (int)(dataLength - fragmentStart));
                    return;
                }

                var fragmentBuffer = new byte[_currentFragmentLength];
                Array.Copy(buffer, fragmentStart, fragmentBuffer, 0, _currentFragmentLength);

                var fragment = new WebSocketFragment(fragmentBuffer);
                OnWebSocketFragmentRecieved(this, new WebSocketFragmentRecievedEventArgs(fragment));

                fragmentStart += _currentFragmentLength;

                if (fragmentStart == dataLength)
                {
                    _dataBuffer = new MemoryStream();
                    _currentFragmentLength = 0;
                    return;
                }

                _currentFragmentLength = 0;
            }
        }
        protected WebSocketFragment ConcatFragments()
        {
            var data = new MemoryStream();
            foreach (var f in _fragmentsList)
            {
                var payload = f.PayloadBinary;
                data.Write(payload, 0, payload.Length);
            }
            var firstFragment = _fragmentsList[0];
            var fragment = new WebSocketFragment(true, firstFragment.OpCode, data.GetBuffer(), null, firstFragment.RSV1,
                                                 firstFragment.RSV2, firstFragment.RSV3);

            _fragmentsList.Clear();
            return fragment;
        }
        protected void CloseConnection(WebSocketFragment fragment)
        {
            SendFragment(fragment);

            SendQueueEmpty += (s, e) => CloseNetworkStream();
        }
 public void SendText(string message)
 {
     var fragment = new WebSocketFragment(true, OpCode.TextFrame, message);
     SendFragment(fragment);
 }
        public void SendFragment(WebSocketFragment fragment)
        {
            if (Status != WebSocketConnectionStatus.Open) throw new InvalidOperationException("Connection is not open");

            WriteData(fragment);
        }
        public void CloseConnection(string reason)
        {
            var fragment = new WebSocketFragment(true, OpCode.ConnectionClose, reason);
            SendFragment(fragment);

            Status = WebSocketConnectionStatus.Closing;
            OnClosing(this, new WebSocketConnectionStateChangeEventArgs { Connection = this });
        }