Пример #1
0
	bool UTPSocket::connectTo(const net::Address& addr)
	{
		if (!bt::Globals::instance().isUTPEnabled())
			return false;

		UTPServer & srv = bt::Globals::instance().getUTPServer();
		reset();

		conn = srv.connectTo(addr);
		Connection::Ptr ptr = conn.toStrongRef();
		if (!ptr)
			return false;

		m_state = CONNECTING;
		ptr->setBlocking(blocking);
		if (blocking)
		{
			bool ret = ptr->waitUntilConnected();
			if (ret)
				m_state = CONNECTED;

			return ret;
		}

		return ptr->connectionState() == CS_CONNECTED;
	}
Пример #2
0
	bool UTPSocket::ready(const net::Poll* p, net::Poll::Mode mode) const
	{
		Q_UNUSED(p);
		Connection::Ptr ptr = conn.toStrongRef();
		if (!ptr)
			return false;

		if (mode == net::Poll::OUTPUT)
		{
			if (polled_for_writing)
			{
				polled_for_writing = false;
				return ptr->isWriteable();
			}
		}
		else
		{
			if (polled_for_reading)
			{
				polled_for_reading = false;
				return bytesAvailable() > 0 || ptr->connectionState() == CS_CLOSED;
			}
		}

		return false;
	}
Пример #3
0
	void UTPSocket::setBlocking(bool on)
	{
		blocking = on;
		Connection::Ptr ptr = conn.toStrongRef();
		if (ptr)
			ptr->setBlocking(on);
	}
Пример #4
0
	bt::Uint32 UTPSocket::bytesAvailable() const
	{
		Connection::Ptr ptr = conn.toStrongRef();
		if (ptr)
			return ptr->bytesAvailable();
		else
			return 0;
	}
Пример #5
0
	void UTPServer::Private::reset(const utp::Header* hdr)
	{
		Connection::Ptr c = find(hdr->connection_id);
		if (c)
		{
			c->reset();
		}
	}
Пример #6
0
int RaftConsensus::onSendAppendEntries(Node& node, msg_appendentries_t& data)
{
  Connection::Ptr pConnection = findOrCreateConnection(node);
  if (pConnection)
  {
    return pConnection->onSendAppendEntries(data);
  }
  return -1;
}
Пример #7
0
void Network::addConnection(Connection::Ptr conn)
{
  DEBUG("Adding a file descriptor %d", conn->getFD());
  connections[conn->getFD()] = conn;
  FD_SET(conn->getFD(), &master_set);
  if (max_fd < conn->getFD()) {
    max_fd = conn->getFD();
  }
}
Пример #8
0
int RaftConsensus::onSendRequestVote(Node& node, msg_requestvote_t& data)
{
  Connection::Ptr pConnection = findOrCreateConnection(node);
  if (pConnection)
  {
    return pConnection->onSendRequestVote(data);
  }
  return -1;
}
Пример #9
0
void RaftConsensus::removeConnection(int id)
{
  Connection::Ptr pConnection;
  pConnection = findConnection(id);
  if (!pConnection)
  {
    pConnection->shutdown();
    OSS::mutex_critic_sec_lock lock(_connectionMutex);
    _connections.erase(id);
  }
}
Пример #10
0
int RaftConsensus::onReceivedRequestVote(const Connection::Ptr& pConnection, msg_requestvote_t& data)
{
  OSS::mutex_lock lock(_raftMutex);
  int ret = 0;
  msg_requestvote_response_t response;
  ret = raft_recv_requestvote(_raft, pConnection->getNode().node(), &data, &response);
  if (ret != 0)
  {
    return ret;
  }
  return pConnection->onSendRequestVoteResponse(response);
}
Пример #11
0
	bool UTPSocket::connectSuccesFull()
	{
		Connection::Ptr ptr = conn.toStrongRef();
		if (ptr && ptr->connectionState() == CS_CONNECTED)
		{
			setRemoteAddress(ptr->remoteAddress());
			m_state = CONNECTED;
			return true;
		}
		else
			return false;
	}
Пример #12
0
int RaftConsensus::onReceivedAppendEntries(const Connection::Ptr& pConnection, msg_appendentries_t& data)
{
  OSS::mutex_lock lock(_raftMutex);
  
  int ret = 0;
  msg_appendentries_response_t response;
  ret = raft_recv_appendentries(_raft, pConnection->getNode().node(), &data, &response);
  if (ret != 0)
  {
    return ret;
  }
  return pConnection->onSendAppendEntriesResponse(response);
}
Пример #13
0
	const net::Address& UTPSocket::getPeerName() const
	{
		Connection::Ptr ptr = conn.toStrongRef();
		if (remote_addr_override)
			return addr;
		else if (ptr)
			return ptr->remoteAddress();
		else
		{
			static net::Address null;
			return null;
		}
	}
Пример #14
0
	void UTPSocket::prepare(net::Poll* p, net::Poll::Mode mode)
	{
		Connection::Ptr ptr = conn.toStrongRef();
		if (ptr && ptr->connectionState() != CS_CLOSED)
		{
			UTPServer & srv = bt::Globals::instance().getUTPServer();
			srv.preparePolling(p, mode, ptr);
			if (mode == net::Poll::OUTPUT)
				polled_for_writing = true;
			else
				polled_for_reading = true;
		}
	}
Пример #15
0
	UTPSocket::UTPSocket(Connection::WPtr conn)
			: net::SocketDevice(bt::UTP), 
			conn(conn), 
			blocking(true), 
			polled_for_reading(false), 
			polled_for_writing(false)
	{
		Connection::Ptr ptr = conn.toStrongRef();
		if (ptr)
		{
			setRemoteAddress(ptr->remoteAddress());
			ptr->setBlocking(blocking);
			m_state = CONNECTED;
		}
	}
Пример #16
0
	void UTPSocket::close()
	{
		Connection::Ptr ptr = conn.toStrongRef();
		if (ptr)
		{
			try
			{
				ptr->close();
			}
			catch (Connection::TransmissionError)
			{
				reset();
			}
		}
	}
Пример #17
0
	bool handleFrame(Connection::Ptr pConnection, Frame::Ptr pFrame)
	{
		if (pFrame->type() == Frame::FRAME_TYPE_EVSU || pFrame->type() == Frame::FRAME_TYPE_EVUN)
		{
			Poco::RemotingNG::ORB& orb = Poco::RemotingNG::ORB::instance();
			std::string suri(pFrame->payloadBegin(), pFrame->getPayloadSize());
			Poco::URI dispURI(suri);
			dispURI.setAuthority(_pListener->endPoint());
			dispURI.setFragment("");
			Poco::RemotingNG::EventDispatcher::Ptr pEventDispatcher = orb.findEventDispatcher(dispURI.toString(), Transport::PROTOCOL);
			if (pFrame->type() == Frame::FRAME_TYPE_EVSU)
			{
				Poco::Timestamp expire;
				expire += _pListener->getEventSubscriptionTimeout().totalMicroseconds();
				pEventDispatcher->subscribe(suri, suri, expire);
			}
			else
			{
				pEventDispatcher->unsubscribe(suri);
			}
			pConnection->returnFrame(pFrame);
			return true;
		}
		return false;
	}
Пример #18
0
	int UTPSocket::send(const bt::Uint8* buf, int len)
	{
		Connection::Ptr ptr = conn.toStrongRef();
		if (!ptr)
			return -1;

		try
		{
			return ptr->send(buf, len);
		}
		catch (Connection::TransmissionError & err)
		{
			close();
			return -1;
		}
	}
Пример #19
0
void Server::sendStateToConnection(Connection::Ptr const& connection)
{
  BOOST_FOREACH(Connection::Ptr const& conn, connectionPool_) {
    connection->send(Message<MessageType::updateReadiness>(
        {conn->id(), conn->ready()}
    ));
  }
Пример #20
0
void ServerConnection::run()
{
	if (_logger.debug()) _logger.debug("ServerConnection started.");
	Connection::Ptr pConnection = new Connection(socket(), Connection::MODE_SERVER);
	EventSubscriptionFrameHandler::Ptr pEventSubFrameHandler = new EventSubscriptionFrameHandler(_pListener);
	RequestFrameHandler::Ptr pRequestFrameHandler = new RequestFrameHandler(_pListener);
	pConnection->pushFrameHandler(pEventSubFrameHandler);
	pConnection->pushFrameHandler(pRequestFrameHandler);
	_pListener->connectionManager().registerConnection(pConnection);
	try
	{
		_pListener->connectionAccepted(pConnection);
	}
	catch (Poco::Exception& exc)
	{
		_logger.error("connectionAccepted event handler threw exception: " + exc.displayText());
	}
	catch (...)
	{
		_logger.error("connectionAccepted event handler threw unknown exception");
	}
	try
	{
		pConnection->run();
	}
	catch (Poco::Exception& exc)
	{
		_logger.log(exc);
	}
	_pListener->connectionManager().unregisterConnection(pConnection);
	pConnection->popFrameHandler(pRequestFrameHandler);
	pConnection->popFrameHandler(pEventSubFrameHandler);
	if (_logger.debug()) _logger.debug("ServerConnection done.");
}
Пример #21
0
	int UTPSocket::recv(bt::Uint8* buf, int max_len)
	{
		Connection::Ptr ptr = conn.toStrongRef();
		if (!ptr || ptr->connectionState() == CS_CLOSED)
			return 0;

		try
		{
			if (ptr->bytesAvailable() == 0)
			{
				if (blocking)
				{
					if (ptr->waitForData())
						return ptr->recv(buf, max_len);
					else
						return 0; // connection should be closed now
				}
				else
					return -1; // No data ready and not blocking so return -1
			}
			else
				return ptr->recv(buf, max_len);
		}
		catch (Connection::TransmissionError & err)
		{
			close();
			return -1;
		}
	}
Пример #22
0
	void accept()
	{
		auto self = shared_from_this();
		awaitingConnection = std::make_shared<Connection>(io);
		acceptor.async_accept(awaitingConnection->stream1(),
			[this, self] (const error_code &error) {
				if (error) {
					std::cerr << "accept: " << error << std::endl;
				} else {
					Connection::Ptr thisConnecion;
					std::swap(thisConnecion, awaitingConnection);
					accept();

					error_code localSocketError;
					thisConnecion->stream2().connect(socketPath, localSocketError);
					if (localSocketError) {
						std::cerr << "connecting local socket: " << localSocketError << std::endl;
						thisConnecion->stream1().close(localSocketError);
						return;
					}
					thisConnecion->start();
				}
			});
	}
Пример #23
0
void Network::masterLoop()
{
  struct timeval tv;
  fd_set cur_set;
  halt = false;
  while (!halt) {

    //sleep(1);
    bool netstat = active;

    while(!timers.empty() && (timers.top()->getExpireTime() <= static_cast<uint64_t>(time(NULL)) ||
          !(timers.top()->isValid()))){
      TimerCallback::Ptr callback = timers.top();
      timers.pop();
      if(callback->isValid())
        callback->call();
    }
    if(timers.empty()){
      tv.tv_sec = 60;
      tv.tv_usec = 0;
    }else{
      tv.tv_sec = (timers.top()->getExpireTime() - time(NULL)) - 1;
      if(tv.tv_sec <= 0){
        tv.tv_sec = 0;
        tv.tv_usec = 200000;
      }else{
        tv.tv_usec = 0;
      }
    }
    fd_set write_set;
    FD_ZERO(&write_set);
    // Unsure what the macro FD_SET stands for, so I can't do for_each
    for(ConnMap::iterator itcurr = writequeue.begin();
        itcurr != writequeue.end(); ++itcurr){
      FD_SET(itcurr->first, &write_set);
    }

    cur_set = master_set;

    if (select(max_fd + 1, &cur_set, &write_set, NULL, &tv) > 0) {

      for(ConnMap::iterator itcurr = writequeue.begin();
          itcurr != writequeue.end(); ++itcurr){
        if(FD_ISSET(itcurr->first, &write_set)){
          Connection::Ptr conn = itcurr->second;
          writequeue.erase(itcurr);
          conn->processWrite();
          //use select again, don't check rest of list as it has changed.
          break;
        }
      }

      ConnMap::iterator itcurr;
      for (itcurr = connections.begin(); itcurr != connections.end(); itcurr++) {
        Connection::Ptr connection = itcurr->second;
        if (FD_ISSET(itcurr->first, &cur_set)) {
          connection->process();
        }
        if (connection->getStatus() == Connection::DISCONNECTED) {
          INFO("Closed connection %d", connection->getFD());
          removeConnection(itcurr->first);
          //use select again, don't check rest of list as it has changed.
          break;
        }
      }

    }

    //advertiser->poll();

    if(netstat != active && active == false){
      for_each_key( connections.begin(), connections.end(), boost::bind( &Network::removeConnection, this, _1 ) );
      DEBUG("Network really stopped");
    }
  }
}
Пример #24
0
void Network::addToWriteQueue(Connection::Ptr conn){
  writequeue[conn->getFD()] = conn;
}
Пример #25
0
int main() {
	// Create our TCP listener socket.
	auto listener = sfn::TcpListener::Create();

	// Listen on 0.0.0.0:443
	listener->Listen( sfn::Endpoint{ sfn::IpAddress{ "0.0.0.0" }, 443 } );

	// Start 3 network processing threads.
	sfn::Start( 3 );

	// Just to make our lives easier...
	typedef sfn::TlsConnection<sfn::TcpSocket, sfn::TlsEndpointType::Server, sfn::TlsVerificationType::None> Connection;

	// A place to store all active connections.
	std::deque<Connection::Ptr> connections;

	const std::string certificate_string =
		"-----BEGIN CERTIFICATE-----\r\n"
		"MIIDPjCCAiagAwIBAgIBAjANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJGUjEO\r\n"
		"MAwGA1UEBxMFUGFyaXMxDjAMBgNVBAoTBVh5U1NMMRYwFAYDVQQDEw1YeVNTTCBU\r\n"
		"ZXN0IENBMB4XDTA3MDcwNzA1MDEyOVoXDTA4MDcwNjA1MDEyOVowMTELMAkGA1UE\r\n"
		"BhMCRlIxDjAMBgNVBAoTBVh5U1NMMRIwEAYDVQQDEwlsb2NhbGhvc3QwggEiMA0G\r\n"
		"CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC40PDcGTgmHkt6noXDfkjVuymjiNYB\r\n"
		"gjtiL7uA1Ke3tXStacEecQek/OJxYqYr7ffcWalS29LL6HbKpi0xLZKBbD9ACkDh\r\n"
		"1Z/SvHlyQPILJdYb9DMw+kzZds5myXUjzn7Aem1YjoxMZUAMyc34i2900X2pL0v2\r\n"
		"SfCeJ9Ym4MOnZxYl217+dX9ZbkgIgrT6uY2IYK4boDwxbTcyT8i/NPsVsiMwtWPM\r\n"
		"rnQMr+XbgS98sUzcZE70Pe1TlV9Iy8j/8d2OiFo+qTyMu/6UpM2s3gdkQkMzx+Sm\r\n"
		"4QitRUjzmEXeUePRUjEgHIv7vz069xuVBzrks36w5BXiVAhLke/OTKVPAgMBAAGj\r\n"
		"TTBLMAkGA1UdEwQCMAAwHQYDVR0OBBYEFNkOyCTx64SDdPySGWl/tzD7/WMSMB8G\r\n"
		"A1UdIwQYMBaAFLzuH5jo+iuD5KR9XsN1cpMx2TJnMA0GCSqGSIb3DQEBBQUAA4IB\r\n"
		"AQBelJv5t+suaqy5Lo5bjNeHjNZfgg8EigDQ7NqaosvlQZAsh2N34Gg5YdkGyVdg\r\n"
		"s32I/K5aaywyUbG9qVXQxCM2T95qBqyK56h9yJoZKWQD9H//+zB8kCK/16WvRfv3\r\n"
		"VA7eSR19qOFWlHe+1qGh2YhxeDUfyi+fm4D36dGxqC2A34tZjo0QPHKtIeqM0kJy\r\n"
		"zzL65TlbJQKkyTuRHofFv0jW9ZFG2wkGysVgCY5fjuLI1do/sWUaXd2987iNFa+K\r\n"
		"FrHsTi6urSfZuGlZNxDXDHEE7Q2snAvvev+KR7DD9X4DJGcPX9gA4CGJj+9ZzyAA\r\n"
		"ZTGpOzk1hIH44RFs2lJMZRlE\r\n"
		"-----END CERTIFICATE-----\r\n";

	const std::string key_string =
		"-----BEGIN PRIVATE KEY-----\r\n"
		"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC40PDcGTgmHkt6\r\n"
		"noXDfkjVuymjiNYBgjtiL7uA1Ke3tXStacEecQek/OJxYqYr7ffcWalS29LL6HbK\r\n"
		"pi0xLZKBbD9ACkDh1Z/SvHlyQPILJdYb9DMw+kzZds5myXUjzn7Aem1YjoxMZUAM\r\n"
		"yc34i2900X2pL0v2SfCeJ9Ym4MOnZxYl217+dX9ZbkgIgrT6uY2IYK4boDwxbTcy\r\n"
		"T8i/NPsVsiMwtWPMrnQMr+XbgS98sUzcZE70Pe1TlV9Iy8j/8d2OiFo+qTyMu/6U\r\n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\r\n"
		"IJTSDG7D+jWN1rYLFtnL0SZT96sRfT37Sf5M59ClIQ+r/P1ZrEAVj0t+x1nRmS2h\r\n"
		"4eZrVs10veLoDcNYAzdhJDNAxE+bM5aepfFyCgIGaW/OTNp4uM7mmEygtAcmaZp+\r\n"
		"+4Ibq7Gi/cXweLcvIdQXZzyTScvq5yYne+O7O7gBAoGAFIBIwAlLrcz1QkHURrPL\r\n"
		"a/Y1bDTgKbM3FR9ase4ql38LaKThAq7SxE6v0qeQODHESGMObuVeGVbXY9TjbI5G\r\n"
		"y3kjcRprUJ+Lgo8Wf+jO36I0FivN3xPK+duqR0QKXT4bYWj8RHg1EK/trbhY0Sw4\r\n"
		"wbMZGrDlydImuNtktgxojIo=\r\n"
		"-----END PRIVATE KEY-----\r\n";

	// Load the certificate and key
	auto certificate = sfn::TlsCertificate::Create( certificate_string );
	auto key = sfn::TlsKey::Create( key_string );

	std::atomic_bool exit{ false };
	std::cout << "Press ENTER to exit.\n";
	// Don't use sfn::Thread for your own projects, it is not what you think it is.
	sfn::Thread exit_handler( [&]() { std::cin.get(); exit = true; } );

	while( !exit ) {
		{
			Connection::Ptr connection;

			// Dequeue any connections from the listener.
			while( ( connection = listener->GetPendingConnection<Connection>() ) ) {
				// Set the server certificate and key pair.
				connection->SetCertificateKeyPair( certificate, key );

				// Turn of connection lingering.
				connection->SetLinger( 0 );

				char response[] =
					"HTTP/1.1 200 OK\r\n"
					"Server: SFNUL HTTPS Server\r\n"
					"Content-Type: text/html; charset=UTF-8\r\n"
					"Connection: close\r\n\r\n"
					"<html><head><title>SFNUL HTTPS Server Page</title></head>"
					"<body>SFNUL HTTPS Server Document</body></html>\r\n\r\n";

				// Send the HTTP response.
				connection->Send( response, sizeof( response ) - 1 );

				// Shutdown the connection for sending.
				connection->Shutdown();

				// Store the connection.
				connections.push_back( connection );
			}
		}

		for( auto connection_iter = connections.begin(); connection_iter != connections.end(); ) {
			// Remove and thereby close all active connections that have been
			// remotely shut down and don't have data left to send.
			if( ( *connection_iter )->RemoteHasShutdown() && !( *connection_iter )->BytesToSend() ) {
				//( *connection_iter )->Shutdown();
				connection_iter = connections.erase( connection_iter );
			}
			else {
				++connection_iter;
			}
		}
	}

	// Close the listener socket.
	listener->Close();

	// Stop all network processing threads.
	sfn::Stop();

	return 0;
}
Пример #26
0
int RaftConsensus::onReceivedAppendEntriesResponse(const Connection::Ptr& pConnection, msg_appendentries_response_t& data)
{
  OSS::mutex_lock lock(_raftMutex);
  return raft_recv_appendentries_response(_raft, pConnection->getNode().node(), &data);
}
Пример #27
0
int RaftConsensus::onReceivedRequestVoteResponse(const Connection::Ptr& pConnection, msg_requestvote_response_t& data)
{
  OSS::mutex_lock lock(_raftMutex);
  return raft_recv_requestvote_response(_raft, pConnection->getNode().node(), &data);
}
Пример #28
0
	bool UTPSocket::ok() const
	{
		Connection::Ptr ptr = conn.toStrongRef();
		return ptr && ptr->connectionState() != CS_CLOSED;
	}
Пример #29
0
void TargetReady::SetConnection(Connection::Ptr conn) {
    assert(conn_set_.find(conn) == conn_set_.end());
    conn_set_.insert(conn);
    conn->BindDelegate(this);
}