impl-client: improve constructor

Pass the protocol in constructor
Keep the link in the server protocol list private to the prototol.
This commit is contained in:
Wim Taymans 2019-12-11 17:19:12 +01:00
parent d99b1918ed
commit 73eea24443
5 changed files with 29 additions and 22 deletions

View file

@ -108,6 +108,8 @@ struct client_data {
struct pw_impl_client *client;
struct spa_hook client_listener;
struct spa_list protocol_link;
struct spa_source *source;
struct pw_protocol_native_connection *connection;
struct spa_hook conn_listener;
@ -261,7 +263,7 @@ connection_data(void *data, int fd, uint32_t mask)
if (res >= 0) {
int mask = this->source->mask;
SPA_FLAG_CLEAR(mask, SPA_IO_OUT);
pw_loop_update_io(client->protocol->context->main_loop,
pw_loop_update_io(client->context->main_loop,
this->source, mask);
} else if (res != EAGAIN) {
pw_log_error("client %p: could not flush: %s",
@ -279,10 +281,10 @@ static void client_free(void *data)
struct client_data *this = data;
struct pw_impl_client *client = this->client;
spa_list_remove(&client->protocol_link);
spa_list_remove(&this->protocol_link);
if (this->source)
pw_loop_destroy_source(client->protocol->context->main_loop, this->source);
pw_loop_destroy_source(client->context->main_loop, this->source);
if (this->connection)
pw_protocol_native_connection_destroy(this->connection);
@ -356,15 +358,13 @@ static struct client_data *client_new(struct server *s, int fd)
pw_properties_setf(props, PW_KEY_MODULE_ID, "%d", d->module->global->id);
client = pw_impl_client_new(protocol->context,
props,
sizeof(struct client_data));
protocol, props, sizeof(struct client_data));
if (client == NULL)
goto exit;
this = pw_impl_client_get_user_data(client);
client->protocol = protocol;
spa_list_append(&s->this.client_list, &client->protocol_link);
spa_list_append(&s->this.client_list, &this->protocol_link);
this->client = client;
this->source = pw_loop_add_io(pw_context_get_main_loop(context),
@ -868,13 +868,13 @@ error_free:
static void destroy_server(struct pw_protocol_server *server)
{
struct server *s = SPA_CONTAINER_OF(server, struct server, this);
struct pw_impl_client *client, *tmp;
struct client_data *data, *tmp;
spa_list_remove(&server->link);
spa_hook_remove(&s->hook);
spa_list_for_each_safe(client, tmp, &server->client_list, protocol_link)
pw_impl_client_destroy(client);
spa_list_for_each_safe(data, tmp, &server->client_list, protocol_link)
pw_impl_client_destroy(data->client);
if (s->source) {
spa_hook_remove(&s->hook);
@ -893,23 +893,20 @@ static void on_before_hook(void *_data)
{
struct server *server = _data;
struct pw_protocol_server *this = &server->this;
struct pw_impl_client *client, *tmp;
struct client_data *data;
struct client_data *data, *tmp;
int res;
spa_list_for_each_safe(client, tmp, &this->client_list, protocol_link) {
data = client->user_data;
spa_list_for_each_safe(data, tmp, &this->client_list, protocol_link) {
res = pw_protocol_native_connection_flush(data->connection);
if (res == -EAGAIN) {
int mask = data->source->mask;
SPA_FLAG_SET(mask, SPA_IO_OUT);
pw_loop_update_io(client->protocol->context->main_loop,
pw_loop_update_io(data->client->context->main_loop,
data->source, mask);
} else if (res < 0) {
pw_log_warn("client %p: could not flush: %s",
data->client, spa_strerror(res));
pw_impl_client_destroy(client);
pw_impl_client_destroy(data->client);
}
}

View file

@ -281,6 +281,7 @@ static const struct pw_context_events context_events = {
*/
SPA_EXPORT
struct pw_impl_client *pw_impl_client_new(struct pw_context *context,
struct pw_protocol *protocol,
struct pw_properties *properties,
size_t user_data_size)
{
@ -299,6 +300,7 @@ struct pw_impl_client *pw_impl_client_new(struct pw_context *context,
pw_log_debug(NAME" %p: new", this);
this->context = context;
this->protocol = protocol;
if (properties == NULL)
properties = pw_properties_new(NULL, NULL);
@ -428,6 +430,12 @@ struct pw_context *pw_impl_client_get_context(struct pw_impl_client *client)
return client->context;
}
SPA_EXPORT
struct pw_protocol *pw_impl_client_get_protocol(struct pw_impl_client *client)
{
return client->protocol;
}
SPA_EXPORT
struct pw_resource *pw_impl_client_get_core_resource(struct pw_impl_client *client)
{

View file

@ -110,9 +110,10 @@ struct pw_impl_client_events {
/** Create a new client. This is mainly used by protocols. */
struct pw_impl_client *
pw_impl_client_new(struct pw_context *context, /**< the context object */
struct pw_properties *properties, /**< client properties */
size_t user_data_size /**< extra user data size */);
pw_impl_client_new(struct pw_context *context, /**< the context object */
struct pw_protocol *prototol, /**< the client protocol */
struct pw_properties *properties, /**< client properties */
size_t user_data_size /**< extra user data size */);
/** Destroy a previously created client */
void pw_impl_client_destroy(struct pw_impl_client *client);
@ -139,6 +140,8 @@ const struct pw_properties *pw_impl_client_get_properties(struct pw_impl_client
/** Get the context used to create this client */
struct pw_context *pw_impl_client_get_context(struct pw_impl_client *client);
/** Get the protocol used to create this client */
struct pw_protocol *pw_impl_client_get_protocol(struct pw_impl_client *client);
/** Get the client context resource */
struct pw_resource *pw_impl_client_get_core_resource(struct pw_impl_client *client);

View file

@ -99,7 +99,7 @@ struct pw_impl_node_events {
/** Create a new node \memberof pw_impl_node */
struct pw_impl_node *
pw_impl_node_new(struct pw_context *context, /**< the context */
pw_impl_node_new(struct pw_context *context, /**< the context */
struct pw_properties *properties, /**< extra properties */
size_t user_data_size /**< user data size */);

View file

@ -117,7 +117,6 @@ struct pw_impl_client {
struct spa_hook_list listener_list;
struct pw_protocol *protocol; /**< protocol in use */
struct spa_list protocol_link; /**< link in the protocol client_list */
int recv_seq; /**< last received sequence number */
int send_seq; /**< last sender sequence number */