コード例 #1
0
        private void ReceiveCallBack(IAsyncResult ar)
        {
            Settings            settings    = ar.AsyncState as Settings;
            TransactionProtocol transaction = ar.AsyncState as TransactionProtocol;

            try
            {
                SocketError response;
                int         buffsize = _socket.EndReceive(ar, out response);
                if (response == SocketError.Success)
                {
                    byte[] payloadbytes = new byte[buffsize];
                    Array.Copy(settings.Buffer, payloadbytes, payloadbytes.Length);

                    PayloadHandler.Handle(payloadbytes, settings);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Can't receive data from Queue Server {e.Message}");
            }
            finally
            {
                try
                {
                    settings.Socket.BeginReceive(settings.Buffer, 0, settings.Buffer.Length, SocketFlags.None, ReceiveCallBack, transaction);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"{e.Message}");
                    settings.Socket.Close();
                }
            }
        }
コード例 #2
0
        private void ReceiveCallback(IAsyncResult asyncResult)
        {
            ConnectionInfo connectionInfo = asyncResult.AsyncState as ConnectionInfo;

            try
            {
                SocketError response;
                int         buffSize = socket.EndReceive(asyncResult, out response);

                if (response == SocketError.Success)
                {
                    byte[] payloadBytes = new byte[buffSize];
                    Array.Copy(connectionInfo.Data, payloadBytes, payloadBytes.Length);



                    PayloadHandler.Handle(payloadBytes);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show($"Could not receive the data from the Broker ! {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                try
                {
                    connectionInfo.Socket.BeginReceive(connectionInfo.Data, 0, connectionInfo.Data.Length, SocketFlags.None, ReceiveCallback, connectionInfo);
                }
                catch (Exception e)
                {
                    //MessageBox.Show($"Error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    connectionInfo.Socket.Close();
                }
            }
        }