PortID DelegateService::unusedODPPort(const SpaceObjectReference& sor) {
    // We want to make this efficient in most cases, but need to make it
    // exhaustively search for available ports when we can't easily find a free
    // one. The approach is simple: choose a random port to start with and check
    // if it's free. From there, perform a linear search (which must wrap and
    // avoid reserved ports). This won't perform well in extreme cases where we
    // the port space is almost completely full...

    PortID starting_port_id = (rand() % (OBJECT_PORT_SYSTEM_MAX-OBJECT_PORT_SYSTEM_RESERVED_MAX)) + (OBJECT_PORT_SYSTEM_RESERVED_MAX+1);

    // If we don't have a PortMap yet, then its definitely not allocated
    PortMap* pm = getPortMap(sor);
    if (pm == NULL) return starting_port_id;

    // Loop until we hit the starting point again
    PortID port_id = starting_port_id;
    do {
        // This port may be allocated already
        PortMap::iterator it = pm->find(port_id);
        if (it == pm->end())
            return port_id;

        // Otherwise we need to move on, ensuring looping of Port IDs.
        port_id++;
        if (port_id > PortID(OBJECT_PORT_SYSTEM_MAX))
            port_id = OBJECT_PORT_SYSTEM_RESERVED_MAX+1;
    } while(port_id != starting_port_id);

    // If we get here, we're really out of ports
    SILOG(odp-delegate-service, error, "Exhausted ODP ports for " << sor);
    return PortID::null();
}
void DelegateService::deallocatePort(DelegatePort* port) {
    PortMap* pm = getPortMap(port->endpoint().spaceObject());
    if (pm == NULL)
        return;

    pm->erase(port->endpoint().port());
}
Port* DelegateService::bindODPPort(const SpaceObjectReference& sor, PortID port) {
    PortMap* pm = getOrCreatePortMap(sor);

    PortMap::iterator it = pm->find(port);
    if (it != pm->end()) {
        // Already allocated
        return NULL;
    }

    DelegatePort* new_port = mCreator(this, sor, port);
    if (new_port != NULL) {
        (*pm)[port] = new_port;
    }
    return new_port;
}
DelegateService::~DelegateService() {
    while(!mSpacePortMap.empty()) {
        SpacePortMap::iterator spm_it = mSpacePortMap.begin();
        PortMap* pm = spm_it->second;

        while(!pm->empty()) {
            PortMap::iterator pm_it = pm->begin();
            DelegatePort* prt = pm_it->second;
            prt->invalidate(); // Will remove from pm_it via deallocatePort
        }

        mSpacePortMap.erase(spm_it);
        delete pm;
    }
}
QueryData genListeningPorts(QueryContext& context) {
  QueryData results;

  auto sockets = SQL::selectAllFrom("process_open_sockets");

  PortMap ports;
  for (const auto& socket : sockets) {
    if (socket.at("remote_port") != "0") {
      // Listening UDP/TCP ports have a remote_port == "0"
      continue;
    }

    if (ports.count(socket.at("local_port")) > 0) {
      bool duplicate = false;
      for (const auto& entry : ports[socket.at("local_port")]) {
        if (entry.first == socket.at("protocol") &&
            entry.second == socket.at("family")) {
          duplicate = true;
          break;
        }
      }

      if (duplicate) {
        // There is a duplicate socket descriptor for this bind.
        continue;
      }
    }

    // Add this family/protocol/port bind to the tracked map.
    ports[socket.at("local_port")].push_back(
        std::make_pair(socket.at("protocol"), socket.at("family")));

    Row r;
    r["pid"] = socket.at("pid");
    r["port"] = socket.at("local_port");
    r["protocol"] = socket.at("protocol");
    r["family"] = socket.at("family");
    r["address"] = socket.at("local_address");

    results.push_back(r);
  }

  return results;
}
uint VnaCorrections::switchMatrices() {
    if (_vna->properties().isZvaFamily())
        return 0;

    _vna->isError();
    _vna->clearStatus();
    bool displayError = _vna->settings().isErrorDisplayOn();
    _vna->settings().errorDisplayOff();

    uint i = 0;
    PortMap map = switchMatrixToVnaPortMap(i+1);
    while (!_vna->isError() && !map.isEmpty()) {
        i++;
        map = switchMatrixToVnaPortMap(i+1);
    }

    _vna->clearStatus();
    _vna->settings().errorDisplayOn(displayError);
    return i;
}
bool VnaCorrections::areSwitchMatrices() {
    if (_vna->properties().isZvaFamily())
        return false;

    _vna->isError();
    _vna->clearStatus();
    bool displayError = _vna->settings().isErrorDisplayOn();
    _vna->settings().errorDisplayOff();

    const PortMap map = switchMatrixToVnaPortMap(1);
    if (!map.isEmpty() && !_vna->isError()) {
        _vna->clearStatus();
        _vna->settings().errorDisplayOn(displayError);
        return true;
    }
    else {
        _vna->settings().errorDisplayOn(displayError);
        return false;
    }
}
Exemple #8
0
void CGameClientInput::ProcessJoysticks()
{
  PERIPHERALS::PeripheralVector joysticks;
  CServiceBroker::GetPeripherals().GetPeripheralsWithFeature(joysticks, PERIPHERALS::FEATURE_JOYSTICK);

  // Perform the port mapping
  PortMap newPortMap = MapJoysticks(joysticks, m_joysticks);

  // Update each joystick
  for (auto& peripheralJoystick : joysticks)
  {
    // Upcast to input interface
    JOYSTICK::IInputProvider *inputProvider = peripheralJoystick.get();

    auto itConnectedPort = newPortMap.find(inputProvider);
    auto itDisconnectedPort = m_portMap.find(inputProvider);

    CGameClientJoystick* newJoystick = itConnectedPort != newPortMap.end() ? itConnectedPort->second : nullptr;
    CGameClientJoystick* oldJoystick = itDisconnectedPort != m_portMap.end() ? itDisconnectedPort->second : nullptr;

    if (oldJoystick != newJoystick)
    {
      // Unregister old input handler
      if (oldJoystick != nullptr)
      {
        oldJoystick->UnregisterInput(inputProvider);
        m_portMap.erase(itDisconnectedPort);
      }

      // Register new handler
      if (newJoystick != nullptr)
      {
        newJoystick->RegisterInput(inputProvider);
        m_portMap[inputProvider] = newJoystick;
      }
    }
  }
}
McSimplePre::McReturnCode
McSimplePreLAG::mc_set_lag_membership(const lag_id_t lag_index,
                                      const PortMap &port_map) {
  boost::unique_lock<boost::shared_mutex> lock(mutex);
  uint16_t member_count = 0;
  if (lag_index > LAG_MAX_ENTRIES) {
    Logger::get()->error("lag membership set failed, invalid lag index");
    return ERROR;
  }
  for (unsigned int j = 0; j < port_map.size(); j++) {
    if (port_map[j]) {
      member_count++;
    }
  }
  LagEntry &lag_entry = lag_entries[lag_index];
  lag_entry.member_count = member_count;
  lag_entry.port_map = port_map;
  Logger::get()->debug("lag membership set for lag index {}", lag_index);
  return SUCCESS;
}
McSimplePre::McReturnCode
McSimplePreLAG::mc_set_lag_membership(const lag_id_t lag_index,
                                      const PortMap &port_map) {
    boost::unique_lock<boost::shared_mutex> lock(lag_lock);
    uint16_t member_count = 0;
    if (lag_index > LAG_MAX_ENTRIES) {
        std::cout << "lag membership set failed. invalid lag index!\n";
        return ERROR;
    }
    for (unsigned int j = 0; j < port_map.size(); j++) {
        if (port_map[j]) {
            member_count++;
        }
    }
    LagEntry &lag_entry = lag_entries[lag_index];
    lag_entry.member_count = member_count;
    lag_entry.port_map = port_map;
    if (pre_debug) {
        std::cout << "lag membership set for lag index : " << lag_index << "\n";
    }
    return SUCCESS;
}