public static ConnackWrk ProcessConnect(byte[] input, ref byte[] clientId)
        {
            MessageInterface.ConnectWrk connect = new ConnectWrk(input); //typecasting
            clientId = connect.connect.clientId;

            if (connect.connect.flags == Flag.cleanSession)                          //TODO: if there are multiple flags, then equal to wont work
            {
                Clients.Delete(clientId, sqliteConnection);
                Publisher.Delete(clientId, sqliteConnection);
                Subscriber.Delete(clientId, sqliteConnection);
            }
            //read the connect object and validate the clientID
            //send appropriate response
            MessageInterface.ConnackWrk connack = new ConnackWrk();
            if (ClientAuthenticate.Validate(clientId))
            {
                Clients client = new Clients(connect.connect);
                Clients.Insert(client, sqliteConnection);
                connack.connack.returnCode = MessageInterface.ReturnCodes.Accepted;
            }
            else
            {
                connack.connack.returnCode = MessageInterface.ReturnCodes.Congestion;
            }

            return(connack);
        }
Ejemplo n.º 2
0
        private static void ReceiveResponse(byte[] input)
        {
            ConnackWrk con = new ConnackWrk(input);

            Console.WriteLine("\t\t\t\t\t\t Connack: " + con.connack.returnCode);
        }
        private static void SocketLoop(Object sObj)
        {
            Socket socket = (Socket)sObj;

            byte[] header = new byte[2];
            byte[] messageBody;

            socket.Listen(0);
            Socket handler = socket.Accept();

            byte[] clientId = new byte[4];
            int    length   = 0;


            while (true)
            {
                try
                {
                    handler.Receive(header, 2, SocketFlags.None);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception occured in thread. Closing. " + ex.Message);
                    handler = socket.Accept();
                    handler.Receive(header, 2, SocketFlags.None);
                }

                MsgTyp type = GetMsgType(header, ref length);

                messageBody = new byte[length - 2];
                try
                {
                    handler.Receive(messageBody);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception occured in thread. Closing. " + ex.Message);
                    return;
                }

                // Create the full message
                byte[] messageFull = new byte[length];
                System.Buffer.BlockCopy(header, 0, messageFull, 0, 2);
                System.Buffer.BlockCopy(messageBody, 0, messageFull, 2, length - 2);

                switch (type)
                {
                case MsgTyp.Connect:

                    ConnackWrk cObj = ProcessConnect(messageFull, ref clientId);
                    handler.Send(cObj.Serialized, 0, cObj.connack.length, SocketFlags.None);
                    break;

                case MsgTyp.Register:

                    RegackWrk rObj = ProcessRegister(messageFull);
                    handler.Send(rObj.Serialized, 0, rObj.regack.length, SocketFlags.None);
                    break;


                case MsgTyp.Publish:


                    PubackWrk pObj = ProcessPublish(messageFull);
                    handler.Send(pObj.Serialized, 0, pObj.puback.length, SocketFlags.None);
                    break;

                case MsgTyp.Subscribe:


                    SubackWrk skObj = ProcessSubscribe(messageFull, clientId);
                    handler.Send(skObj.Serialized, 0, skObj.suback.length, SocketFlags.None);
                    break;

                case MsgTyp.Puback:

                    PubackWrk pkObj = ProcessPuback(messageFull, clientId, sqliteConnection);
                    handler.Send(pkObj.Serialized, 0, pkObj.puback.length, SocketFlags.None);
                    break;
                }


                // Send out message to be published to this client
                PublishClients(clientId, handler);
            }
        }