Esempio n. 1
0
        private void Raw_Received(object sender, BufferData buffer)
        {
            Connection Ctx     = null;
            RawSocket  RawSock = (RawSocket)sender;

            if (sender.GetType() == typeof(Peer))
            {
                Ctx = ((Peer)sender).Ctx;
            }
            else if (sender.GetType() == typeof(Connection))
            {
                Ctx = (Connection)sender;
            }
            else if (sender.GetType() == typeof(RawSocket))
            {
                Ctx = (Connection)((RawSocket)sender).CachePeer.Ctx;
            }

            if (Ctx != null)
            {
                try
                {
                    if (WebData.Append(Ctx, RawSock, buffer))
                    {
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Close("Raw_Received" + ex.Message);
                    return;
                }
            }

            Close("Connection closed");
            return;
        }
Esempio n. 2
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);
        }