Пример #1
0
        public void request(HttpRequest req, HttpResponse res)
        {
            if (!m_connected)
            {
                connect();
            }

            try
            {
                std::iostream stream = new std::iostream(m_streamBuf.get());
                HttpParser    parser = new HttpParser();
                stream << req;
                stream.flush();
                parser.receiveResponse(stream, res);
            }
            catch (System.Exception)
            {
                disconnect();
                throw;
            }
        }
Пример #2
0
        private void acceptLoop()
        {
            try
            {
                System.TcpConnection connection = new System.TcpConnection();
                bool accepted = false;

                while (!accepted)
                {
                    try
                    {
                        connection = m_listener.accept();
                        accepted   = true;
                    }
                    catch (System.InterruptedException)
                    {
                        throw;
                    }
                    catch (System.Exception)
                    {
                        // try again
                    }
                }

                m_connections.Add(connection);
                BOOST_SCOPE_EXIT_ALL(this, connection)
                {
                    m_connections.erase(connection);
                };

                workingContextGroup.spawn(std::bind(acceptLoop, this));

                var addr = connection.getPeerAddressAndPort();

                logger(DEBUGGING) << "Incoming connection from " << addr.first.toDottedDecimal() << ":" << addr.second;

                System.TcpStreambuf streambuf = new System.TcpStreambuf(connection);
                std::iostream       stream    = new std::iostream(streambuf);
                HttpParser          parser    = new HttpParser();

                for (;;)
                {
                    HttpRequest  req  = new HttpRequest();
                    HttpResponse resp = new HttpResponse();

                    parser.receiveRequest(stream, req);
                    processRequest(req, resp);

                    stream << resp;
                    stream.flush();

                    if (stream.peek() == std::iostream.traits_type.eof())
                    {
                        break;
                    }
                }

                logger(DEBUGGING) << "Closing connection from " << addr.first.toDottedDecimal() << ":" << addr.second << " total=" << m_connections.Count;
            }
            catch (System.InterruptedException)
            {
            }
            catch (System.Exception e)
            {
                logger(WARNING) << "Connection error: " << e.Message;
            }
        }