mirror of
https://github.com/boostorg/asio.git
synced 2026-01-28 18:52:09 +00:00
........ r43377 | chris_kohlhoff | 2008-02-23 09:43:54 +1100 (Sat, 23 Feb 2008) | 2 lines Use the correct vector of timer queues when dispatching timers. ........ r43437 | chris_kohlhoff | 2008-02-29 23:57:57 +1100 (Fri, 29 Feb 2008) | 2 lines Add missing tie(). ........ r43469 | chris_kohlhoff | 2008-03-04 00:21:05 +1100 (Tue, 04 Mar 2008) | 4 lines Disable use of CancelIo by default, due to the possibility of silent failure on some system configurations. Swallow error returned by CancelIoEx if there are no operations to be cancelled. ........ r43470 | chris_kohlhoff | 2008-03-04 00:27:06 +1100 (Tue, 04 Mar 2008) | 2 lines Add missing 'boost_' prefix to helper namespace. ........ r43471 | chris_kohlhoff | 2008-03-04 00:36:35 +1100 (Tue, 04 Mar 2008) | 2 lines Regenerate documentation. ........ r43472 | chris_kohlhoff | 2008-03-04 01:05:35 +1100 (Tue, 04 Mar 2008) | 1 line Update copyright notices. ........ r43473 | chris_kohlhoff | 2008-03-04 01:13:01 +1100 (Tue, 04 Mar 2008) | 2 lines Update copyright notices. ........ r43569 | chris_kohlhoff | 2008-03-13 00:25:49 +1100 (Thu, 13 Mar 2008) | 4 lines Revert to having the windows-bug workaround (short timeout on GetQueuedCompletionStatus) on all threads as there are still scenarios where threads can get stuck indefinitely. ........ [SVN r43571]
188 lines
4.6 KiB
C++
188 lines
4.6 KiB
C++
//
|
|
// chat_client.cpp
|
|
// ~~~~~~~~~~~~~~~
|
|
//
|
|
// 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 <cstdlib>
|
|
#include <deque>
|
|
#include <iostream>
|
|
#include <boost/bind.hpp>
|
|
#include <boost/asio.hpp>
|
|
#include <boost/thread.hpp>
|
|
#include "chat_message.hpp"
|
|
|
|
using boost::asio::ip::tcp;
|
|
|
|
typedef std::deque<chat_message> chat_message_queue;
|
|
|
|
class chat_client
|
|
{
|
|
public:
|
|
chat_client(boost::asio::io_service& io_service,
|
|
tcp::resolver::iterator endpoint_iterator)
|
|
: io_service_(io_service),
|
|
socket_(io_service)
|
|
{
|
|
tcp::endpoint endpoint = *endpoint_iterator;
|
|
socket_.async_connect(endpoint,
|
|
boost::bind(&chat_client::handle_connect, this,
|
|
boost::asio::placeholders::error, ++endpoint_iterator));
|
|
}
|
|
|
|
void write(const chat_message& msg)
|
|
{
|
|
io_service_.post(boost::bind(&chat_client::do_write, this, msg));
|
|
}
|
|
|
|
void close()
|
|
{
|
|
io_service_.post(boost::bind(&chat_client::do_close, this));
|
|
}
|
|
|
|
private:
|
|
|
|
void handle_connect(const boost::system::error_code& error,
|
|
tcp::resolver::iterator endpoint_iterator)
|
|
{
|
|
if (!error)
|
|
{
|
|
boost::asio::async_read(socket_,
|
|
boost::asio::buffer(read_msg_.data(), chat_message::header_length),
|
|
boost::bind(&chat_client::handle_read_header, this,
|
|
boost::asio::placeholders::error));
|
|
}
|
|
else if (endpoint_iterator != tcp::resolver::iterator())
|
|
{
|
|
socket_.close();
|
|
tcp::endpoint endpoint = *endpoint_iterator;
|
|
socket_.async_connect(endpoint,
|
|
boost::bind(&chat_client::handle_connect, this,
|
|
boost::asio::placeholders::error, ++endpoint_iterator));
|
|
}
|
|
}
|
|
|
|
void handle_read_header(const boost::system::error_code& error)
|
|
{
|
|
if (!error && read_msg_.decode_header())
|
|
{
|
|
boost::asio::async_read(socket_,
|
|
boost::asio::buffer(read_msg_.body(), read_msg_.body_length()),
|
|
boost::bind(&chat_client::handle_read_body, this,
|
|
boost::asio::placeholders::error));
|
|
}
|
|
else
|
|
{
|
|
do_close();
|
|
}
|
|
}
|
|
|
|
void handle_read_body(const boost::system::error_code& error)
|
|
{
|
|
if (!error)
|
|
{
|
|
std::cout.write(read_msg_.body(), read_msg_.body_length());
|
|
std::cout << "\n";
|
|
boost::asio::async_read(socket_,
|
|
boost::asio::buffer(read_msg_.data(), chat_message::header_length),
|
|
boost::bind(&chat_client::handle_read_header, this,
|
|
boost::asio::placeholders::error));
|
|
}
|
|
else
|
|
{
|
|
do_close();
|
|
}
|
|
}
|
|
|
|
void do_write(chat_message msg)
|
|
{
|
|
bool write_in_progress = !write_msgs_.empty();
|
|
write_msgs_.push_back(msg);
|
|
if (!write_in_progress)
|
|
{
|
|
boost::asio::async_write(socket_,
|
|
boost::asio::buffer(write_msgs_.front().data(),
|
|
write_msgs_.front().length()),
|
|
boost::bind(&chat_client::handle_write, this,
|
|
boost::asio::placeholders::error));
|
|
}
|
|
}
|
|
|
|
void handle_write(const boost::system::error_code& error)
|
|
{
|
|
if (!error)
|
|
{
|
|
write_msgs_.pop_front();
|
|
if (!write_msgs_.empty())
|
|
{
|
|
boost::asio::async_write(socket_,
|
|
boost::asio::buffer(write_msgs_.front().data(),
|
|
write_msgs_.front().length()),
|
|
boost::bind(&chat_client::handle_write, this,
|
|
boost::asio::placeholders::error));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
do_close();
|
|
}
|
|
}
|
|
|
|
void do_close()
|
|
{
|
|
socket_.close();
|
|
}
|
|
|
|
private:
|
|
boost::asio::io_service& io_service_;
|
|
tcp::socket socket_;
|
|
chat_message read_msg_;
|
|
chat_message_queue write_msgs_;
|
|
};
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
try
|
|
{
|
|
if (argc != 3)
|
|
{
|
|
std::cerr << "Usage: chat_client <host> <port>\n";
|
|
return 1;
|
|
}
|
|
|
|
boost::asio::io_service io_service;
|
|
|
|
tcp::resolver resolver(io_service);
|
|
tcp::resolver::query query(argv[1], argv[2]);
|
|
tcp::resolver::iterator iterator = resolver.resolve(query);
|
|
|
|
chat_client c(io_service, iterator);
|
|
|
|
boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));
|
|
|
|
char line[chat_message::max_body_length + 1];
|
|
while (std::cin.getline(line, chat_message::max_body_length + 1))
|
|
{
|
|
using namespace std; // For strlen and memcpy.
|
|
chat_message msg;
|
|
msg.body_length(strlen(line));
|
|
memcpy(msg.body(), line, msg.body_length());
|
|
msg.encode_header();
|
|
c.write(msg);
|
|
}
|
|
|
|
c.close();
|
|
t.join();
|
|
}
|
|
catch (std::exception& e)
|
|
{
|
|
std::cerr << "Exception: " << e.what() << "\n";
|
|
}
|
|
|
|
return 0;
|
|
}
|