xref: /haiku/src/kits/media/DataExchange.cpp (revision d7e489f80a82a0dc5974df1e780d7a908129bab4)
1 /*
2  * Copyright 2002, Marcus Overhagen. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <OS.h>
7 #include <Messenger.h>
8 #include <string.h>
9 #include "debug.h"
10 #include "PortPool.h"
11 #include "DataExchange.h"
12 #include "ServerInterface.h" // NEW_MEDIA_SERVER_SIGNATURE
13 
14 #define TIMEOUT 2000000
15 
16 namespace BPrivate {
17 namespace media {
18 
19 team_id team;
20 
21 namespace dataexchange {
22 
23 BMessenger *MediaServerMessenger;
24 static port_id MediaServerPort;
25 static port_id MediaAddonServerPort;
26 
27 class initit
28 {
29 public:
30 	initit()
31 	{
32 		MediaServerMessenger = new BMessenger(NEW_MEDIA_SERVER_SIGNATURE);
33 		MediaServerPort = find_port("media_server port");
34 		MediaAddonServerPort = find_port("media_addon_server port");
35 
36 		thread_info info;
37 		get_thread_info(find_thread(NULL), &info);
38 		team = info.team;
39 	}
40 	~initit()
41 	{
42 		delete MediaServerMessenger;
43 	}
44 };
45 initit _initit;
46 
47 
48 status_t
49 request_data::SendReply(status_t result, reply_data *reply, int replysize) const
50 {
51 	reply->result = result;
52 	return SendToPort(reply_port, 0, reply, replysize);
53 }
54 
55 
56 // BMessage based data exchange with the media_server
57 status_t SendToServer(BMessage *msg)
58 {
59 	status_t rv;
60 	rv = MediaServerMessenger->SendMessage(msg, static_cast<BHandler *>(NULL), TIMEOUT);
61 	if (rv != B_OK)
62 		TRACE("SendToServer: SendMessage failed\n");
63 	return rv;
64 }
65 
66 
67 status_t QueryServer(BMessage *request, BMessage *reply)
68 {
69 	status_t rv;
70 	rv = MediaServerMessenger->SendMessage(request, reply, TIMEOUT, TIMEOUT);
71 	if (rv != B_OK)
72 		TRACE("QueryServer: SendMessage failed\n");
73 	return rv;
74 }
75 
76 
77 // Raw data based data exchange with the media_server
78 status_t SendToServer(int32 msgcode, void *msg, int size)
79 {
80 	return SendToPort(MediaServerPort, msgcode, msg, size);
81 }
82 
83 status_t QueryServer(int32 msgcode, request_data *request, int requestsize, reply_data *reply, int replysize)
84 {
85 	return QueryPort(MediaServerPort, msgcode, request, requestsize, reply, replysize);
86 }
87 
88 
89 // Raw data based data exchange with the media_addon_server
90 status_t SendToAddonServer(int32 msgcode, void *msg, int size)
91 {
92 	return SendToPort(MediaAddonServerPort, msgcode, msg, size);
93 }
94 
95 
96 status_t QueryAddonServer(int32 msgcode, request_data *request, int requestsize, reply_data *reply, int replysize)
97 {
98 	return QueryPort(MediaAddonServerPort, msgcode, request, requestsize, reply, replysize);
99 }
100 
101 
102 // Raw data based data exchange with the media_server
103 status_t SendToPort(port_id sendport, int32 msgcode, void *msg, int size)
104 {
105 	status_t rv;
106 	rv = write_port_etc(sendport, msgcode, msg, size, B_RELATIVE_TIMEOUT, TIMEOUT);
107 	if (rv != B_OK)
108 		TRACE("SendToPort: write_port failed, port %ld, error %#lx (%s)\n", sendport, rv, strerror(rv));
109 	return B_OK;
110 }
111 
112 
113 status_t QueryPort(port_id requestport, int32 msgcode, request_data *request, int requestsize, reply_data *reply, int replysize)
114 {
115 	status_t rv;
116 	int32 code;
117 
118 	request->reply_port = _PortPool->GetPort();
119 
120 	rv = write_port_etc(requestport, msgcode, request, requestsize, B_RELATIVE_TIMEOUT, TIMEOUT);
121 	if (rv != B_OK) {
122 		TRACE("QueryPort: write_port failed, port %ld, error %#lx (%s)\n", requestport, rv, strerror(rv));
123 		_PortPool->PutPort(request->reply_port);
124 		return rv;
125 	}
126 
127 	rv = read_port_etc(request->reply_port, &code, reply, replysize, B_RELATIVE_TIMEOUT, TIMEOUT);
128 	_PortPool->PutPort(request->reply_port);
129 
130 	if (rv < B_OK)
131 		TRACE("QueryPort: read_port failed, port %ld, error %#lx (%s)\n", request->reply_port, rv, strerror(rv));
132 
133 	return (rv < B_OK) ? rv : reply->result;
134 }
135 
136 }; // dataexchange
137 }; // media
138 }; // BPrivate
139 
140