add: TRACE("ServerApp::HandleMessage %#lx leave\n", code);
context: ServerApp::HandleMessage(int32 code, void *data, size_t size){	status_t rv;	INFO("ServerApp::HandleMessage %#lx\n", code);	switch (code) {		case SERVER_CHANGE_ADDON_FLAVOR_INSTANCES_COUNT:		{			const server_change_addon_flavor_instances_count_request *request = reinterpret_cast<const server_change_addon_flavor_instances_count_request *>(data);			server_change_addon_flavor_instances_count_reply reply;			ASSERT(request->delta == 1 || request->delta == -1);			if (request->delta == 1)				rv = gNodeManager->IncrementAddonFlavorInstancesCount(request->addonid,	request->flavorid, request->team);			else				rv = gNodeManager->DecrementAddonFlavorInstancesCount(request->addonid,	request->flavorid, request->team);			request->SendReply(rv, &reply, sizeof(reply));			break;		}		case SERVER_RESCAN_DEFAULTS:		{			gNodeManager->RescanDefaultNodes();			break;		}			case SERVER_REGISTER_ADDONSERVER:		{			const server_register_addonserver_request *request = reinterpret_cast<const server_register_addonserver_request *>(data);			server_register_addonserver_reply reply;			rv = gAppManager->RegisterAddonServer(request->team);			request->SendReply(rv, &reply, sizeof(reply));			break;		}				case SERVER_REGISTER_APP:		{			const server_register_app_request *request = reinterpret_cast<const server_register_app_request *>(data);			server_register_app_reply reply;			rv = gAppManager->RegisterTeam(request->team, request->messenger);			request->SendReply(rv, &reply, sizeof(reply));			break;		}		case SERVER_UNREGISTER_APP:		{			const server_unregister_app_request *request = reinterpret_cast<const server_unregister_app_request *>(data);			server_unregister_app_reply reply;			rv = gAppManager->UnregisterTeam(request->team);			request->SendReply(rv, &reply, sizeof(reply));			break;		}			case SERVER_GET_MEDIAADDON_REF:		{			server_get_mediaaddon_ref_request *msg = (server_get_mediaaddon_ref_request *)data;			server_get_mediaaddon_ref_reply reply;			entry_ref tempref;			reply.result = gNodeManager->GetAddonRef(&tempref, msg->addonid);			reply.ref = tempref;			write_port(msg->reply_port, 0, &reply, sizeof(reply));			break;		}		case SERVER_NODE_ID_FOR:		{			const server_node_id_for_request *request = reinterpret_cast<const server_node_id_for_request *>(data);			server_node_id_for_reply reply;			rv = gNodeManager->FindNodeId(&reply.nodeid, request->port);			request->SendReply(rv, &reply, sizeof(reply));			break;		}				case SERVER_GET_LIVE_NODE_INFO:		{			const server_get_live_node_info_request *request = reinterpret_cast<const server_get_live_node_info_request *>(data);			server_get_live_node_info_reply reply;			rv = gNodeManager->GetLiveNodeInfo(&reply.live_info, request->node);			request->SendReply(rv, &reply, sizeof(reply));			break;		}				case SERVER_GET_LIVE_NODES:		{			const server_get_live_nodes_request *request = reinterpret_cast<const server_get_live_nodes_request *>(data);			server_get_live_nodes_reply reply;			Stack<live_node_info> livenodes;			rv = gNodeManager->GetLiveNodes(					&livenodes,					request->maxcount,					request->has_input ? &request->inputformat : NULL,					request->has_output ? &request->outputformat : NULL,					request->has_name ? request->name : NULL,					request->require_kinds);			reply.count = livenodes.CountItems();			if (reply.count <= MAX_LIVE_INFO) {				for (int32 index = 0; index < reply.count; index++)					livenodes.Pop(&reply.live_info[index]);				reply.area = -1;			} else {				// we create an area here, and pass it to the library, where it will be deleted.				live_node_info *start_addr;				size_t size;				size = ((reply.count * sizeof(live_node_info)) + B_PAGE_SIZE - 1) & ~(B_PAGE_SIZE - 1);				reply.area = create_area("get live nodes", reinterpret_cast<void **>(&start_addr), B_ANY_ADDRESS, size, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);				if (reply.area < B_OK) {					FATAL("SERVER_GET_LIVE_NODES: failed to create area, error %#lx\n", reply.area);					reply.count = 0;					rv = B_ERROR;				} else {					for (int32 index = 0; index < reply.count; index++)						livenodes.Pop(&start_addr[index]);				}			}			rv = request->SendReply(rv, &reply, sizeof(reply));			if (rv != B_OK)				delete_area(reply.area); // if we couldn't send the message, delete the area			break;		}				case SERVER_GET_NODE_FOR:		{			const server_get_node_for_request *request = reinterpret_cast<const server_get_node_for_request *>(data);			server_get_node_for_reply reply;			rv = gNodeManager->GetCloneForId(&reply.clone, request->nodeid, request->team);			request->SendReply(rv, &reply, sizeof(reply));			break;		}				case SERVER_RELEASE_NODE:		{			const server_release_node_request *request = reinterpret_cast<const server_release_node_request *>(data);			server_release_node_reply reply;			rv = gNodeManager->ReleaseNode(request->node, request->team);			request->SendReply(rv, &reply, sizeof(reply));			break;		}				case SERVER_REGISTER_NODE:		{			const server_register_node_request *request = reinterpret_cast<const server_register_node_request *>(data);			server_register_node_reply reply;			rv = gNodeManager->RegisterNode(&reply.nodeid, request->addon_id, request->addon_flavor_id, request->name, request->kinds, request->port, request->team);			request->SendReply(rv, &reply, sizeof(reply));			break;		}				case SERVER_UNREGISTER_NODE:		{			const server_unregister_node_request *request = reinterpret_cast<const server_unregister_node_request *>(data);			server_unregister_node_reply reply;			rv = gNodeManager->UnregisterNode(&reply.addonid, &reply.flavorid, request->nodeid, request->team);			request->SendReply(rv, &reply, sizeof(reply));			break;		}		case SERVER_PUBLISH_INPUTS:		{			const server_publish_inputs_request *request = reinterpret_cast<const server_publish_inputs_request *>(data);			server_publish_inputs_reply reply;			if (request->count <= MAX_INPUTS) {				rv = gNodeManager->PublishInputs(request->node, request->inputs, request->count);			} else {				media_input *inputs;				area_id clone;				clone = clone_area("media_inputs clone", reinterpret_cast<void **>(&inputs), B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA, request->area);				if (clone < B_OK) {					FATAL("SERVER_PUBLISH_INPUTS: failed to clone area, error %#lx\n", clone);					rv = B_ERROR;				} else {					rv = gNodeManager->PublishInputs(request->node, inputs, request->count);					delete_area(clone);				}			}			request->SendReply(rv, &reply, sizeof(reply));			break;		}				case SERVER_PUBLISH_OUTPUTS:		{			const server_publish_outputs_request *request = reinterpret_cast<const server_publish_outputs_request *>(data);			server_publish_outputs_reply reply;			if (request->count <= MAX_OUTPUTS) {				rv = gNodeManager->PublishOutputs(request->node, request->outputs, request->count);			} else {				media_output *outputs;				area_id clone;				clone = clone_area("media_outputs clone", reinterpret_cast<void **>(&outputs), B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA, request->area);				if (clone < B_OK) {					FATAL("SERVER_PUBLISH_OUTPUTS: failed to clone area, error %#lx\n", clone);					rv = B_ERROR;				} else {					rv = gNodeManager->PublishOutputs(request->node, outputs, request->count);					delete_area(clone);				}			}			request->SendReply(rv, &reply, sizeof(reply));			break;		}		case SERVER_GET_NODE:		{			const server_get_node_request *request = reinterpret_cast<const server_get_node_request *>(data);			server_get_node_reply reply;			rv = gNodeManager->GetClone(&reply.node, reply.input_name, &reply.input_id, request->type, request->team);			request->SendReply(rv, &reply, sizeof(reply));			break;		}		case SERVER_SET_NODE:		{			const server_set_node_request *request = reinterpret_cast<const server_set_node_request *>(data);			server_set_node_reply reply;			rv = gNodeManager->SetDefaultNode(request->type, request->use_node ? &request->node : NULL, request->use_dni ? &request->dni : NULL, request->use_input ?  &request->input : NULL);			request->SendReply(rv, &reply, sizeof(reply));			break;		}				case SERVER_GET_DORMANT_NODE_FOR:		{			const server_get_dormant_node_for_request *request = reinterpret_cast<const server_get_dormant_node_for_request *>(data);			server_get_dormant_node_for_reply reply;			rv = gNodeManager->GetDormantNodeInfo(&reply.node_info, request->node);			request->SendReply(rv, &reply, sizeof(reply));			break;		}		case SERVER_GET_INSTANCES_FOR:		{			const server_get_instances_for_request *request = reinterpret_cast<const server_get_instances_for_request *>(data);			server_get_instances_for_reply reply;			rv = gNodeManager->GetInstances(reply.node_id, &reply.count, min_c(request->maxcount, MAX_NODE_ID), request->addon_id, request->addon_flavor_id);			if (reply.count == MAX_NODE_ID && request->maxcount > MAX_NODE_ID) { // XXX might be fixed by using an area				FATAL("SERVER_GET_INSTANCES_FOR: WARNING! returning possibly truncated list of node id's\n");			}			request->SendReply(rv, &reply, sizeof(reply));			break;		}		case SERVER_REGISTER_MEDIAADDON:		{			server_register_mediaaddon_request *msg = (server_register_mediaaddon_request *)data;			server_register_mediaaddon_reply reply;			gNodeManager->RegisterAddon(msg->ref, &reply.addonid);			write_port(msg->reply_port, 0, &reply, sizeof(reply));			break;		}				case SERVER_UNREGISTER_MEDIAADDON:		{			server_unregister_mediaaddon_command *msg = (server_unregister_mediaaddon_command *)data;			gNodeManager->UnregisterAddon(msg->addonid);			break;		}				case SERVER_REGISTER_DORMANT_NODE:		{			xfer_server_register_dormant_node *msg = (xfer_server_register_dormant_node *)data;			dormant_flavor_info dfi;			if (msg->purge_id > 0)				gNodeManager->InvalidateDormantFlavorInfo(msg->purge_id);			rv = dfi.Unflatten(msg->dfi_type, &(msg->dfi), msg->dfi_size);			ASSERT(rv == B_OK);			gNodeManager->AddDormantFlavorInfo(dfi);				break;		}				case SERVER_GET_DORMANT_NODES:		{			xfer_server_get_dormant_nodes *msg = (xfer_server_get_dormant_nodes *)data;			xfer_server_get_dormant_nodes_reply reply;			dormant_node_info * infos = new dormant_node_info[msg->maxcount];			reply.count = msg->maxcount;			reply.result = gNodeManager->GetDormantNodes(				infos, 				&reply.count,				msg->has_input ? &msg->inputformat : NULL,				msg->has_output ? &msg->outputformat : NULL,				msg->has_name ? msg->name : NULL,				msg->require_kinds,				msg->deny_kinds);			if (reply.result != B_OK)				reply.count = 0;			write_port(msg->reply_port, 0, &reply, sizeof(reply));			if (reply.count > 0)				write_port(msg->reply_port, 0, infos, reply.count * sizeof(dormant_node_info));			delete [] infos;			break;		}				case SERVER_GET_DORMANT_FLAVOR_INFO:		{			xfer_server_get_dormant_flavor_info *msg = (xfer_server_get_dormant_flavor_info *)data;			dormant_flavor_info dfi;			status_t rv;			rv = gNodeManager->GetDormantFlavorInfoFor(msg->addon, msg->flavor_id, &dfi);			if (rv != B_OK) {				xfer_server_get_dormant_flavor_info_reply reply;				reply.result = rv;				write_port(msg->reply_port, 0, &reply, sizeof(reply));			} else {				xfer_server_get_dormant_flavor_info_reply *reply;				int replysize;				replysize = sizeof(xfer_server_get_dormant_flavor_info_reply) + dfi.FlattenedSize();				reply = (xfer_server_get_dormant_flavor_info_reply *)malloc(replysize);				reply->dfi_size = dfi.FlattenedSize();				reply->dfi_type = dfi.TypeCode();				reply->result = dfi.Flatten(reply->dfi, reply->dfi_size);				write_port(msg->reply_port, 0, reply, replysize);				free(reply);			}			break;		}		case SERVER_SET_NODE_CREATOR:		{			const server_set_node_creator_request *request = reinterpret_cast<const server_set_node_creator_request *>(data);			server_set_node_creator_reply reply;			rv = gNodeManager->SetNodeCreator(request->node, request->creator);			request->SendReply(rv, &reply, sizeof(reply));			break;		}		case SERVER_GET_SHARED_BUFFER_AREA:		{			const server_get_shared_buffer_area_request *request = reinterpret_cast<const server_get_shared_buffer_area_request *>(data);			server_get_shared_buffer_area_reply reply;			reply.area = gBufferManager->SharedBufferListID();			request->SendReply(B_OK, &reply, sizeof(reply));			break;		}						case SERVER_REGISTER_BUFFER:		{			const server_register_buffer_request *request = reinterpret_cast<const server_register_buffer_request *>(data);			server_register_buffer_reply reply;			status_t status;			if (request->info.buffer == 0) {				reply.info = request->info; //size, offset, flags, area is kept				// get a new beuffer id into reply.info.buffer 				status = gBufferManager->RegisterBuffer(request->team, request->info.size, request->info.flags, request->info.offset, request->info.area, &reply.info.buffer);			} else {				reply.info = request->info; //buffer id is kept				status = gBufferManager->RegisterBuffer(request->team, request->info.buffer, &reply.info.size, &reply.info.flags, &reply.info.offset, &reply.info.area);			}			request->SendReply(status, &reply, sizeof(reply));			break;		}		case SERVER_UNREGISTER_BUFFER:		{			const server_unregister_buffer_command *cmd = reinterpret_cast<const server_unregister_buffer_command *>(data);			gBufferManager->UnregisterBuffer(cmd->team, cmd->bufferid);			break;		}		default:			printf("media_server: received unknown message code %#08lx\n",code);	}}