コード例 #1
0
ファイル: Gateway.cs プロジェクト: rafal06/Quarrel
        private void HandleClosed(Exception exception)
        {
            Logger?.LogError(new EventId(), exception, "HandleClosed");

            ConnectedSocket = false;
            if (exception is WebSocketClosedException ex)
            {
                Debug.WriteLine("Gateway closed with code " + ex.CloseCode + " and reason \"" + ex.Reason + "\"");
            }
            GatewayClosed?.Invoke(null, exception);
        }
コード例 #2
0
ファイル: Gateway.cs プロジェクト: rafal06/Quarrel
        public async Task SendMessageAsync(string message)
        {
#if DEBUG
            Debug.WriteLine(">>> " + (message.Length > 80 ? message.Substring(0, 80) + "..." : message));
#endif
            try
            {
                var bytes = Encoding.UTF8.GetBytes(message);
                await _socket.SendAsync(bytes, 0, bytes.Length, true);
            }
            catch (Exception exception)
            {
                ConnectedSocket = false;
                if (exception is WebSocketClosedException ex)
                {
                    Debug.WriteLine("Gateway closed with code " + ex.CloseCode + " and reason \"" + ex.Reason + "\"");
                    GatewayClosed?.Invoke(null, ex);
                }
            }
        }
コード例 #3
0
ファイル: Gateway.cs プロジェクト: uponatime2019/Quarrel
        public async Task SendMessageAsync(string message)
        {
#if DEBUG
            Debug.WriteLine(">>> " + message);
#endif
            try
            {
                _dataWriter.WriteString(message);
                await _dataWriter.StoreAsync();
            }
            catch (Exception exception)
            {
                ConnectedSocket = false;
                var error = Windows.Networking.Sockets.WebSocketError.GetStatus(exception.HResult);
                if (error == WebErrorStatus.ConnectionAborted)
                {
                    Debug.WriteLine(error);
                    GatewayClosed?.Invoke(null, null);
                }
            }
        }
コード例 #4
0
ファイル: Gateway.cs プロジェクト: uponatime2019/Quarrel
        private async void HandleMessage(object sender, MessageWebSocketMessageReceivedEventArgs e)
        {
            try
            {
                if (UseCompression)
                {
                    using (var datastr = e.GetDataStream()?.AsStreamForRead())
                        using (var ms = new MemoryStream())
                        {
                            datastr.CopyTo(ms);
                            ms.Position = 0;
                            byte[] data = new byte[ms.Length];
                            ms.Read(data, 0, (int)ms.Length);
                            int index = 0;
                            int count = data.Length;
                            using (var decompressed = new MemoryStream())
                            {
                                if (data[0] == 0x78)
                                {
                                    _compressed.Write(data, index + 2, count - 2);
                                    _compressed.SetLength(count - 2);
                                }
                                else
                                {
                                    _compressed.Write(data, index, count);
                                    _compressed.SetLength(count);
                                }

                                _compressed.Position = 0;
                                _decompressor.CopyTo(decompressed);
                                _compressed.Position  = 0;
                                decompressed.Position = 0;

                                using (var reader = new StreamReader(decompressed))
                                {
#if DEBUG
                                    string content = await reader.ReadToEndAsync();

                                    Debug.WriteLine("<<< " + content);
                                    SocketFrame frame = JsonConvert.DeserializeObject <SocketFrame>(content);
                                    if (frame.SequenceNumber.HasValue)
                                    {
                                        lastGatewayEventSeq = frame.SequenceNumber.Value;
                                    }
                                    if (operationHandlers.ContainsKey(frame.Operation.GetValueOrDefault()))
                                    {
                                        operationHandlers[frame.Operation.GetValueOrDefault()](frame);
                                    }

                                    if (frame.Type != null && eventHandlers.ContainsKey(frame.Type))
                                    {
                                        eventHandlers[frame.Type](frame);
                                    }
#else
                                    using (JsonReader jsreader = new JsonTextReader(reader))
                                    {
                                        JsonSerializer serializer = new JsonSerializer();
                                        SocketFrame    frame      = serializer.Deserialize <SocketFrame>(jsreader);
                                        if (frame.SequenceNumber.HasValue)
                                        {
                                            lastGatewayEventSeq = frame.SequenceNumber.Value;
                                        }
                                        if (operationHandlers.ContainsKey(frame.Operation.GetValueOrDefault()))
                                        {
                                            operationHandlers[frame.Operation.GetValueOrDefault()](frame);
                                        }

                                        if (frame.Type != null && eventHandlers.ContainsKey(frame.Type))
                                        {
                                            eventHandlers[frame.Type](frame);
                                        }
                                    }
#endif
                                }
                            }
                        }
                }
                else
                {
                    using (var reader = new StreamReader(e.GetDataStream().AsStreamForRead()))
                        using (JsonReader jsreader = new JsonTextReader(reader))
                        {
                            JsonSerializer serializer = new JsonSerializer();
                            SocketFrame    frame      = serializer.Deserialize <SocketFrame>(jsreader);
                            lastGatewayEventSeq = frame.SequenceNumber ?? 0;
                            if (operationHandlers.ContainsKey(frame.Operation.GetValueOrDefault()))
                            {
                                operationHandlers[frame.Operation.GetValueOrDefault()](frame);
                            }

                            if (frame.Type != null && eventHandlers.ContainsKey(frame.Type))
                            {
                                eventHandlers[frame.Type](frame);
                            }
                        }
                }
            }
            catch (Exception exception)
            {
                ConnectedSocket = false;
                var error = Windows.Networking.Sockets.WebSocketError.GetStatus(exception.HResult);
                if (error == WebErrorStatus.ConnectionAborted)
                {
                    GatewayClosed?.Invoke(null, null);
                }
            }
        }
コード例 #5
0
ファイル: Gateway.cs プロジェクト: uponatime2019/Quarrel
 private void HandleClosed(object sender, WebSocketClosedEventArgs args)
 {
     ConnectedSocket = false;
     GatewayClosed?.Invoke(null, args);
     Debug.WriteLine("Gateway closed with code " + args.Code + " and reason \"" + args.Reason + "\"");
 }