Ejemplo n.º 1
0
        private bool digestIncomingMessage(byte[] _buffer)
        {
            if (_buffer.Length == 0)
            {
                //the client has disconnected from the server
                return(false);
            }

            //message has successfully been received
            try
            {
                for (int i = 0; i < _buffer.Length; i++)
                {
                    buffer.Add(_buffer[i]);
                }
                if (draft == null)
                {
                    foreach (Draft d in server.Drafts)
                    {
                        try
                        {
                            header = d.ParseClientRequestHandshake(buffer);
                            draft  = d;

                            if (header.URL == "/")
                            {
                                application = server;
                            }
                            else
                            {
                                //Extracting application's name
                                Regex  regex   = new Regex("/?([^/\\?\\*\\+\\\\]+)[/\\?]?.*");
                                Match  mtch    = regex.Match(header.URL);
                                string appName = mtch.Groups[1].Value;

                                application = ((IServer)server).GetApplication(appName);
                                if (application == null)
                                {
                                    ((ILogger)server).error("Invalid application: " + appName + ", URL: " + header.URL);
                                    sendHttpResponse(404);
                                    ((IConnection)this).Close();
                                    return(false);
                                }
                            }

                            byte[] b = d.CreateServerResponseHandshake(header);
                            socketStream.Write(b, 0, b.Length);
                            socketStream.Flush();
#if LOGGER
                            ((ILogger)server).log("Handshake was sent to:" + ((IConnection)this).IP.ToString());
#endif
                            connected = true;
                            application.AddConnection(this);
//							server.AddConnection(this);

                            break;
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                if (draft != null)
                {
                    Frame f;
                    while ((f = draft.ParseClientFrameBytes(buffer)) != null)
                    {
                        f.Connection = this;
                        if (f.OpCode == Frame.OpCodeType.Close)
                        {
                            ((IConnection)this).Close();
                            break;
                        }
                        else if (f.OpCode == Frame.OpCodeType.Ping)
                        {
                            ((IConnection)this).Send(new Frame(Frame.OpCodeType.Pong));
                        }
                        else if (f.OpCode == Frame.OpCodeType.Pong)
                        {
                            server.OnPonged(this);
                        }

                        if (application != null)
                        {
                            application.EnqueueIncomingFrame(f);
                        }
                    }
                }
            }
            catch
            {
            }

            return(true);
        }