player: require explicit connection-call
This makes it possible to change the host and port used for connections between player and editor without recompiling the library.
This commit is contained in:
parent
bd9408fa85
commit
078e344b24
@ -118,10 +118,12 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
sync_device *rocket = sync_create_device("sync");
|
sync_device *rocket = sync_create_device("sync");
|
||||||
if (!rocket)
|
if (!rocket)
|
||||||
die("something went wrong - failed to connect to host?");
|
die("out of memory?");
|
||||||
|
|
||||||
#ifndef SYNC_PLAYER
|
#ifndef SYNC_PLAYER
|
||||||
sync_set_callbacks(rocket, &bass_cb, (void *)stream);
|
sync_set_callbacks(rocket, &bass_cb, (void *)stream);
|
||||||
|
if (sync_connect(rocket, "localhost", SYNC_DEFAULT_PORT))
|
||||||
|
die("failed to connect to host");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* get tracks */
|
/* get tracks */
|
||||||
@ -144,7 +146,8 @@ int main(int argc, char *argv[])
|
|||||||
while (!done) {
|
while (!done) {
|
||||||
double row = bass_get_row(stream);
|
double row = bass_get_row(stream);
|
||||||
#ifndef SYNC_PLAYER
|
#ifndef SYNC_PLAYER
|
||||||
sync_update(rocket, (int)floor(row));
|
if (sync_update(rocket, (int)floor(row)))
|
||||||
|
sync_connect(rocket, "localhost", SYNC_DEFAULT_PORT);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* draw */
|
/* draw */
|
||||||
|
|||||||
@ -20,11 +20,6 @@ static const char *sync_track_path(const char *base, const char *name)
|
|||||||
|
|
||||||
#ifndef SYNC_PLAYER
|
#ifndef SYNC_PLAYER
|
||||||
|
|
||||||
#ifndef REMOTE_HOST
|
|
||||||
#define REMOTE_HOST "localhost"
|
|
||||||
#endif
|
|
||||||
#define REMOTE_PORT 1338
|
|
||||||
|
|
||||||
static SOCKET server_connect(const char *host, int nport)
|
static SOCKET server_connect(const char *host, int nport)
|
||||||
{
|
{
|
||||||
struct hostent *he;
|
struct hostent *he;
|
||||||
@ -93,7 +88,7 @@ struct sync_device *sync_create_device(const char *base)
|
|||||||
#ifndef SYNC_PLAYER
|
#ifndef SYNC_PLAYER
|
||||||
d->cb = d->cb_param = NULL;
|
d->cb = d->cb_param = NULL;
|
||||||
d->row = -1;
|
d->row = -1;
|
||||||
d->sock = server_connect(REMOTE_HOST, REMOTE_PORT);
|
d->sock = INVALID_SOCKET;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return d;
|
return d;
|
||||||
@ -230,16 +225,25 @@ static int purge_and_rerequest(struct sync_device *d)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int sync_connect(struct sync_device *d, const char *host, int port)
|
||||||
|
{
|
||||||
|
if (d->sock != INVALID_SOCKET)
|
||||||
|
closesocket(d->sock);
|
||||||
|
|
||||||
|
d->sock = server_connect(host, port);
|
||||||
|
if (d->sock == INVALID_SOCKET)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
return purge_and_rerequest(d);
|
||||||
|
}
|
||||||
|
|
||||||
int sync_update(struct sync_device *d, int row)
|
int sync_update(struct sync_device *d, int row)
|
||||||
{
|
{
|
||||||
if (d->sock == INVALID_SOCKET) {
|
if (d->sock == INVALID_SOCKET)
|
||||||
d->sock = server_connect(REMOTE_HOST, REMOTE_PORT);
|
return 1;
|
||||||
if (d->sock != INVALID_SOCKET && purge_and_rerequest(d))
|
|
||||||
goto sockerr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* look for new commands */
|
/* look for new commands */
|
||||||
while (d->sock != INVALID_SOCKET && socket_poll(d->sock)) {
|
while (socket_poll(d->sock)) {
|
||||||
unsigned char cmd = 0, flag;
|
unsigned char cmd = 0, flag;
|
||||||
int row;
|
int row;
|
||||||
if (!recv(d->sock, (char *)&cmd, 1, 0))
|
if (!recv(d->sock, (char *)&cmd, 1, 0))
|
||||||
@ -288,6 +292,7 @@ int sync_update(struct sync_device *d, int row)
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
sockerr:
|
sockerr:
|
||||||
|
closesocket(d->sock);
|
||||||
d->sock = INVALID_SOCKET;
|
d->sock = INVALID_SOCKET;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,6 +22,8 @@ struct sync_cb {
|
|||||||
int (*is_playing)(void *);
|
int (*is_playing)(void *);
|
||||||
};
|
};
|
||||||
void sync_set_callbacks(struct sync_device *, struct sync_cb *, void *);
|
void sync_set_callbacks(struct sync_device *, struct sync_cb *, void *);
|
||||||
|
#define SYNC_DEFAULT_PORT 1338
|
||||||
|
int sync_connect(struct sync_device *, const char *, int port);
|
||||||
int sync_update(struct sync_device *, int);
|
int sync_update(struct sync_device *, int);
|
||||||
#endif /* !defined(SYNC_PLAYER) */
|
#endif /* !defined(SYNC_PLAYER) */
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user