Example #1
0
/**
	@brief Create a new port and associate it with the monitor.
	@param portName The name to give the new port.
	@return true on success, false on failure.
*/
bool
PortMonitor::AddPort(const wchar_t* portName)
{
	if (!this->AddPortToRegistry(portName))
		return false;
		
	Loki::ScopeGuard registryGuard = Loki::MakeGuard(
		&PortMonitor::RemovePortFromRegistry, *this, portName
	);
	
	AddPortIpcRequest request(portName);

	PipeClient pipeClient;
	if (!pipeClient.Send(&request))
		return false;

	AddPortIpcResponse* response = request.GetExactResponse();

	if (response->GetReturnCode())
	{
		registryGuard.Dismiss();
		this->portNames.push_back(std::wstring(portName));
		return true;
	}
	else
	{
		SetLastError(response->GetErrorCode());
		return false;
	}
}
Example #2
0
/**
	@brief Delete the port matching the given name.
	@return true on success, false on failure.
*/
bool 
PortMonitor::DeletePort(const wchar_t* portName)
{
	std::vector<std::wstring>::iterator it;
	for (it = this->portNames.begin(); it != this->portNames.end(); ++it)
	{
		if (wcscmp(it->c_str(), portName) == 0)
		{
			if (!this->RemovePortFromRegistry(portName))
				return false;
				
			Loki::ScopeGuard registryGuard = Loki::MakeGuard(&PortMonitor::AddPortToRegistry, *this, portName);
				
			DeletePortIpcRequest request(portName);

			PipeClient pipeClient;
			if (!pipeClient.Send(&request))
				return false;

			DeletePortIpcResponse* response = request.GetExactResponse();

			if (response->GetReturnCode())
			{
				registryGuard.Dismiss();
				this->portNames.erase(it);
				return true;
			}
			else
			{
				SetLastError(response->GetErrorCode());
				return false;
			}
		}
	}
	return false;
}