Exemple #1
0
        private void SendPacketsThread()
        {
            try
            {
                while (client.Connected)
                {
                    sendPacketsPending.Reset();

                    byte[][] packets;
                    if (sendPacketsQueue.TryDequeueAll(out packets))
                    {
                        if (!SendPacketsBlocking(packets))
                        {
                            break;
                        }
                    }

                    sendPacketsPending.WaitOne();
                }
            }
            catch (ThreadAbortException)
            {
                // Occurs on Disconnect()
            }
            catch (ThreadInterruptedException)
            {
                // Thrown if the receive packets thread interrupted this thread,
                // while it is exiting, if this thread is waiting on the ManualResetEvent
            }
            catch (IOException)
            {
                // TODO: Handle timeout error
            }
            catch (Exception exception)
            {
                Debug.LogError($"Send packets thread exception:\n{exception}");
                RaiseUnknownError();
            }
            finally
            {
                // Clean up no matter what
                // We might get SocketExceptions when sending if the 'host has
                // failed to respond' - in which case we should close the connection
                // which causes the ReceiveLoop to end and fire the Disconnected
                // message. otherwise the connection would stay alive forever even
                // though we can't send anymore.
                sslStream?.Close();
                client?.Close();
            }
        }