Add affinity between the program and its mixer control.

Currently, if there are two instances of the same program, sndiod will
allocate one volume control to each. If both programs disconnect and
reconnect, the information of which control is assigned to which
program is lost. This makes difficult to run two instances of a player
and crossfade between each other with a MIDI controller.

To address this, the program chooses a 32-bit "id" (for now the
process pid) and sends it to the server. The server records the id in
the client's slot structure.  When the server accepts a new
connection, it uses the id to identify the slot the client used during
the previous connection; if it was not recycled yet, it's assigned to
the program.
This commit is contained in:
Alexandre Ratchov 2019-07-12 08:31:01 +02:00
parent 93ffa39ea2
commit 285eafeec0
5 changed files with 21 additions and 5 deletions

View File

@ -99,7 +99,7 @@ struct amsg {
#define AMSG_VERSION 7
uint8_t version; /* protocol version */
uint8_t devnum; /* device number */
uint32_t _reserved[1]; /* for future use */
uint32_t id; /* client id */
#define AMSG_OPTMAX 12
char opt[AMSG_OPTMAX]; /* profile name */
char who[12]; /* hint for leases */

View File

@ -548,6 +548,7 @@ _aucat_open(struct aucat *hdl, const char *str, unsigned int mode)
hdl->wmsg.u.hello.version = AMSG_VERSION;
hdl->wmsg.u.hello.mode = htons(mode);
hdl->wmsg.u.hello.devnum = devnum;
hdl->wmsg.u.hello.id = htonl(getpid());
strlcpy(hdl->wmsg.u.hello.who, __progname,
sizeof(hdl->wmsg.u.hello.who));
strlcpy(hdl->wmsg.u.hello.opt, opt,

View File

@ -1529,7 +1529,7 @@ slot_freebufs(struct slot *s)
* allocate a new slot and register the given call-backs
*/
struct slot *
slot_new(struct dev *d, struct opt *opt, char *who,
slot_new(struct dev *d, struct opt *opt, unsigned int id, char *who,
struct slotops *ops, void *arg, int mode)
{
char *p;
@ -1564,13 +1564,24 @@ slot_new(struct dev *d, struct opt *opt, char *who,
unit[s->unit] = s;
}
/*
* find the free slot with the least unit number and same id
*/
for (i = 0; i < DEV_NSLOT; i++) {
s = unit[i];
if (s != NULL && s->ops == NULL && s->id == id)
goto found;
}
/*
* find the free slot with the least unit number
*/
for (i = 0; i < DEV_NSLOT; i++) {
s = unit[i];
if (s != NULL && s->ops == NULL)
if (s != NULL && s->ops == NULL) {
s->id = id;
goto found;
}
}
/*
@ -1596,6 +1607,7 @@ slot_new(struct dev *d, struct opt *opt, char *who,
for (i = 0; unit[i] != NULL; i++)
; /* nothing */
s->unit = i;
s->id = id;
goto found;
}

View File

@ -89,6 +89,7 @@ struct slot {
unsigned int unit; /* instance of name */
unsigned int serial; /* global unique number */
unsigned int vol; /* current (midi) volume */
unsigned int id; /* process id */
};
struct opt {
@ -231,7 +232,7 @@ void dev_midi_vol(struct dev *, struct slot *);
* sio_open(3) like interface for clients
*/
void slot_log(struct slot *);
struct slot *slot_new(struct dev *, struct opt *, char *,
struct slot *slot_new(struct dev *, struct opt *, unsigned int, char *,
struct slotops *, void *, int);
void slot_del(struct slot *);
void slot_setvol(struct slot *, unsigned int);

View File

@ -769,8 +769,10 @@ sock_hello(struct sock *f)
struct dev *d;
struct opt *opt;
unsigned int mode;
unsigned int id;
mode = ntohs(p->mode);
id = ntohl(p->id);
#ifdef DEBUG
if (log_level >= 3) {
sock_log(f);
@ -842,7 +844,7 @@ sock_hello(struct sock *f)
opt = opt_byname(d, p->opt);
if (opt == NULL)
return 0;
f->slot = slot_new(d, opt, p->who, &sock_slotops, f, mode);
f->slot = slot_new(d, opt, id, p->who, &sock_slotops, f, mode);
if (f->slot == NULL)
return 0;
f->midi = NULL;