Exemplo n.º 1
0
        private void ReceiveCallBack(IAsyncResult asyncResult)
        {
            ConnectionInfo connection = asyncResult.AsyncState as ConnectionInfo;

            try
            {
                Socket      senderSocket = connection.Socket;
                SocketError response;
                int         buffSize = senderSocket.EndReceive(asyncResult, out response);

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

                    PayloadHandler.Handle(payload, connection);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Can't receive data: {e.Message}");
            }
            finally
            {
                try
                {
                    connection.Socket.BeginReceive(connection.Data, 0, connection.Data.Length,
                                                   SocketFlags.None, ReceiveCallBack, connection);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"{e.Message}");
                    var address = connection.Socket.RemoteEndPoint.ToString();

                    Payload payload = new Payload()
                    {
                        type  = "end",
                        topic = "publisher-end",
                        post  = Settings.LAST_WILL_MESSAGE
                    };

                    PayloadStorage.Add(payload);

                    ConnectionsStorage.Remove(address);
                    connection.Socket.Close();
                }
            }
        }
Exemplo n.º 2
0
        private void ReceiveCallback(IAsyncResult asyncResult)
        {
            ConnectionInfo connection = asyncResult.AsyncState as ConnectionInfo;

            try
            {
                Socket senderSocket = connection.Socket;
                // vom pastra rezultat returnat daca s-a citit cu succes
                SocketError response;
                int         buffSize = senderSocket.EndReceive(asyncResult, out response);

                if (response == SocketError.Success)
                {
                    byte[] payload = new byte[buffSize];
                    // aici se copie toate datele din stream
                    Array.Copy(connection.Data, payload, payload.Length);

                    PayloadHandler.Handle(payload, connection);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"We cannot receive buffer data: {e.Message}");
            }
            // dupa ce am citit datele din o conexiune trebuie sa ascultam date noi
            finally
            {
                try
                {
                    connection.Socket.BeginReceive(connection.Data, 0, connection.Data.Length,
                                                   SocketFlags.None, ReceiveCallback, connection);
                }
                catch (Exception e)
                {
                    // cand un socket s-a conectat, consulam, si deconectam
                    Console.Write($"{e.Message}");
                    var address = connection.Socket.RemoteEndPoint.ToString();

                    // stergem din storage
                    ConnectionStorage.Remove(address);

                    connection.Socket.Close();
                }
            }
        }
Exemplo n.º 3
0
        private void ReceiveCallback(IAsyncResult asyncResult)
        {
            ConnectionInfo connection = asyncResult.AsyncState as ConnectionInfo;

            try
            {
                Socket      senderSocket = connection.Socket;
                SocketError response;
                int         buffSize = senderSocket.EndReceive(asyncResult, out response);

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

                    //Handle payload
                    PayloadHandler.Handle(payload, connection);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Can't recieve data. {e.Message}");
            }
            finally
            {
                try
                {
                    connection.Socket.BeginReceive(connection.Data, 0, connection.Data.Length,
                                                   SocketFlags.None, ReceiveCallback, connection);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"{e.Message  }");
                    var address = connection.Socket.RemoteEndPoint.ToString();
                    ConnectionsStorage.Remove(address);
                    connection.Socket.Close();
                }
            }
        }
Exemplo n.º 4
0
        private void ReceiveCallBack(IAsyncResult asyncResult)
        {
            ConnectionInfo connection = asyncResult.AsyncState as ConnectionInfo;

            try
            {
                Socket      senderSocket = connection.Socket;
                SocketError response;
                int         buffsize = senderSocket.EndReceive(asyncResult, out response);

                if (response == SocketError.Success)
                {
                    byte[] payload = new byte[buffsize];
                    Array.Copy(connection.Data, payload, payload.Length);


                    PayloadHandler.Handle(payload, connection);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Could not receive the data from the Broker !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                try
                {
                    connection.Socket.BeginReceive(connection.Data, 0, connection.Data.Length, SocketFlags.None, ReceiveCallBack, connection);
                }
                catch (Exception e)
                {
                    //MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    var address = connection.Socket.RemoteEndPoint.ToString();

                    ConnectionStorage.Remove(address);
                    connection.Socket.Close();
                }
            }
        }