コード例 #1
0
        protected void LoopListeningAsync()
        {
            Task.Run(async() =>
            {
                while (this.Connected)
                {
                    LoopResetEvent.WaitOne();
                    if (!this.Connected)
                    {
                        this.Disconnect("#server_disconnection_timeout");
                    }
                    LoopResetEvent.Reset();

                    Task.Run(async() =>
                    {
                        try
                        {
                            NetObject obj = await NetData <NetDummy> .ReceiveDataAsync(this.Client.Client, LoopResetEvent)
                                            .ConfigureAwait(true); //ReceiveDataAsync(this.Client.Client).Result;


                            if (this.Connected)
                            {
                                if (obj is NetKick kick)
                                {
                                    Disconnect(kick.Reason, true);
                                    return;
                                }

                                if (obj is NetDummy)
                                {
                                }
                                else
                                {
                                    Task.Run(() =>
                                    {
                                        OnServerDataReceived?.Invoke(obj);

                                        lock (PendingDataLocker) PendingData.Add(obj);
                                    });
                                }
                            }
                        }
                        catch (System.IO.InvalidDataException e)
                        {
                        }
                        catch (AggregateException e)
                        {
                            foreach (Exception exc in e.InnerExceptions)
                            {
                                if (exc is SocketException se)
                                {
                                    Connected = false;
                                    LoopResetEvent.Set();
                                }
                            }

                            if (Connected)
                            {
                                Debug.LogError("Multiple errors while receiving data: " + e);
                                LoopResetEvent.Set();
                            }
                        }
                        catch (SocketException se)
                        {
                            Connected = false;
                            LoopResetEvent.Set();
                        }
                        catch (Exception e)
                        {
                            Debug.LogError("Error while receiving data: " + e);
                            LoopResetEvent.Set();
                        }
                    });
                }

                //Debug.Log("stopped checking data.");

                //Disconnect();
            });
        }
コード例 #2
0
ファイル: BaseServer.cs プロジェクト: Arekva/winecrash
        /// <summary>
        /// Reads all incoming data from a client while <see cref="Running"/> is set to true.
        /// <br>Adds received data to <see cref="PendingData"/></br>
        /// </summary>
        /// <param name="client">The connected client to listen to.</param>
        protected void LoopListening(TcpClient client)
        {
            Task.Run(async() =>
            {
                while (this.Running)
                {
                    NetObject obj = new NetDummy();;

                    try
                    {
                        obj = await NetData <NetDummy> .ReceiveDataAsync(client.Client).ConfigureAwait(true);
                    }
                    catch (NullReferenceException ne)
                    {
                        //Debug.LogError("Unknown error while receiving data from client: " + ne);
                        break;
                    }
                    catch (ObjectDisposedException obje)
                    {
                        //Debug.LogError("Unknown error while receiving data from client: " + obje);
                        break;
                    }
                    catch (SocketException se)
                    {
                        //Debug.LogError("Unknown error while receiving data from client: " + se);
                        break;
                    }
                    catch (System.IO.InvalidDataException de)
                    {
                    }
                    catch (Exception e)
                    {
                        Debug.LogError("Unknown error while receiving data from client: " + e);
                    }

                    if (obj is NetDummy)
                    {
                        continue;
                    }
                    else if (client != null && this.Running)
                    {
                        OnClientDataReceived?.Invoke(client, obj);

                        lock (PendingDataLocker)
                        {
                            PendingData.Add(new PendingData(client, obj));
                        }
                    }

                    /*try
                     * {
                     *  NetObject obj = await NetData<NetDummy>.ReceiveDataAsync(client.Client).ConfigureAwait(true);
                     *
                     *  if (obj is NetDummy)
                     *  {
                     *      continue;
                     *  }
                     *
                     *  if (client != null && this.Running)
                     *  {
                     *      lock (PendingDataLocker)
                     *      {
                     *          OnClientDataReceived?.Invoke(client, obj);
                     *          PendingData.Add(new PendingData(client, obj));
                     *      }
                     *  }
                     * }
                     * catch(SocketException skte) {}
                     * catch(ObjectDisposedException obje) {}
                     * catch(Exception e)
                     * {
                     *  Debug.LogError("Error on receiving infos from client: " + e);
                     *  break;
                     * }*/
                }
            });
        }