Exemplo n.º 1
0
    public ConnectStatus Connect(string serverAddress, int serverPort, string userName, string userPassword)
    {
        // Try to establish server connection
        this.serverAddress = serverAddress;
        this.serverPort    = serverPort;
        this.userName      = userName;
        this.userPassword  = userPassword;

        if ((serverConnection = RabbitNativeMethods.CreateConnection(serverAddress, serverPort)) == IntPtr.Zero)
        {
            return(ConnectStatus.FailedToConnect);
        }

        Debug.Log("Connected to Rabbit Server at " + serverAddress + ":" + serverPort.ToString());

        // Login to the rabbitmq server using the provided credentials
        if (!RabbitNativeMethods.Login(serverConnection, userName, userPassword))
        {
            return(ConnectStatus.FailedToLogin);
        }

        isConnected = true;

#if WINDOWS_UWP
        // Start the thread for the message receiver
        messageConsumerTask = new Task(ConsumeMessageFromQueue);
        messageConsumerTask.Start();
#endif

        return(ConnectStatus.Success);
    }
Exemplo n.º 2
0
 public void SendRabbitMessage(string routingKey, string message)
 {
     if (!isConnected)
     {
         return;
     }
     RabbitNativeMethods.SendString(serverConnection, "stealth", routingKey, message);
 }
Exemplo n.º 3
0
    public bool SubscribeToRoutingKey(string routingKey, SubscribeDelegate callback)
    {
        if (!isConnected)
        {
            return(false);
        }

        lock (_lock)
        {
            if (RabbitNativeMethods.ConnectToExchange(serverConnection, "stealth", routingKey) == IntPtr.Zero)
            {
                Debug.LogWarning("Failed to connect to exchange");
            }
            funcDict[routingKey] = callback;
        }

        return(true);
    }
Exemplo n.º 4
0
    void ConsumeMessageFromQueue()
    {
        uint readAmt   = 0;
        uint headerAmt = 0;

        byte[] header = new byte[500];

        RabbitData currentRabbitData = new RabbitData();

        while (isConnected)
        {
            // Find a data chunk that is available for loading the message
            bool messageReceived = false;

            lock (_lock)
            {
                messageReceived = RabbitNativeMethods.ConsumeMessage(serverConnection, ref headerAmt, header, ref readAmt, currentRabbitData.data);
            }
            // the code that you want to measure comes here

            if (messageReceived)
            {
                if (readAmt == 0)
                {
                    continue;
                }
                currentRabbitData.isUsed = true;

                currentRabbitData.routingkey = System.Text.Encoding.UTF8.GetString(header, 0, (int)headerAmt);

                lock (_lock)
                {
                    if (!dataDict.ContainsKey(currentRabbitData.routingkey))
                    {
                        dataDict[currentRabbitData.routingkey] = new RabbitData();
                    }

                    currentRabbitData.data.CopyTo(dataDict[currentRabbitData.routingkey].data, 0);
                }
            }
        }
    }