1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
/*
(c) Copyright 2012-2013 DirectFB integrated media GmbH
(c) Copyright 2001-2013 The world wide DirectFB Open Source Community (directfb.org)
(c) Copyright 2000-2004 Convergence (integrated media) GmbH
All rights reserved.
Written by Denis Oliver Kropp <dok@directfb.org>,
Andreas Shimokawa <andi@directfb.org>,
Marek Pikarski <mass@directfb.org>,
Sven Neumann <neo@directfb.org>,
Ville Syrjälä <syrjala@sci.fi> and
Claudio Ciccani <klan@users.sf.net>.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <direct/debug.h>
#include <direct/list.h>
#include <direct/mem.h>
#include <direct/messages.h>
#include <direct/thread.h>
#include <direct/util.h>
#include <voodoo/client.h>
#include <voodoo/conf.h>
#include <voodoo/internal.h>
#include <voodoo/link.h>
#include <voodoo/manager.h>
#include <voodoo/play.h>
D_DEBUG_DOMAIN( Voodoo_Client, "Voodoo/Client", "Voodoo Client" );
/**********************************************************************************************************************/
struct __V_VoodooClient {
DirectLink link;
int refs;
VoodooLink vl;
VoodooManager *manager;
char *host;
int port;
};
static DirectLink *m_clients; // FIXME: add lock
/**********************************************************************************************************************/
static DirectResult
send_discover_and_receive_info( VoodooLink *link,
VoodooPlayVersion *ret_version,
VoodooPlayInfo *ret_info )
{
struct {
VoodooMessageHeader header;
VoodooPlayVersion version;
VoodooPlayInfo info;
} msg;
int ret;
VoodooMessageHeader header;
size_t got;
D_INFO( "Voodoo/Player: Sending VMSG_DISCOVER message via Voodoo TCP port...\n" );
header.size = sizeof(VoodooMessageHeader);
header.serial = 0;
header.type = VMSG_DISCOVER;
ret = link->Write( link, &header, sizeof(header) );
if (ret < 0) {
ret = errno2result( errno );
D_PERROR( "Voodoo/Player: Failed to send VMSG_DISCOVER message via Voodoo TCP port!\n" );
return ret;
}
// wait for up to one second (old server will not reply anything, so we have to timeout)
ret = link->WaitForData( link, 1000 );
if (ret) {
D_DERROR( ret, "Voodoo/Player: Failed to wait for reply after sending VMSG_DISCOVER message via Voodoo TCP port!\n" );
return ret;
}
D_INFO( "Voodoo/Player: New Voodoo Server with VMSG_DISCOVER support, reading version/info (SENDINFO) reply...\n" );
got = 0;
while (got < sizeof(msg)) {
ret = link->Read( link, (u8*) &msg + got, sizeof(msg) - got );
if (ret < 0) {
ret = errno2result( errno );
D_PERROR( "Voodoo/Player: Failed to read after sending VMSG_DISCOVER message via Voodoo TCP port!\n" );
return ret;
}
got += ret;
}
if (msg.header.type != VMSG_SENDINFO) {
D_ERROR( "Voodoo/Player: Received message after sending VMSG_DISCOVER message via Voodoo TCP port is no VMSG_SENDINFO!\n");
return DR_INVARG;
}
*ret_version = msg.version;
*ret_info = msg.info;
D_INFO( "Voodoo/Player: Voodoo Server sent name '%s', version %d.%d.%d\n",
msg.info.name, msg.version.v[1], msg.version.v[2], msg.version.v[3] );
return DR_OK;
}
/**********************************************************************************************************************/
static DirectResult
discover_host( VoodooPlayer *player,
const char *address,
VoodooPlayInfo *ret_info,
char *ret_addr,
int max_addr )
{
DirectResult ret;
int bc_num = 5;
int bc_wait = 30000;
voodoo_player_broadcast( player );
while (bc_num--) {
direct_thread_sleep( bc_wait );
bc_wait += bc_wait;
if (address)
ret = voodoo_player_lookup_by_address( player, address, ret_info );
else
ret = voodoo_player_lookup( player, NULL, ret_info, ret_addr, max_addr );
if (ret == DR_OK)
break;
voodoo_player_broadcast( player );
}
return ret;
}
DirectResult
voodoo_client_create( const char *host,
int port,
VoodooClient **ret_client )
{
DirectResult ret;
VoodooPlayInfo info;
VoodooClient *client;
VoodooPlayer *player;
char buf[100] = { 0 };
const char *hostname = host;
bool raw = true;
D_ASSERT( ret_client != NULL );
if (!host)
host = "";
if (!port)
port = 2323;
D_DEBUG_AT( Voodoo_Client, "%s( '%s', %d )\n", __FUNCTION__, host, port );
if (port != 2323) {
D_DEBUG_AT( Voodoo_Client, " -> port != 2323, using PACKET mode right away\n" );
raw = false;
}
direct_list_foreach (client, m_clients) {
if (!strcmp( client->host, host ) && client->port == port) {
D_INFO( "Voodoo/Client: Reconnecting to '%s', increasing ref count of existing connection!\n", host );
client->refs++;
*ret_client = client;
return DR_OK;
}
}
/*
* Get the player singleton
*/
ret = voodoo_player_create( NULL, &player );
if (ret) {
D_DERROR( ret, "Voodoo/Client: Could not create the player!\n" );
return ret;
}
/*
* If we got a hostname or address try to lookup the player info
*/
// FIXME: resolve first, not late in voodoo_link_init_connect
if (hostname && hostname[0]) {
ret = voodoo_player_lookup_by_address( player, hostname, &info );
if (ret == DR_OK) {
if (info.flags & VPIF_PACKET)
raw = false;
}
}
else {
/*
* Start discovery and use first host visible
*/
ret = discover_host( player, NULL, &info, buf, sizeof(buf) );
if (ret == DR_OK) {
if (info.flags & VPIF_PACKET)
raw = false;
hostname = buf;
}
}
if (!hostname || !hostname[0]) {
D_ERROR( "Voodoo/Client: Did not find any other player!\n" );
return DR_ITEMNOTFOUND;
}
/* Allocate client structure. */
client = D_CALLOC( 1, sizeof(VoodooClient) );
if (!client)
return D_OOM();
raw = !voodoo_config->link_packet && (voodoo_config->link_raw || raw);
/* Create a link to the other player. */
ret = voodoo_link_init_connect( &client->vl, hostname, port, raw );
if (ret) {
D_DERROR( ret, "Voodoo/Client: Failed to initialize Voodoo Link!\n" );
D_FREE( client );
return ret;
}
D_INFO( "Voodoo/Client: Fetching player information...\n" );
if (raw) { // FIXME: send_discover_and_receive_info() only does RAW, but we don't need it for packet connection, yet
VoodooPlayVersion version;
VoodooPlayInfo info;
ret = send_discover_and_receive_info( &client->vl, &version, &info );
if (ret) {
D_DEBUG_AT( Voodoo_Client, " -> Failed to receive player info via TCP!\n" );
D_INFO( "Voodoo/Client: No player information from '%s', trying to discover via UDP!\n", host );
/*
* Fallback to UDP discovery
*/
ret = discover_host( player, hostname, &info, buf, sizeof(buf) );
if (ret == DR_OK) {
if (info.flags & VPIF_PACKET)
raw = false;
}
}
else {
D_INFO( "Voodoo/Client: Connected to '%s' (%-15s) %s "
"=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x= "
"(vendor: %s, model: %s)\n",
info.name, host,
(info.flags & VPIF_LEVEL2) ? "*" : " ",
info.uuid[0], info.uuid[1], info.uuid[2], info.uuid[3], info.uuid[4],
info.uuid[5], info.uuid[6], info.uuid[7], info.uuid[8], info.uuid[9],
info.uuid[10], info.uuid[11], info.uuid[12], info.uuid[13], info.uuid[14],
info.uuid[15],
info.vendor, info.model );
if (raw && !voodoo_config->link_raw) {
/*
* Switch to packet mode?
*/
if (info.flags & VPIF_PACKET)
raw = false;
}
}
/*
* Switch to packet mode?
*/
if (!raw) {
D_INFO( "Voodoo/Client: Switching to packet mode!\n" );
client->vl.Close( &client->vl );
/* Create another link to the other player. */
ret = voodoo_link_init_connect( &client->vl, hostname, port, false );
if (ret) {
D_DERROR( ret, "Voodoo/Client: Failed to initialize second Voodoo Link!\n" );
D_FREE( client );
return ret;
}
}
}
/* Create the manager. */
ret = voodoo_manager_create( &client->vl, client, NULL, &client->manager );
if (ret) {
client->vl.Close( &client->vl );
D_FREE( client );
return ret;
}
client->refs = 1;
client->host = D_STRDUP( host );
client->port = port;
direct_list_prepend( &m_clients, &client->link );
/* Return the new client. */
*ret_client = client;
D_DEBUG_AT( Voodoo_Client, " => client %p\n", client );
return DR_OK;
}
DirectResult
voodoo_client_destroy( VoodooClient *client )
{
D_ASSERT( client != NULL );
D_DEBUG_AT( Voodoo_Client, "%s( %p )\n", __FUNCTION__, client );
D_INFO( "Voodoo/Client: Decreasing ref count of connection...\n" );
if (! --(client->refs)) {
voodoo_manager_destroy( client->manager );
//client->vl.Close( &client->vl );
direct_list_remove( &m_clients, &client->link );
D_FREE( client->host );
D_FREE( client );
}
return DR_OK;
}
VoodooManager *
voodoo_client_manager( const VoodooClient *client )
{
D_ASSERT( client != NULL );
return client->manager;
}
|