示例#1
0
    void TCPServer::mapConnection(TCPConnectionPtr const & connPtr)
    {
    	std::string connStr = createConnectionStr(connPtr->remoteIp(), connPtr->remotePort());

		#if (((STREAMS_BOOST_VERSION / 100) % 1000) < 53)
			streams_boost::mutex::scoped_lock scoped_lock(mutex_);
		#else
			streams_boost::unique_lock<streams_boost::mutex> scoped_lock(mutex_);
		#endif

		connMap_[connStr] = connPtr;
    }
示例#2
0
    void TCPServer::handleWrite(SPL::blob & raw, std::string const & ipAddress, uint32_t port)
    {
//    	std::string connStr = broadcastResponse_ ? "" : createConnectionStr(ipAddress, port);

//		std::cerr << "writing to: " << connStr << std::endl;

//		TCPConnectionWeakPtr connWeakPtr;
//		bool connExist = false;

		TCPConnectionWeakPtrMap::iterator iter = broadcastResponse_ ? findFirstConnection() : findConnection(createConnectionStr(ipAddress, port));

    	if(iter == connMap_.end()) {
			if(!broadcastResponse_) errorHandler_.handleError(streams_boost::system::error_code(streams_boost::asio::error::connection_aborted), ipAddress, port);
    	}

		else {
			AsyncDataItemPtr asyncDataItemPtr = streams_boost::make_shared<AsyncDataItem>(errorHandler_);
			asyncDataItemPtr->setData<Format>(raw);

    		do {
				TCPConnectionWeakPtr connWeakPtr = iter->second;
				TCPConnectionPtr connPtr = connWeakPtr.lock();

				/// Validate existing connection
				if (connPtr && connPtr->socket().is_open()) {

					uint32_t * numOutstandingWritesPtr = connPtr->getNumOutstandingWritesPtr();

					/// Check if client consumes data from a socket
					if (*numOutstandingWritesPtr <= maxUnreadResponseCount_) {
						__sync_fetch_and_add(numOutstandingWritesPtr, 1);

						if(Format == mcts::block) {
							async_write(connPtr->socket(), asyncDataItemPtr->getBuffers(),
									connPtr->strand().wrap( streams_boost::bind(&AsyncDataItem::handleError, asyncDataItemPtr,
															streams_boost::asio::placeholders::error,
															connPtr->remoteIp(), connPtr->remotePort(), connWeakPtr)
									)
							);
						}
						else {
							async_write(connPtr->socket(), asyncDataItemPtr->getBuffer(),
									connPtr->strand().wrap( streams_boost::bind(&AsyncDataItem::handleError, asyncDataItemPtr,
															streams_boost::asio::placeholders::error,
															connPtr->remoteIp(), connPtr->remotePort(), connWeakPtr)
									)
							);
						}

						iter++;
					}
					else {
						connPtr->shutdown_conn(makeConnReadOnly_);
						if(makeConnReadOnly_) {
							iter++;
						}
						else {
							iter = unmapConnection(iter);
						}

						errorHandler_.handleError(streams_boost::system::error_code(streams_boost::asio::error::would_block), ipAddress, port, connWeakPtr);
					}
				}
				else {
					iter = unmapConnection(iter);
				}
			} while (broadcastResponse_ && (iter != connMap_.end()));
    	}

    }