Close() public abstract method

public abstract Close ( ) : void
return void
コード例 #1
0
        public void Disconnect()
        {
            Log.Print(LogType.Game, "Client disconnected.");
            if (Socket.IsConnected)
            {
                Socket.Close();
            }

            isReady = false;

            Socket.Dispose();
        }
コード例 #2
0
		static async Task HandleConnectionAsync(WebSocket ws, CancellationToken cancellation)
		{
			try
			{
				while (ws.IsConnected && !cancellation.IsCancellationRequested)
				{
					String msg = await ws.ReadStringAsync(cancellation).ConfigureAwait(false);
					Log("Message: " + msg);
					ws.WriteString(msg);
				}
			}
			catch (Exception aex)
			{
				Log("Error Handling connection: " + aex.GetBaseException().Message);
				try { ws.Close(); }
				catch { }
			}
			finally
			{
				ws.Dispose();
			}
		}
コード例 #3
0
        static async Task HandleConnectionAsync(WebSocket ws, CancellationToken cancellation)
        {
            try
            {
                IWebSocketLatencyMeasure l = ws as IWebSocketLatencyMeasure;

                PerformanceCounters.Connected.Increment();
                while (ws.IsConnected && !cancellation.IsCancellationRequested)
                {
                    String msg = await ws.ReadStringAsync(cancellation).ConfigureAwait(false);
                    PerformanceCounters.MessagesIn.Increment();

                    ws.WriteString(msg);
                    PerformanceCounters.MessagesOut.Increment();

                    PerformanceCounters.Delay.IncrementBy(l.Latency.Ticks * Stopwatch.Frequency / 10000);
                    PerformanceCounters.DelayBase.Increment();
                }
            }
            catch (TaskCanceledException)
            {

            }
            catch (Exception aex)
            {
                Log("Error Handling connection: " + aex.GetBaseException().Message);
                try { ws.Close(); }
                catch { }
            }
            finally
            {
                ws.Dispose();
                PerformanceCounters.Connected.Decrement();
            }
        }
コード例 #4
0
 protected override Task PerformCloseAsync(CancellationToken cancellationToken)
 {
     _webSocket.Close();
     return(TaskUtil.CompletedTask);
 }
コード例 #5
0
ファイル: Program.cs プロジェクト: papci/WebSocketListener
        static async Task HandleConnectionAsync(WebSocket ws, CancellationToken cancellation)
        {
            bool isAuthenticated = false;
            try
            {
                Interlocked.Increment(ref PerformanceCounters.Connected);
                Console.WriteLine("Connected " + PerformanceCounters.Connected);

                await WaitForAuthenticateMessage(ws, cancellation).ConfigureAwait(false);
                isAuthenticated = true;

                await WaitForMessage(ws, cancellation).ConfigureAwait(false);
            }
            catch (TaskCanceledException)
            {

            }
            catch (Exception aex)
            {
                Log("Error Handling connection: " + aex.GetBaseException().Message);
                try { ws.Close(); }
                catch { }
            }
            finally
            {
                ws.Dispose();
                Interlocked.Increment(ref PerformanceCounters.Connected);
                Interlocked.Increment(ref PerformanceCounters.Accepted);
                if (isAuthenticated)
                {
                    Interlocked.Increment(ref PerformanceCounters.Authenticated);
                }

                Console.WriteLine("Connected " + PerformanceCounters.Connected);
                Console.WriteLine("Accepted " + PerformanceCounters.Accepted);
                Console.WriteLine("Authenticated " + PerformanceCounters.Authenticated);
            }
        }
コード例 #6
0
        static async Task HandleConnectionAsync(WebSocket ws, CancellationToken token)
        {
            try
            {
                PerformanceCounters.Connected.Increment();
                Byte[] buffer = new Byte[2046];
                Int32 readed;

                IWebSocketLatencyMeasure l = ws as IWebSocketLatencyMeasure;
                while (ws.IsConnected && !token.IsCancellationRequested)
                {
                    // await a message
                    using (var messageReader = await ws.ReadMessageAsync(token).ConfigureAwait(false))
                    {
                        if (messageReader == null)
                            continue; // disconnection

                        switch (messageReader.MessageType)
                        {
                            case WebSocketMessageType.Text:

                                PerformanceCounters.MessagesIn.Increment();
                                using (var messageWriter = ws.CreateMessageWriter(WebSocketMessageType.Text))
                                {
                                    readed = -1;
                                    Int32 r = 0;
                                    while(readed!=0)
                                    {
                                        readed = messageReader.Read(buffer, 0, buffer.Length);
                                        if (readed != 0)
                                        {
                                            messageWriter.Write(buffer, 0, readed);
                                            r += readed;
                                        }
                                    }
                                    await messageWriter.CloseAsync(token).ConfigureAwait(false);
                                }
                               PerformanceCounters.MessagesOut.Increment();

                                break;

                            case WebSocketMessageType.Binary:
                                using (var messageWriter = ws.CreateMessageWriter(WebSocketMessageType.Binary))
                                    await messageReader.CopyToAsync(messageWriter).ConfigureAwait(false);
                                break;
                        }
                    }

                    PerformanceCounters.Delay.IncrementBy(l.Latency.Ticks * Stopwatch.Frequency / 10000);
                    PerformanceCounters.DelayBase.Increment();
                }
            }
            catch (TaskCanceledException)
            {

            }
            catch (Exception aex)
            {
                var ex = aex.GetBaseException();
                _log.Error("HandleConnectionAsync", ex);
                Log("Error Handling connection: " + ex.GetType().Name + ": " + ex.Message);
                try { ws.Close(); }
                catch { }
            }
            finally
            {
                ws.Dispose();
                PerformanceCounters.Connected.Decrement();
            }
        }
コード例 #7
0
 async Task HandleConnectionAsync(WebSocket ws, CancellationToken cancellation)
 {
     try
     {
         while (ws.IsConnected && !cancellation.IsCancellationRequested)
         {
             // Get the JSON Message.
             String msg = await ws.ReadStringAsync(cancellation).ConfigureAwait(false);
             if (msg != null)
             {
                 // Convert the JSON String to a dictionary
                 Dictionary<string, dynamic> msgDict = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(msg); ;
                 // If the dictionary contains a uniqueId, the message is a log-in request.
                 if (msgDict.ContainsKey("uniqueId"))
                 {
                     this.HandleMatchQueue(ws, msgDict);
                 }
                 // Message doesn't contain a UniqueId field.
                 else
                 {
                     // Make sure the websocket is in a match
                     if (wsToIdDictionary.ContainsKey(ws))
                     {
                         idToPlayerDictionary[wsToIdDictionary[ws]].currentMatch.AcceptMessage(ws, msgDict);
                     }
                 }
             }
         }
     }
     catch (Exception aex)
     {
         Console.WriteLine("Error Handling connection: " + aex.GetBaseException().Message);
         try { ws.Close(); }
         catch { }
     }
     // Theoretically, this happens when the ws disconnected, right?
     finally
     {
         if (wsToIdDictionary.ContainsKey(ws))
         {
             if (attackerQueue.Contains(wsToIdDictionary[ws]))
             {
                 string tempId = wsToIdDictionary[ws];
                 // loop ends when we find the id matching the ws we want to remove.
                 while (tempId != attackerQueue.Peek())
                 {
                     attackerQueue.Enqueue(attackerQueue.Dequeue());
                 }
                 attackerQueue.Dequeue();
             }
         }
         if (wsToIdDictionary.ContainsKey(ws))
         {
             if (defenderQueue.Contains(wsToIdDictionary[ws]))
             {
                 string tempId = wsToIdDictionary[ws];
                 // loop ends when we find the id matching the ws we want to remove.
                 while (tempId != defenderQueue.Peek())
                 {
                     defenderQueue.Enqueue(attackerQueue.Dequeue());
                 }
                 defenderQueue.Dequeue();
             }
         }
         if (wsToIdDictionary.ContainsKey(ws))
         {
             if (idToPlayerDictionary.ContainsKey(wsToIdDictionary[ws]))
             {
                 idToPlayerDictionary[wsToIdDictionary[ws]].currentMatch.ws_disconnected(ws);
             }
         }
         if (ws.IsConnected)
         {
             ws.Dispose();
         }
         
     }
 }
コード例 #8
0
ファイル: Program.cs プロジェクト: paulfryer/NetworkListener
 private static async Task HandleConnectionAsync(WebSocket ws, CancellationToken cancellation)
 {
     try
     {
         WebSockets.Add(ws);
     }
     catch (Exception aex)
     {
         Log("Error Handling connection: " + aex.GetBaseException().Message);
         try
         {
             ws.Close();
         }
         catch
         {
             Console.WriteLine("Error trying to close websocket connection.");
         }
     }
 }