Source

tinymq / src / connection.cpp

/*
 Copyright (c) 2009 Andy Gross <andy@andygross.org>

 This file is provided to you under the Apache License,
 Version 2.0 (the "License"); you may not use this file
 except in compliance with the License.  You may obtain
 a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.

 Based on example code from the boost::asio package which is:

 Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)

 Distributed under the Boost Software License, Version 1.0. (See accompanying
 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/

#include <vector>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/asio/local/stream_protocol.hpp>
#include <boost/asio/local/connect_pair.hpp>
#include <boost/asio/buffer.hpp>
#include "connection.hpp"
#include "server.hpp"

namespace asio = boost::asio;

namespace tinymq {

connection::connection(asio::io_service& io_service, server *server)
  : io_service_(io_service),
    socket_(io_service),
    server_(server),
    client_(new tcp_client(&server->queue(), io_service)){}

connection::~connection() {}

asio::ip::tcp::socket& connection::socket() {
    return socket_;
}

void connection::read_next_command() {
    asio::async_read(socket_, asio::buffer(buffer_, command_length),
        boost::bind(&connection::handle_read_command, shared_from_this(),
          asio::placeholders::error,
          asio::placeholders::bytes_transferred));
}

void connection::wait() {
    client_->wait();
    asio::async_read(client_->waker_sock(), asio::buffer(buffer_, command_length),
        boost::bind(&connection::handle_resume, shared_from_this(),
                asio::placeholders::error,
                asio::placeholders::bytes_transferred));
}

void connection::start() {
    std::string client_ip = socket_.remote_endpoint().address().to_string();
    std::string client_port = boost::lexical_cast<std::string>(
            socket_.remote_endpoint().port());
    read_next_command();
}

void connection::cmd_get() {
    message *m = client_->dequeue_message();
    if (!m) wait();
    else {
        message_id mid = m->id();
        std::size_t msize = m->data()->size();
        std::vector<char> data(m->data()->begin(), m->data()->end());
        boost::array<boost::asio::const_buffer, 3> bufs = {
                asio::buffer((void*)&mid, sizeof(message_id)),
            asio::buffer((void*)&msize, sizeof(uint32_t)),
            asio::buffer(data)
        };
        asio::async_write(socket_, bufs,
                boost::bind(&connection::handle_write, shared_from_this(),
                        asio::placeholders::error,
                        asio::placeholders::bytes_transferred));
    }
}

void connection::cmd_ack(message_id id) {
    client_->ack_message(id);
    read_next_command();
}

void connection::handle_read_command(const boost::system::error_code& e,
    std::size_t bytes_transferred) {
    if (!e) {
        asio::const_buffer b = asio::buffer(buffer_);
        const command *command_type = asio::buffer_cast<const command *>(b);
        if (*command_type == get_msg)  {
            cmd_get();
        }
        else if (*command_type == ack_msg) {
            asio::async_read(socket_, asio::buffer(buffer_, sizeof(message_id)),
                 bind(&connection::handle_read_message_id, shared_from_this(),
                   asio::placeholders::error,
                   asio::placeholders::bytes_transferred));
        }
        else if (*command_type == put_msg) {
            asio::async_read(socket_, asio::buffer(buffer_, sizeof(uint32_t)),
                 boost::bind(&connection::handle_read_length, shared_from_this(),
                   asio::placeholders::error,
                   asio::placeholders::bytes_transferred));
        }
        else {
            printf("ERROR: unknown message type %d, closing connection\n", *command_type);
            socket_.shutdown(
                    asio::ip::tcp::socket::shutdown_both);
        }
    }
}

void connection::handle_resume(const boost::system::error_code& e,
    std::size_t bytes_transferred) {
    if (!e)
        cmd_get();
}

void connection::handle_read_message_id(const boost::system::error_code& e,
        std::size_t bytes_transferred) {
    if (!e) {
        asio::const_buffer b = asio::buffer(buffer_);
        const message_id *id = asio::buffer_cast<const message_id*>(b);
        cmd_ack(*id);
    }
    else {
        printf("handle_read_message_id error %s\n", e.message().c_str());
    }
}

void connection::handle_read_length(const boost::system::error_code& e,
        std::size_t bytes_transferred) {
    if (!e) {
        asio::const_buffer b = asio::buffer(buffer_);
        const std::size_t *msg_size = asio::buffer_cast<const std::size_t*>(b);
        incoming_.reserve(*msg_size);
        incoming_.insert(incoming_.begin(), *msg_size, 0);
        asio::async_read(socket_, asio::buffer(incoming_),
             boost::bind(&connection::handle_read_message, shared_from_this(),
               asio::placeholders::error,
               asio::placeholders::bytes_transferred));

    }
    else {
        printf("handle_read_length error %s\n", e.message().c_str());
    }
}

void connection::handle_read_message(const boost::system::error_code& e,
    std::size_t bytes_transferred) {
    if (!e) {
        asio::const_buffer b = asio::buffer(incoming_);
        const char *data = asio::buffer_cast<const char*>(b);
        message_id mid = client_->enqueue_message((unsigned char *)data, incoming_.size());
        incoming_.resize(0);
        asio::async_write(socket_, asio::buffer((void*)&mid, sizeof(message_id)),
                 boost::bind(&connection::handle_write, shared_from_this(),
                         asio::placeholders::error,
                         asio::placeholders::bytes_transferred));

    }
    else {
        printf("handle_read_message error %s\n", e.message().c_str());
    }
}

void connection::handle_write(const boost::system::error_code& e,
            std::size_t bytes_transferred) {
    if (!e)
        read_next_command();
    else
        printf("handle_write error %s\n", e.message().c_str());
}

} // namespace tinymq
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.