예제 #1
0
        iMsg Data_Command(object sender, cMsg Args)
        {
            iMsg             sReturn    = null;
            WebsocketHandler Connection = (WebsocketHandler)sender;

            if (Received == null)
            {
                return(null);
            }
            if (Connection == null)
            {
                return(null);
            }

            try
            {
                sReturn = Received(Connection, Connection.Peer, Args);
            }
            catch (Exception ex)
            {
                string sLine = "Server.Err: " + ex.Message;

                Write(sLine);

                sReturn = new sMsg("error", sLine);
            }

            return(sReturn);
        }
예제 #2
0
        private void Data_Command(object sender, cMsg Args)
        {
            iMsg             sReturn    = null;
            WebsocketHandler Connection = (WebsocketHandler)sender;

            if (Command == null)
            {
                return;
            }
            if (Connection == null)
            {
                return;
            }

            try
            {
                sReturn = Command(Connection, Args);
            }
            catch (Exception ex)
            {
                sReturn = new sMsg("error", ex.Message);
            }

            if (Connection != null)
            {
                Connection.Send(sReturn);
            }
        }
예제 #3
0
        private iMsg ProcessLogin(Connection Ctx, Peer Peer, cMsg Command)
        {
            LoginInfo Params = Command.Read <LoginInfo>();

            if (Server.Local)
            {
                Params.key = "local";
            }

            if ((Params.key == null) || (Params.key.Length < 1))
            {
                return(lJson("Invalid key"));
            }
            if ((Params.serial == null) || (Params.serial.Length < 1))
            {
                return(lJson("Invalid serial"));
            }
            if ((Params.key == "local") && !Server.Local)
            {
                return(lJson("Not local"));
            }

            string cKey = Params.key.Trim();

            List <Connection> MatchList = GetMatches(Ctx);

            if (MatchList == null || MatchList.Count == 0)
            {
                MatchList = (List <Connection>)Server.Get(cKey, true);
                if (MatchList.Count > 0)
                {
                    this.Match(MatchList[MatchList.Count - 1], Ctx);
                }
            }

            if (!Server.Contains(Ctx))
            {
                Ctx.Key    = cKey;
                Ctx.Device = false;

                if (Server.Add(Ctx))
                {
                    RegisterLogin(Ctx);
                }
            }

            if (MatchList.Count == 0)
            {
                if (!Server.Local || !StartDevice(Params, Ctx))
                {
                    return(new sMsg("wait", "Recorder is offline"));
                }
            }

            Mirror(Ctx, MatchList, Command.Json);

            return(new sMsg(Command.type, "Succesfull"));
        }
예제 #4
0
        private iMsg ProcessRegister(Connection Ctx, Peer Peer, cMsg Command)
        {
            RegisterInfo Params = Command.Read <RegisterInfo>();

            if (Server.Local)
            {
                Params.key = "local";
            }

            if ((Params.key == null) || (Params.key.Length < 1))
            {
                return(lJson("Invalid key"));
            }
            if ((Params.serial == null) || (Params.serial.Length < 1))
            {
                return(lJson("Invalid serial"));
            }

            if (Params.key == "local")
            {
                if (!Server.Local)
                {
                    return(lJson("Not local"));
                }
            }

            string cKey      = Params.key.Trim();
            var    MatchList = GetMatches(Ctx);

            if (MatchList.Count == 0)
            {
                MatchList = (List <Connection>)Server.Get(cKey, false);

                foreach (Connection Match in MatchList)
                {
                    this.Match(Match, Ctx);
                }
            }

            Ctx.Key    = cKey;
            Ctx.Device = true;

            if (Server.Add(Ctx))
            {
                RegisterLogin(Ctx);
            }

            Mirror(Ctx, MatchList, Command.Json);

            return(new sMsg(Command.type, "Succesfull"));
        }
예제 #5
0
        //public bool Append(Peer Peer, BufferData Buffer)
        //{
        //    return Append(Peer.xCon, Buffer);
        //}

        public bool Append(WebsocketHandler wHandler, RawSocket SourceSocket, BufferData Buffer)
        {
            if (wHandler == null)
            {
                return(false);
            }

            if (!handshakeComplete)
            {
                if (wHandler.Socket == null)
                {
                    wHandler.Socket = SourceSocket;
                }

                string sNew = Utils.FromLatin(Buffer.Data, 0, Buffer.Data.Length);

                dataString.Append(sNew);

                if (sNew.EndsWith("\r\n"))
                {
                    string sData = dataString.ToString();

                    if (sData.EndsWith("\r\n\r\n"))
                    {
                        if (Header != null)
                        {
                            Header(this, new TextArgs(sData));
                        }

                        dataString = new StringBuilder();
                    }
                }

                return(true);
            }

            if (dataString.Length == 0)
            {
                ushort dummy = 0;
                dataLength = Hybi.HybiLength(Buffer.Data, ref dummy);

                if (dataLength == 0)
                {
                    return(false);
                }
            }

            ushort left = 0;

            byte[] newData = null;

            int end = Math.Min(Buffer.Data.Length, dataLength - dataString.Length);

            dataString.Append(Utils.FromLatin(Buffer.Data, 0, end));

            if (dataString.Length != dataLength)
            {
                return(true);
            }

            if (end < Buffer.Data.Length)
            {
                left = (ushort)(Buffer.Data.Length - end);
            }

            byte[] bData = Utils.ToLatin(dataString.ToString());
            string sRet  = Hybi.HybiDecode(bData, ref Mask);

            if (sRet.Length == 0)
            {
                return(false);
            }

            dataString = new StringBuilder();

            if (left > 0)
            {
                newData = new byte[left];
                Array.Copy(Buffer.Data, end, newData, 0, left);
            }

            if ((sRet.Substring(0, 1) != "{") || (sRet.Substring(sRet.Length - 1) != "}"))
            {
                Utils.Error("Invalid JSON: " + sRet);
                return(false);
            }

            if (Command != null)
            {
                cMsg Cmd = null;

                try
                {
                    Cmd = new cMsg(sRet);
                }
                catch (Exception ex)
                {
                    Utils.Error("Invalid JSON: " + ex.Message);
                    return(false);
                }

                if (Command != null)
                {
                    Command(wHandler, Cmd);
                }
            }

            if (newData != null)
            {
                return(Append(wHandler, SourceSocket, new BufferData(newData, newData.Length)));
            }

            return(true);
        }
예제 #6
0
        private iMsg Process(object sender, Peer Peer, cMsg Command)
        {
            List <Connection> MatchList = null;
            Connection        Ctx       = (Connection)sender;

            switch (Command.type)
            {
            case "login":
            {
                return(ProcessLogin(Ctx, null, Command));
            }

            case "register":
            {
                return(ProcessRegister(Ctx, null, Command));
            }

            case "logout":
            {
                Logout(Ctx, Command.Text, false);
                break;
            }

            case "error":
            {
                WriteLine(this, new TextArgs("Received error: " + Command.Text));

                break;
            }

            default:
            {
                if (!Server.Contains(Ctx))
                {
                    return(lJson("Not logged in"));
                }

                MatchList = GetMatches(Ctx);
                if (MatchList.Count > 0)
                {
                    break;
                }

                if (Ctx.Device)
                {
                    Logout(Ctx, "Client not available", false);
                }
                else
                {
                    Logout(Ctx, "Device not available", false);
                }

                return(null);
            }
            }

            //WriteLine("Data: " + Command.Json);

            MatchList = GetMatches(Ctx);

            Mirror(Ctx, MatchList, Command.Json);
            return(null);
        }