Beispiel #1
0
	/// <summary>
	/// Handle_accepts the specified error.
	/// </summary>
	/// <param name="error">The error.</param>
	/// <param name="conn">The conn.</param>
	void TcpServer::handle_accept( const boost::system::error_code& error, const TcpConnectionPtr conn)
	{
		if (!error)	/// ´¦Àí¼àÌýʼþ
		{
			conn->SetMessageCallBack(messageCallBack_);
			conn->SetErrorCallBack(errorCallBack_);
			conn->SetWriteCompleteCallBack(writecompleteCallBack_);
			if (acceptedCallBack_)
			{
				acceptedCallBack_(conn);
			}

			boost::weak_ptr<WheelEntry<TcpConnection> > weak_ptr = timing_wheel_.Register(conn, 
				TimeOutCallBackT<TcpConnection>(boost::bind(&TcpServer::handle_timeout, this, _1)));

			conn->SetContent(boost::any(weak_ptr));

			conn->Start();
			TcpConnectionPtr tcpconnPtr(new TcpConnection(ioservicepool_.get_io_service(), messageCallBack_, &timing_wheel_));
			acceptor_.async_accept(tcpconnPtr->socket(),
				boost::bind(&TcpServer::handle_accept, this,
				boost::asio::placeholders::error,tcpconnPtr));
		}
		else
		{
			if (errorCallBack_)
			{
				errorCallBack_(conn, error);
			}
		}

	}
Beispiel #2
0
void Channel::handleEvents() {
	LOG_INFO << "handle event "
	         << sockfd_
	         << " events "
	         << EventToString(r_events_);

	if ( (r_events_ & EPOLLRDHUP ) && (r_events_ & EPOLLIN) ) {
		LOG_INFO << "handle close" ;
		if (closeCallBack_) {
			closeCallBack_();
			return;
		}
	}

	if ( r_events_ & EPOLLIN  ) {
		LOG_INFO << "handle read" ;
		if (readCallBack_) {
			readCallBack_();
			return;
		}
	}

	if (r_events_ & EPOLLOUT ) {
		LOG_INFO << "handle write" ;
		if (writeCallBack_) writeCallBack_();
	}

	if (r_events_ & EPOLLERR) {
		LOG_INFO << "handle error" ;
		if (errorCallBack_) errorCallBack_();
	}
};
Beispiel #3
0
	/// <summary>
	/// Handle writes the data with error code.
	/// </summary>
	/// <param name="e">The error code.</param>
	/// <param name="pSendMsg">The point of MSG sent.</param>
	void TcpConnection::handle_write(const boost::system::error_code& e, std::string* pSendMsg)
	{
		if (!e)
		{
			bool callback = false;
			{
				boost::lock_guard<boost::mutex> lock(sendMutex_);
				callback = sendCompleteCallBackList_.front();
			}
			if ( callback && writecompleteCallBack_ )
			{
				writecompleteCallBack_(shared_from_this());
			}
		}
		else
		{
			// TODO
			if (errorCallBack_)
			{
				errorCallBack_(shared_from_this(),e);
			}
		}

		boost::lock_guard<boost::mutex> lock(sendMutex_);
		sendList_.pop_front();
		sendCompleteCallBackList_.pop_front();
		bool bSendListEmpty = sendList_.empty();

		if ( !bSendListEmpty )
		{
			std::string* pNextSendMsg = sendList_.front();
			boost::asio::async_write(socket_,
				boost::asio::buffer(pNextSendMsg->c_str(),pNextSendMsg->length()),
				boost::bind(&TcpConnection::handle_write, shared_from_this(),
				boost::asio::placeholders::error,pNextSendMsg));
		}

		if (pSendMsg)
		{
			delete pSendMsg;
			pSendMsg = NULL;
		}
	}
Beispiel #4
0
	/// <summary>
	/// Handle reads the data with error code.
	/// </summary>
	/// <param name="e">The error code.</param>
	/// <param name="bytes_transferred">The bytes_transferred.</param>
	void TcpConnection::handle_read(const boost::system::error_code& e,
		std::size_t bytes_transferred)
	{
		if (!e)
		{
			if(bytes_transferred > 0)
			{
				receiveMsgbuffer_.append(readBuffer_.data(),bytes_transferred);
				boost::posix_time::ptime  receiveTime = boost::posix_time::microsec_clock::universal_time();
				bool receAgain = true;
				if (messageCallBack_)
				{
					receAgain = messageCallBack_(shared_from_this(),boost::ref(receiveMsgbuffer_),receiveTime);
				}

				if ( receAgain && socket_.is_open())
				{
					socket_.async_read_some(boost::asio::buffer(readBuffer_),
						boost::bind(&TcpConnection::handle_read, shared_from_this(),
						boost::asio::placeholders::error,
						boost::asio::placeholders::bytes_transferred));
				}

				boost::weak_ptr<WheelEntry<TcpConnection> > weak_ptr(boost::any_cast<boost::weak_ptr<WheelEntry<TcpConnection> > >(any_));

				if(p_timing_wheel_)
				{
					p_timing_wheel_->Active(weak_ptr);
				}
			}
		}
		else
		{
			// TODO
			if (errorCallBack_)
			{
				errorCallBack_(shared_from_this(),e);
			}
			Stop();
		}
	}