Пример #1
0
        void AsyncSendMessage(NetworkStream stream)
        {
            try
            {
                while (!RequestQueue.IsEmpty)
                {
                    byte[] request = RequestQueue.Dequeue();
                    Array.Resize(ref request, bufferSize);
                    stream.WriteAsync(request, 0, bufferSize);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"AsyncSendMessage: {ex.Message}");

                if (OnFailToSendMessage != null && OnFailToSendMessage.Invoke(ex))
                {
                    Exception disconnectionException = null;

                    try
                    {
                        stream.Dispose();
                    }
                    catch (Exception streamException)
                    {
                        Console.WriteLine($"AsyncSendMessage: {streamException.Message}");
                        disconnectionException = streamException;
                    }

                    OnDisconnect?.Invoke(disconnectionException);
                }
            }
        }
Пример #2
0
        void AsyncSendMessage(Socket socket, ExtendedConcurrentQueue <byte[]> queue, Dictionary <int, object> paramDictionary)
        {
            try
            {
                while (!socket.Connected)
                {
                    Thread.Sleep(100);
                }

                while (!queue.IsEmpty)
                {
                    byte[] request = queue.Dequeue();
                    Array.Resize(ref request, bufferSize);

                    int sent = 0;

                    while (sent < bufferSize)
                    {
                        sent += socket.Send(request, sent, bufferSize - sent, SocketFlags.None);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Message: {ex.Message}");
            }
        }
Пример #3
0
 void AsyncSendMessage(NetworkStream stream)
 {
     try
     {
         while (!RequestQueue.IsEmpty)
         {
             byte[] request = RequestQueue.Dequeue();
             Array.Resize(ref request, bufferSize);
             #warning Things may go wrong??
             stream.WriteAsync(request, 0, bufferSize);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine($"AsyncSendMessage: {ex.Message}");
     }
 }