Exemplo n.º 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);
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 3
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);
        }