Ejemplo n.º 1
0
static gboolean
gst_tcp_server_src_stop (GstBaseSrc * bsrc)
{
  GstTCPServerSrc *src = GST_TCP_SERVER_SRC (bsrc);
  GError *err = NULL;

  if (src->client_socket) {
    GST_DEBUG_OBJECT (src, "closing socket");

    if (!g_socket_close (src->client_socket, &err)) {
      GST_ERROR_OBJECT (src, "Failed to close socket: %s", err->message);
      g_clear_error (&err);
    }
    g_object_unref (src->client_socket);
    src->client_socket = NULL;
  }

  if (src->server_socket) {
    GST_DEBUG_OBJECT (src, "closing socket");

    if (!g_socket_close (src->server_socket, &err)) {
      GST_ERROR_OBJECT (src, "Failed to close socket: %s", err->message);
      g_clear_error (&err);
    }
    g_object_unref (src->server_socket);
    src->server_socket = NULL;
  }

  GST_OBJECT_FLAG_UNSET (src, GST_TCP_SERVER_SRC_OPEN);

  return TRUE;
}
Ejemplo n.º 2
0
static gboolean fusion_tls_output_stream_real_close (GOutputStream* base, GCancellable* cancellable, GError** error) {
	FusionTLSOutputStream * self;
	gboolean result = FALSE;
	GError * _inner_error_ = NULL;
	self = (FusionTLSOutputStream*) base;
	{
		GSocket* _tmp0_ = NULL;
		_tmp0_ = self->priv->socket;
		g_socket_close (_tmp0_, &_inner_error_);
		if (_inner_error_ != NULL) {
			goto __catch5_g_error;
		}
		result = TRUE;
		return result;
	}
	goto __finally5;
	__catch5_g_error:
	{
		GError* e = NULL;
		e = _inner_error_;
		_inner_error_ = NULL;
		result = FALSE;
		_g_error_free0 (e);
		return result;
	}
	__finally5:
	g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
	g_clear_error (&_inner_error_);
	return FALSE;
}
Ejemplo n.º 3
0
void
kms_sctp_connection_close (KmsSCTPConnection * conn)
{
  GError *err = NULL;

  g_return_if_fail (conn != NULL);

  if (conn->socket == NULL)
    return;

  if (g_socket_is_closed (conn->socket)) {
    GST_DEBUG ("Socket is already closed");
    return;
  }

  if (!g_socket_shutdown (conn->socket, TRUE, TRUE, &err)) {
    GST_DEBUG ("%s", err->message);
    g_clear_error (&err);
  }

  GST_DEBUG ("Closing socket");

  if (!g_socket_close (conn->socket, &err)) {
    GST_ERROR ("Failed to close socket %p: %s", conn->socket, err->message);
    g_clear_error (&err);
  }
}
Ejemplo n.º 4
0
static int remote_disconnect(void* connectiondata) {
	g_message("disconnecting remote socket");
	GSocket* sock = (GSocket*) connectiondata;
	g_socket_close(sock, NULL);
	g_object_unref(sock);
	return 0;
}
Ejemplo n.º 5
0
static void
gst_net_client_clock_finalize (GObject * object)
{
  GstNetClientClock *self = GST_NET_CLIENT_CLOCK (object);

  if (self->priv->thread) {
    gst_net_client_clock_stop (self);
  }

  g_free (self->priv->address);
  self->priv->address = NULL;

  if (self->priv->servaddr != NULL) {
    g_object_unref (self->priv->servaddr);
    self->priv->servaddr = NULL;
  }

  if (self->priv->socket != NULL) {
    g_socket_close (self->priv->socket, NULL);
    g_object_unref (self->priv->socket);
    self->priv->socket = NULL;
  }

  G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_net_client_internal_clock_finalize (GObject * object)
{
  GstNetClientInternalClock *self = GST_NET_CLIENT_INTERNAL_CLOCK (object);

  if (self->thread) {
    gst_net_client_internal_clock_stop (self);
  }

  g_free (self->address);
  self->address = NULL;

  if (self->servaddr != NULL) {
    g_object_unref (self->servaddr);
    self->servaddr = NULL;
  }

  if (self->socket != NULL) {
    if (!g_socket_close (self->socket, NULL))
      GST_ERROR_OBJECT (self, "Failed to close socket");
    g_object_unref (self->socket);
    self->socket = NULL;
  }

  g_warn_if_fail (self->busses == NULL);

  G_OBJECT_CLASS (gst_net_client_internal_clock_parent_class)->finalize
      (object);
}
static void
g_network_monitor_netlink_finalize (GObject *object)
{
  GNetworkMonitorNetlink *nl = G_NETWORK_MONITOR_NETLINK (object);

  if (nl->priv->sock)
    {
      g_socket_close (nl->priv->sock, NULL);
      g_object_unref (nl->priv->sock);
    }

  if (nl->priv->source)
    {
      g_source_destroy (nl->priv->source);
      g_source_unref (nl->priv->source);
    }

  if (nl->priv->dump_source)
    {
      g_source_destroy (nl->priv->dump_source);
      g_source_unref (nl->priv->dump_source);
    }

  G_OBJECT_CLASS (g_network_monitor_netlink_parent_class)->finalize (object);
}
Ejemplo n.º 8
0
static void
gst_multiudpsink_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstMultiUDPSink *udpsink;

  udpsink = GST_MULTIUDPSINK (object);

  switch (prop_id) {
    case PROP_SOCKET:
      if (udpsink->socket != NULL && udpsink->socket != udpsink->used_socket &&
          udpsink->close_socket) {
        GError *err = NULL;

        if (!g_socket_close (udpsink->socket, &err)) {
          GST_ERROR ("failed to close socket %p: %s", udpsink->socket,
              err->message);
          g_clear_error (&err);
        }
      }
      if (udpsink->socket)
        g_object_unref (udpsink->socket);
      udpsink->socket = g_value_dup_object (value);
      GST_DEBUG_OBJECT (udpsink, "setting socket to %p", udpsink->socket);
      break;
    case PROP_CLOSE_SOCKET:
      udpsink->close_socket = g_value_get_boolean (value);
      break;
    case PROP_CLIENTS:
      gst_multiudpsink_set_clients_string (udpsink, g_value_get_string (value));
      break;
    case PROP_AUTO_MULTICAST:
      udpsink->auto_multicast = g_value_get_boolean (value);
      break;
    case PROP_TTL:
      udpsink->ttl = g_value_get_int (value);
      break;
    case PROP_TTL_MC:
      udpsink->ttl_mc = g_value_get_int (value);
      break;
    case PROP_LOOP:
      udpsink->loop = g_value_get_boolean (value);
      break;
    case PROP_QOS_DSCP:
      udpsink->qos_dscp = g_value_get_int (value);
      gst_multiudpsink_setup_qos_dscp (udpsink);
      break;
    case PROP_SEND_DUPLICATES:
      udpsink->send_duplicates = g_value_get_boolean (value);
      break;
    case PROP_BUFFER_SIZE:
      udpsink->buffer_size = g_value_get_int (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}
Ejemplo n.º 9
0
/* system or coroutine context */
static void channel_disconnect(SpiceChannel *channel)
{
    spice_channel *c = SPICE_CHANNEL_GET_PRIVATE(channel);

    g_return_if_fail(c != NULL);

    if (c->state == SPICE_CHANNEL_STATE_UNCONNECTED) {
        return;
    }

    c->has_error = TRUE; /* break the loop */

    if (c->connect_delayed_id) {
        g_source_remove(c->connect_delayed_id);
        c->connect_delayed_id = 0;
    }

    spice_openssl_verify_free(c->sslverify);
    c->sslverify = NULL;

    if (c->ssl) {
        SSL_free(c->ssl);
        c->ssl = NULL;
    }

    if (c->ctx) {
        SSL_CTX_free(c->ctx);
        c->ctx = NULL;
    }

    if (c->sock) {
        g_socket_close(c->sock, NULL);
        g_object_unref(c->sock);
        c->sock = NULL;
    }
    c->fd = -1;

    free(c->peer_msg);
    c->peer_msg = NULL;
    c->peer_pos = 0;

    if (c->xmit_buffer) {
        g_free(c->xmit_buffer);
        c->xmit_buffer = NULL;
        c->xmit_buffer_size = 0;
        c->xmit_buffer_capacity = 0;
    }

    g_array_set_size(c->remote_common_caps, 0);
    g_array_set_size(c->remote_caps, 0);
    g_array_set_size(c->common_caps, 0);
    g_array_set_size(c->caps, 0);

    if (c->state == SPICE_CHANNEL_STATE_READY)
        emit_main_context(channel, SPICE_CHANNEL_EVENT, SPICE_CHANNEL_CLOSED);
    c->state = SPICE_CHANNEL_STATE_UNCONNECTED;
}
Ejemplo n.º 10
0
/**
 * gst_switch_server_output_client_socket_removed:
 *
 * Invoked when a client socket is removed on the output port. The socket
 * is required to be freed manually here.
 */
static void
gst_switch_server_output_client_socket_removed (GstElement * element,
    GSocket * socket, GstSwitchServer * srv)
{
  g_return_if_fail (G_IS_SOCKET (socket));

  //INFO ("client-socket-removed: %d", g_socket_get_fd (socket));

  g_socket_close (socket, NULL);
}
Ejemplo n.º 11
0
/**
 * @param rec The GstRecorder instance.
 * @param element
 * @param socket
 * @memberof GstRecorder
 *
 * Invoked when the client socket on the encoding out port is closed. We need
 * to manually close the socket to avoid FD leaks.
 */
static void
gst_recorder_client_socket_removed (GstElement * element,
    GSocket * socket, GstRecorder * rec)
{
  g_return_if_fail (G_IS_SOCKET (socket));

  INFO ("client-socket-removed: %d", g_socket_get_fd (socket));

  g_socket_close (socket, NULL);
}
Ejemplo n.º 12
0
//Bfoadcast ivy msgs to clients
void broadcast_to_clients () {

  int i;

  if (uTCP) {
    //broadcast using tcp connection
    GError *error = NULL;

    for (i = 0; i < MAXCLIENT; i++) {
      if (ConnectedClients[i].used > 0) {
        GOutputStream * ostream = g_io_stream_get_output_stream (ConnectedClients[i].ClientTcpData);
        g_output_stream_write(ostream, ivybuffer, strlen(ivybuffer), NULL, &error);
      }

    }
    return;
  }

  i=0;
  for (i = 0; i < MAXCLIENT; i++) {
    if (ConnectedClients[i].used > 0) {

      //Send data
      GInetAddress *udpAddress;
      GSocketAddress *udpSocketAddress;
      GSocket *udpSocket;

      udpAddress = g_inet_address_new_from_string(ConnectedClients[i].client_ip);

      udpSocketAddress = g_inet_socket_address_new(udpAddress, udp_port);

      udpSocket = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, 17, NULL);

      if (g_socket_connect(udpSocket, udpSocketAddress, NULL, NULL) == FALSE) {
        printf("App Server: stg wrong with socket connect\n");
        fflush(stdout);
      }
      if (g_socket_send(udpSocket, ivybuffer, strlen(ivybuffer) , NULL, NULL) < 0) {
        printf("App Server: stg wrong with send func\n");
        fflush(stdout);
      }
      if (g_socket_close(udpSocket, NULL) == FALSE) {
        printf("App Server: stg wrong with socket close\n");
        fflush(stdout);
      }
      //Unref objects
      g_object_unref(udpAddress);
      g_object_unref(udpSocketAddress);
      g_object_unref(udpSocket);
    }
  }

}
Ejemplo n.º 13
0
static gboolean
gst_multiudpsink_stop (GstBaseSink * bsink)
{
  GstMultiUDPSink *udpsink;

  udpsink = GST_MULTIUDPSINK (bsink);

  if (udpsink->used_socket) {
    if (udpsink->close_socket || !udpsink->external_socket) {
      GError *err = NULL;

      if (!g_socket_close (udpsink->used_socket, &err)) {
        GST_ERROR_OBJECT (udpsink, "Failed to close socket: %s", err->message);
        g_clear_error (&err);
      }
    }

    g_object_unref (udpsink->used_socket);
    udpsink->used_socket = NULL;
  }

  if (udpsink->used_socket_v6) {
    if (udpsink->close_socket || !udpsink->external_socket) {
      GError *err = NULL;

      if (!g_socket_close (udpsink->used_socket_v6, &err)) {
        GST_ERROR_OBJECT (udpsink, "Failed to close socket: %s", err->message);
        g_clear_error (&err);
      }
    }

    g_object_unref (udpsink->used_socket_v6);
    udpsink->used_socket_v6 = NULL;
  }

  return TRUE;
}
Ejemplo n.º 14
0
static void
tear_down_mock_xdg_runtime_dir (void)
{
  GError *error = NULL;

  g_socket_close (mock_bus, &error);
  g_assert_no_error (error);

  if (g_unlink (mock_bus_path) < 0)
    g_error ("g_unlink(\"%s\"): %s", mock_bus_path, g_strerror (errno));

  if (g_rmdir (tmpdir) < 0)
    g_error ("g_rmdir(\"%s\"): %s", tmpdir, g_strerror (errno));

  g_clear_object (&mock_bus);
  g_clear_pointer (&mock_bus_path, g_free);
}
Ejemplo n.º 15
0
NiceSocket *
nice_tcp_passive_socket_accept (NiceSocket *sock)
{
  union {
    struct sockaddr_storage storage;
    struct sockaddr addr;
  } name;
  TcpPassivePriv *priv = sock->priv;
  GSocket *gsock = NULL;
  GSocketAddress *gaddr;
  NiceAddress remote_addr;
  NiceSocket *new_socket = NULL;

  gsock = g_socket_accept (sock->fileno, NULL, NULL);

  if (gsock == NULL) {
    return NULL;
  }

  /* GSocket: All socket file descriptors are set to be close-on-exec. */
  g_socket_set_blocking (gsock, false);

  gaddr = g_socket_get_remote_address (gsock, NULL);
  if (gaddr == NULL ||
      !g_socket_address_to_native (gaddr, &name.addr, sizeof (name), NULL)) {
    g_socket_close (gsock, NULL);
    g_object_unref (gsock);
    return NULL;
  }
  g_object_unref (gaddr);

  nice_address_set_from_sockaddr (&remote_addr, &name.addr);

  new_socket = nice_tcp_bsd_socket_new_from_gsock (priv->context, gsock,
      &sock->addr, &remote_addr, TRUE);
  g_object_unref (gsock);

  if (new_socket) {
    NiceAddress *key = nice_address_dup (&remote_addr);

    nice_socket_set_writable_callback (new_socket, _child_writable_cb, sock);
    g_hash_table_insert (priv->connections, key, new_socket);
  }
  return new_socket;
}
Ejemplo n.º 16
0
GInetAddress *interface_getConfiguredAddress(struct node *n)
{
    GInetAddress *ret = NULL;
    GSocket *s = n->ubnetd;

    if( s != NULL ){
        if( g_socket_condition_check(s, G_IO_IN) & G_IO_IN ){
            gchar buf[1];
            g_socket_receive(s,buf,1,NULL,NULL);
            if( buf[0] == 'A' )
                ret = n->netadr;
            g_socket_close(s, NULL);
            g_object_unref(s);
            n->ubnetd = NULL;
        }
    }
    return ret;
}
Ejemplo n.º 17
0
/**
 * g_socket_listener_close:
 * @listener: a #GSocketListener
 *
 * Closes all the sockets in the listener.
 *
 * Since: 2.22
 */
void
g_socket_listener_close (GSocketListener *listener)
{
  GSocket *socket;
  int i;

  g_return_if_fail (G_IS_SOCKET_LISTENER (listener));

  if (listener->priv->closed)
    return;

  for (i = 0; i < listener->priv->sockets->len; i++)
    {
      socket = listener->priv->sockets->pdata[i];
      g_socket_close (socket, NULL);
    }
  listener->priv->closed = TRUE;
}
Ejemplo n.º 18
0
static void net_removeUDPSocket(struct node *n, guint classid)
{
    GError *err = NULL;
    syslog(LOG_DEBUG,"net_removeSockets: Closing udp socket\n");
    g_source_destroy(n->udpsockets[classid].source);
    g_source_unref(n->udpsockets[classid].source);

    //gboolean rc = g_socket_shutdown(n->udp, FALSE, FALSE, &err);
    gboolean rc = g_socket_close(n->udpsockets[classid].socket, &err);
    if( rc  == TRUE ){
        syslog(LOG_DEBUG,"success\n");
    }else{
        //TODO: log error
        syslog(LOG_WARNING, "error in g_socket_close: %s\n", err->message);
        g_error_free(err);
    }
    g_object_unref(n->udpsockets[classid].socket);
}
Ejemplo n.º 19
0
static gboolean
gst_udpsrc_close (GstUDPSrc * src)
{
  GST_DEBUG ("closing sockets");

  if (src->used_socket) {
    if (src->auto_multicast
        &&
        g_inet_address_get_is_multicast (g_inet_socket_address_get_address
            (src->addr))) {
      GError *err = NULL;

      GST_DEBUG_OBJECT (src, "leaving multicast group %s", src->address);

      if (!g_socket_leave_multicast_group (src->used_socket,
              g_inet_socket_address_get_address (src->addr), FALSE,
              src->multi_iface, &err)) {
        GST_ERROR_OBJECT (src, "Failed to leave multicast group: %s",
            err->message);
        g_clear_error (&err);
      }
    }

    if (src->close_socket || !src->external_socket) {
      GError *err = NULL;
      if (!g_socket_close (src->used_socket, &err)) {
        GST_ERROR_OBJECT (src, "Failed to close socket: %s", err->message);
        g_clear_error (&err);
      }
    }

    g_object_unref (src->used_socket);
    src->used_socket = NULL;
    g_object_unref (src->addr);
    src->addr = NULL;
  }

  gst_udpsrc_reset_memory_allocator (src);

  gst_udpsrc_free_cancellable (src);

  return TRUE;
}
Ejemplo n.º 20
0
static gboolean
gst_udpsrc_stop (GstBaseSrc * bsrc)
{
  GstUDPSrc *src;

  src = GST_UDPSRC (bsrc);

  GST_DEBUG ("stopping, closing sockets");

  if (src->used_socket) {
    if (src->auto_multicast
        &&
        g_inet_address_get_is_multicast (g_inet_socket_address_get_address
            (src->addr))) {
      GError *err = NULL;

      GST_DEBUG_OBJECT (src, "leaving multicast group %s", src->host);

      if (!g_socket_leave_multicast_group (src->used_socket,
              g_inet_socket_address_get_address (src->addr), FALSE,
              src->multi_iface, &err)) {
        GST_ERROR_OBJECT (src, "Failed to leave multicast group: %s",
            err->message);
        g_clear_error (&err);
      }
    }

    if (src->close_socket || !src->external_socket) {
      GError *err = NULL;
      if (!g_socket_close (src->used_socket, &err)) {
        GST_ERROR_OBJECT (src, "Failed to close socket: %s", err->message);
        g_clear_error (&err);
      }
    }

    g_object_unref (src->used_socket);
    src->used_socket = NULL;
    g_object_unref (src->addr);
    src->addr = NULL;
  }

  return TRUE;
}
static void
gst_sctp_base_sink_destroy_socket (GstSCTPBaseSink * self)
{
  GError *err = NULL;

  if (!GST_OBJECT_FLAG_IS_SET (self, GST_SCTP_BASE_SINK_OPEN))
    return;

  if (self->priv->socket != NULL) {
    GST_DEBUG_OBJECT (self, "closing socket");

    if (!g_socket_close (self->priv->socket, &err)) {
      GST_ERROR_OBJECT (self, "Failed to close socket: %s", err->message);
      g_clear_error (&err);
    }
    g_clear_object (&self->priv->socket);
  }

  GST_OBJECT_FLAG_UNSET (self, GST_SCTP_BASE_SINK_OPEN);
}
Ejemplo n.º 22
0
gint interface_createAddress(struct node *n) //GInetAddress *addr)
{
    GError * e = NULL;
    
    GInetAddress *lo = g_inet_address_new_loopback(
                            G_SOCKET_FAMILY_IPV6);
    GSocketAddress *sa = g_inet_socket_address_new(
                            lo,
                            42);
    
    GSocket *s = n->ubnetd;
    if( s != NULL ){
        g_socket_close(s,NULL);
        g_object_unref(s);
    }

    s = g_socket_new(G_SOCKET_FAMILY_IPV6,
                            G_SOCKET_TYPE_STREAM,
                            G_SOCKET_PROTOCOL_TCP,
                            &e);
    if( e != NULL ){
        syslog(LOG_ERR,"interface_pushAddress: Error while creating socket: %s\n", e->message);
        g_error_free(e);
        return -1;
    }

    g_socket_connect(s, sa, NULL, &e);
    if( e != NULL ){
        syslog(LOG_ERR,"interface_pushAddress: Error while connecting: %s\n", e->message);
        g_error_free(e);
        return -1;
    }
    
    g_socket_send(s,"A",1,NULL,NULL);
    gchar *bytes = (gchar*)g_inet_address_to_bytes(n->netadr);
    g_socket_send(s,bytes,16,NULL,NULL);
    n->ubnetd = s;
    //gchar buf[1];
    //g_socket_receive(s,buf,1,NULL,NULL);
    return 0;
}
Ejemplo n.º 23
0
static void
gssdp_socket_source_dispose (GObject *object)
{
        GSSDPSocketSource *self;

        self = GSSDP_SOCKET_SOURCE (object);

        if (self->priv->source != NULL) {
                g_source_unref (self->priv->source);
                g_source_destroy (self->priv->source);
                self->priv->source = NULL;
        }

        if (self->priv->socket != NULL) {
                g_socket_close (self->priv->socket, NULL);
                g_object_unref (self->priv->socket);
                self->priv->socket = NULL;
        }

        G_OBJECT_CLASS (gssdp_socket_source_parent_class)->dispose (object);
}
Ejemplo n.º 24
0
static void
g_socket_input_stream_finalize (GObject *object)
{
  GSocketInputStreamX *stream = G_SOCKET_INPUT_STREAM (object);

  if (stream->priv->socket) {
    GError *error = NULL;
    /*
    g_print ("%s:%d: %s, %d\n", __FILE__, __LINE__, __FUNCTION__,
	g_socket_get_fd (stream->priv->socket));
    */
    g_socket_close (stream->priv->socket, &error);
    if (error) {
      //ERROR ("%s", error->message);
    }
    g_object_unref (stream->priv->socket);
  }

  if (G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize)
    (*G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize) (object);
}
Ejemplo n.º 25
0
static void
socket_close (NiceSocket *sock)
{
  TcpPriv *priv = sock->priv;

  if (sock->fileno) {
    g_socket_close (sock->fileno, NULL);
    g_object_unref (sock->fileno);
    sock->fileno = NULL;
  }
  if (priv->io_source) {
    g_source_destroy (priv->io_source);
    g_source_unref (priv->io_source);
  }
  g_queue_foreach (&priv->send_queue, (GFunc) free_to_be_sent, NULL);
  g_queue_clear (&priv->send_queue);

  if (priv->context)
    g_main_context_unref (priv->context);

  g_slice_free(TcpPriv, sock->priv);
}
Ejemplo n.º 26
0
static gboolean
gst_tcp_mix_src_stop (GstTCPMixSrc * src, GstTCPMixSrcPad * pad)
{
  GError *err = NULL;
  GList *item;

  GST_OBJECT_LOCK (src);
  GST_DEBUG_OBJECT (src, "Closing client sockets");
  for (item = GST_ELEMENT_PADS (src); item; item = g_list_next (item)) {
    GstPad *p = GST_PAD (item->data);
    if (GST_PAD_IS_SRC (p)) {
      gst_tcp_mix_src_pad_reset (GST_TCP_MIX_SRC_PAD (p));
    }
  }
  GST_OBJECT_UNLOCK (src);

  if (src->server_socket) {
    GST_DEBUG_OBJECT (src, "Closing server socket");

    if (!g_socket_close (src->server_socket, &err)) {
      GST_ERROR_OBJECT (src, "Failed to close socket: %s", err->message);
      g_clear_error (&err);
    }

    g_object_unref (src->server_socket);
    src->server_socket = NULL;

    gst_tcp_mix_src_stop_acceptor (src);

    g_atomic_int_set (&src->bound_port, 0);
    g_object_notify (G_OBJECT (src), "bound-port");
  }

  GST_OBJECT_FLAG_UNSET (src, GST_TCP_MIX_SRC_OPEN);

  return TRUE;
}
Ejemplo n.º 27
0
//Bfoadcast ivy msgs to clients
void broadcast_to_clients () {

  int i;
  for (i = 0; i < MAXCLIENT; i++) {
    if (ConnectedClients[i].used > 0) {

      //Send data
      GInetAddress *udpAddress;
      GSocketAddress *udpSocketAddress;
      GSocket *udpSocket;

      udpAddress = g_inet_address_new_from_string(ConnectedClients[i].client_ip);

      udpSocketAddress = g_inet_socket_address_new(udpAddress, udp_port);

      udpSocket = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, 17, NULL);

      if (g_socket_connect(udpSocket, udpSocketAddress, NULL, NULL) == FALSE) {
        printf("App Server: stg wrong with socket connect\n");
        fflush(stdout);
      }
      if (g_socket_send(udpSocket, ivybuffer, strlen(ivybuffer) , NULL, NULL) < 0) {
        printf("App Server: stg wrong with send func\n");
        fflush(stdout);
      }
      if (g_socket_close(udpSocket, NULL) == FALSE) {
        printf("App Server: stg wrong with socket close\n");
        fflush(stdout);
      }
      //Unref objects
      g_object_unref(udpAddress);
      g_object_unref(udpSocketAddress);
      g_object_unref(udpSocket);
    }
  }
}
Ejemplo n.º 28
0
int
main (int argc,
      char *argv[])
{
  GSocket *socket, *new_socket, *recv_socket;
  GSocketAddress *src_address;
  GSocketAddress *address;
  GSocketType socket_type;
  GSocketFamily socket_family;
  GError *error = NULL;
  GOptionContext *context;
  GCancellable *cancellable;
  char *display_addr;
  GTlsCertificate *tlscert = NULL;
  GIOStream *connection;
  GInputStream *istream;
  GOutputStream *ostream;

  g_type_init ();

  context = g_option_context_new (" - Test GSocket server stuff");
  g_option_context_add_main_entries (context, cmd_entries, NULL);
  if (!g_option_context_parse (context, &argc, &argv, &error))
    {
      g_printerr ("%s: %s\n", argv[0], error->message);
      return 1;
    }

  if (unix_socket && argc != 2)
    {
      g_printerr ("%s: %s\n", argv[0], "Need to specify unix socket name");
      return 1;
    }

  if (cancel_timeout)
    {
      GThread *thread;
      cancellable = g_cancellable_new ();
      thread = g_thread_new ("cancel", cancel_thread, cancellable);
      g_thread_unref (thread);
    }
  else
    {
      cancellable = NULL;
    }

  if (tls_cert_file)
    {
      if (use_udp)
	{
	  g_printerr ("DTLS (TLS over UDP) is not supported");
	  return 1;
	}

      tlscert = g_tls_certificate_new_from_file (tls_cert_file, &error);
      if (!tlscert)
	{
	  g_printerr ("Could not read server certificate '%s': %s\n",
		      tls_cert_file, error->message);
	  return 1;
	}
    }

  loop = g_main_loop_new (NULL, FALSE);

  if (use_udp)
    socket_type = G_SOCKET_TYPE_DATAGRAM;
  else
    socket_type = G_SOCKET_TYPE_STREAM;

  if (unix_socket)
    socket_family = G_SOCKET_FAMILY_UNIX;
  else
    socket_family = G_SOCKET_FAMILY_IPV4;

  socket = g_socket_new (socket_family, socket_type, 0, &error);

  if (socket == NULL)
    {
      g_printerr ("%s: %s\n", argv[0], error->message);
      return 1;
    }

  if (non_blocking)
    g_socket_set_blocking (socket, FALSE);

  if (unix_socket)
    {
      src_address = socket_address_from_string (argv[1]);
      if (src_address == NULL)
	{
	  g_printerr ("%s: Could not parse '%s' as unix socket name\n", argv[0], argv[1]);
	  return 1;
	}
    }
  else
    {
      src_address = g_inet_socket_address_new (g_inet_address_new_any (G_SOCKET_FAMILY_IPV4), port);
    }

  if (!g_socket_bind (socket, src_address, !dont_reuse_address, &error))
    {
      g_printerr ("Can't bind socket: %s\n", error->message);
      return 1;
    }
  g_object_unref (src_address);

  if (!use_udp)
    {
      if (!g_socket_listen (socket, &error))
	{
	  g_printerr ("Can't listen on socket: %s\n", error->message);
	  return 1;
	}

      address = g_socket_get_local_address (socket, &error);
      if (!address)
	{
	  g_printerr ("Error getting local address: %s\n",
		      error->message);
	  return 1;
	}
      display_addr = socket_address_to_string (address);
      g_print ("listening on %s...\n", display_addr);
      g_free (display_addr);

      ensure_socket_condition (socket, G_IO_IN, cancellable);
      new_socket = g_socket_accept (socket, cancellable, &error);
      if (!new_socket)
	{
	  g_printerr ("Error accepting socket: %s\n",
		      error->message);
	  return 1;
	}

      if (non_blocking)
	g_socket_set_blocking (new_socket, FALSE);
      if (read_timeout)
	g_socket_set_timeout (new_socket, read_timeout);

      address = g_socket_get_remote_address (new_socket, &error);
      if (!address)
	{
	  g_printerr ("Error getting remote address: %s\n",
		      error->message);
	  return 1;
	}

      display_addr = socket_address_to_string (address);
      g_print ("got a new connection from %s\n", display_addr);
      g_free(display_addr);
      g_object_unref (address);

      recv_socket = new_socket;

      connection = G_IO_STREAM (g_socket_connection_factory_create_connection (recv_socket));
      g_object_unref (new_socket);
    }
  else
    {
      recv_socket = socket;
      connection = NULL;
    }

  if (tlscert)
    {
      GIOStream *tls_conn;

      tls_conn = g_tls_server_connection_new (connection, tlscert, &error);
      if (!tls_conn)
	{
	  g_printerr ("Could not create TLS connection: %s\n",
		      error->message);
	  return 1;
	}

      if (!g_tls_connection_handshake (G_TLS_CONNECTION (tls_conn),
				       cancellable, &error))
	{
	  g_printerr ("Error during TLS handshake: %s\n",
		      error->message);
	  return 1;
       }

      g_object_unref (connection);
      connection = tls_conn;
    }

  if (connection)
    {
      istream = g_io_stream_get_input_stream (connection);
      ostream = g_io_stream_get_output_stream (connection);
    }
  else
    {
      g_assert (use_udp);
      istream = NULL;
      ostream = NULL;
    }

  while (TRUE)
    {
      gchar buffer[4096];
      gssize size;
      gsize to_send;

      if (use_udp)
	{
	  ensure_socket_condition (recv_socket, G_IO_IN, cancellable);
	  size = g_socket_receive_from (recv_socket, &address,
					buffer, sizeof buffer,
					cancellable, &error);
	}
      else
	{
	  ensure_connection_condition (connection, G_IO_IN, cancellable);
	  size = g_input_stream_read (istream,
				      buffer, sizeof buffer,
				      cancellable, &error);
	}

      if (size < 0)
	{
	  g_printerr ("Error receiving from socket: %s\n",
		      error->message);
	  return 1;
	}

      if (size == 0)
	break;

      g_print ("received %" G_GSSIZE_FORMAT " bytes of data", size);
      if (use_udp)
	g_print (" from %s", socket_address_to_string (address));
      g_print ("\n");

      if (verbose)
	g_print ("-------------------------\n"
		 "%.*s\n"
		 "-------------------------\n",
		 (int)size, buffer);

      to_send = size;

#ifdef __QNXNTO__
      if (delay_)
#else
      if (delay)
#endif
	{
#ifdef __QNXNTO__
	  if (verbose)
	    g_print ("delaying %d seconds before response\n", delay_);
	  g_usleep (1000 * 1000 * delay_);
#else
	  if (verbose)
	    g_print ("delaying %d seconds before response\n", delay);
	  g_usleep (1000 * 1000 * delay);
#endif
	}

      while (to_send > 0)
	{
	  if (use_udp)
	    {
	      ensure_socket_condition (recv_socket, G_IO_OUT, cancellable);
	      size = g_socket_send_to (recv_socket, address,
				       buffer, to_send, cancellable, &error);
	    }
	  else
	    {
	      ensure_connection_condition (connection, G_IO_OUT, cancellable);
	      size = g_output_stream_write (ostream,
					    buffer, to_send,
					    cancellable, &error);
	    }

	  if (size < 0)
	    {
	      if (g_error_matches (error,
				   G_IO_ERROR,
				   G_IO_ERROR_WOULD_BLOCK))
		{
		  g_print ("socket send would block, handling\n");
		  g_error_free (error);
		  error = NULL;
		  continue;
		}
	      else
		{
		  g_printerr ("Error sending to socket: %s\n",
			      error->message);
		  return 1;
		}
	    }

	  g_print ("sent %" G_GSSIZE_FORMAT " bytes of data\n", size);

	  if (size == 0)
	    {
	      g_printerr ("Unexpected short write\n");
	      return 1;
	    }

	  to_send -= size;
	}
    }

  g_print ("connection closed\n");

  if (connection)
    {
      if (!g_io_stream_close (connection, NULL, &error))
	{
	  g_printerr ("Error closing connection stream: %s\n",
		      error->message);
	  return 1;
	}
      g_object_unref (connection);
    }

  if (!g_socket_close (socket, &error))
    {
      g_printerr ("Error closing master socket: %s\n",
		  error->message);
      return 1;
    }
  g_object_unref (socket);

  return 0;
}
Ejemplo n.º 29
0
Archivo: main.c Proyecto: dram/configs
int main(void)
{
	GSocket *sock;

	sock = create_socket();
	committed = g_string_new("");

	puts("Emacs-fcitx is ready.");

	while (TRUE) {
		GSource *source;
		GSocket *connection;
		GMainLoop *main_loop;

		if ((connection = g_socket_accept(sock, NULL, NULL)) == NULL) {
			perror("g_socket_accept");
			exit(1);
		}

		puts("Opening connection.");

		client = fcitx_client_new();

		g_signal_connect(client, "connected",
				 G_CALLBACK(connect_cb), NULL);
		g_signal_connect(client, "disconnected",
				 G_CALLBACK(destroy_cb), NULL);
		g_signal_connect(client, "enable-im",
				 G_CALLBACK(enable_im_cb), NULL);
		g_signal_connect(client, "close-im",
				 G_CALLBACK(close_im_cb), NULL);
		g_signal_connect(client, "forward-key",
				 G_CALLBACK(forward_key_cb), NULL);
		g_signal_connect(client, "commit-string",
				 G_CALLBACK(commit_string_cb), NULL);
		g_signal_connect(client,
				 "update-client-side-ui",
				 G_CALLBACK(update_client_side_ui_cb),
				 connection);

		main_loop = g_main_loop_new(NULL, FALSE);

		source = g_socket_create_source(
			connection,
			(GIOCondition)(G_IO_IN | G_IO_HUP | G_IO_ERR), NULL);

		g_source_set_callback(
			source, (GSourceFunc)source_cb, main_loop, NULL);

		g_source_attach(source, NULL);

		g_main_loop_run(main_loop);

		puts("main loop finished.");

		g_socket_close(connection, NULL);

		g_object_unref(client);
		g_source_unref(source);
		g_object_unref(connection);
		g_main_loop_unref(main_loop);
	}

	g_object_unref(sock);
	g_string_free(committed, TRUE);

	return 0;
}
Ejemplo n.º 30
0
static gboolean
on_incoming_message (GSocket      *socket,
                     GIOCondition  condition,
                     gpointer      user_data)
{
  g_debug (G_STRLOC ": %s: socket fd:%d", G_STRFUNC, g_socket_get_fd (socket));

  DasomIM *im = DASOM_IM (user_data);
  dasom_message_unref (im->result->reply);
  im->result->is_dispatched = TRUE;

  if (condition & (G_IO_HUP | G_IO_ERR))
  {
    /* Because two GSource is created over one socket,
     * when G_IO_HUP | G_IO_ERR, callback can run two times.
     * the following code avoid that callback runs two times. */
    GSource *source = g_main_current_source ();

    if (source == im->default_context_source)
      g_source_destroy (im->sockets_context_source);
    else if (source == im->sockets_context_source)
      g_source_destroy (im->default_context_source);

    if (!g_socket_is_closed (socket))
      g_socket_close (socket, NULL);

    im->result->reply    = NULL;

    g_critical (G_STRLOC ": %s: G_IO_HUP | G_IO_ERR", G_STRFUNC);

    return G_SOURCE_REMOVE;
  }

  DasomMessage *message;
  message = dasom_recv_message (socket);
  im->result->reply = message;
  gboolean retval;

  if (G_UNLIKELY (message == NULL))
  {
    g_critical (G_STRLOC ": NULL message");
    return G_SOURCE_CONTINUE;
  }

  switch (message->header->type)
  {
    /* reply */
    case DASOM_MESSAGE_FILTER_EVENT_REPLY:
    case DASOM_MESSAGE_RESET_REPLY:
    case DASOM_MESSAGE_FOCUS_IN_REPLY:
    case DASOM_MESSAGE_FOCUS_OUT_REPLY:
    case DASOM_MESSAGE_SET_SURROUNDING_REPLY:
    case DASOM_MESSAGE_GET_SURROUNDING_REPLY:
    case DASOM_MESSAGE_SET_CURSOR_LOCATION_REPLY:
    case DASOM_MESSAGE_SET_USE_PREEDIT_REPLY:
      break;
    /* signals */
    case DASOM_MESSAGE_PREEDIT_START:
      g_signal_emit_by_name (im, "preedit-start");
      dasom_send_message (socket, DASOM_MESSAGE_PREEDIT_START_REPLY, NULL, 0, NULL);
      break;
    case DASOM_MESSAGE_PREEDIT_END:
      g_signal_emit_by_name (im, "preedit-end");
      dasom_send_message (socket, DASOM_MESSAGE_PREEDIT_END_REPLY, NULL, 0, NULL);
      break;
    case DASOM_MESSAGE_PREEDIT_CHANGED:
      g_free (im->preedit_string);
      im->preedit_string = g_strndup (message->data,
                                      message->header->data_len - 1 - sizeof (gint));
      im->cursor_pos = *(gint *) (message->data +
                                  message->header->data_len - sizeof (gint));
      g_signal_emit_by_name (im, "preedit-changed");
      dasom_send_message (socket,
                          DASOM_MESSAGE_PREEDIT_CHANGED_REPLY,
                          NULL, 0, NULL);
      break;
    case DASOM_MESSAGE_COMMIT:
      dasom_message_ref (message);
      g_signal_emit_by_name (im, "commit", (const gchar *) message->data);
      dasom_message_unref (message);
      dasom_send_message (socket, DASOM_MESSAGE_COMMIT_REPLY, NULL, 0, NULL);
      break;
    case DASOM_MESSAGE_RETRIEVE_SURROUNDING:
      g_signal_emit_by_name (im, "retrieve-surrounding", &retval);
      dasom_send_message (socket, DASOM_MESSAGE_RETRIEVE_SURROUNDING_REPLY,
                          &retval, sizeof (gboolean), NULL);
      break;
    case DASOM_MESSAGE_DELETE_SURROUNDING:
      dasom_message_ref (message);
      g_signal_emit_by_name (im, "delete-surrounding",
                             ((gint *) message->data)[0],
                             ((gint *) message->data)[1], &retval);
      dasom_message_unref (message);
      dasom_send_message (socket, DASOM_MESSAGE_DELETE_SURROUNDING_REPLY,
                          &retval, sizeof (gboolean), NULL);
      break;
    default:
      g_warning (G_STRLOC ": %s: Unknown message type: %d", G_STRFUNC, message->header->type);
      break;
  }

  return G_SOURCE_CONTINUE;
}