Beispiel #1
0
// Init
status_t
ServerConnection::Init(vnode_id connectionBrokenTarget)
{
	if (!fServerInfo)
		RETURN_ERROR(B_BAD_VALUE);

	// create a connection broken event
	fConnectionBrokenEvent
		= new(std::nothrow) ConnectionBrokenEvent(connectionBrokenTarget);
	if (!fConnectionBrokenEvent)
		return B_NO_MEMORY;

	// get the server address
	const char* connectionMethod = fServerInfo->GetConnectionMethod();
	HashString server;
	status_t error = fServerInfo->GetAddress().GetString(&server, false);
	if (error != B_OK)
		RETURN_ERROR(error);

	// create the volume map
	fVolumes = new(std::nothrow) VolumeMap;
	if (!fVolumes)
		RETURN_ERROR(B_NO_MEMORY);
	error = fVolumes->InitCheck();
	if (error != B_OK)
		RETURN_ERROR(error);

	// establish the connection
	Connection* connection;
	ConnectionFactory factory;
	error = factory.CreateConnection(connectionMethod, server.GetString(),
		&connection);
	if (error != B_OK)
		RETURN_ERROR(error);

	// create a request connection
	fConnection = new(std::nothrow) RequestConnection(connection, this);
	if (!fConnection) {
		delete connection;
		RETURN_ERROR(B_NO_MEMORY);
	}
	error = fConnection->Init();
	if (error != B_OK)
		return error;

	// send an `init connection request'

	// prepare the request
	InitConnectionRequest request;
	request.bigEndian = B_HOST_IS_BENDIAN;

	// send the request
	Request* _reply;
	error = fConnection->SendRequest(&request, &_reply);
	if (error != B_OK)
		return error;
	ObjectDeleter<Request> replyDeleter(_reply);

	// everything OK?
	InitConnectionReply* reply = dynamic_cast<InitConnectionReply*>(_reply);
	if (!reply)
		return B_BAD_DATA;
	if (reply->error != B_OK)
		return reply->error;

	fConnected = true;
	return B_OK;
}