]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
Add support for disabling/enabling a tcp client
authorAndreas Öman <andreas@lonelycoder.com>
Sat, 17 May 2008 07:45:33 +0000 (07:45 +0000)
committerAndreas Öman <andreas@lonelycoder.com>
Sat, 17 May 2008 07:45:33 +0000 (07:45 +0000)
tcp.c
tcp.h

diff --git a/tcp.c b/tcp.c
index 4cf0b9d03eb3d703cef98a2ed8010e64098e0ba5..c5652267ac6b1e3bb6a0e4686957f7c9961d1796 100644 (file)
--- a/tcp.c
+++ b/tcp.c
@@ -526,6 +526,8 @@ tcp_session_peer_resolved(void *aux, struct sockaddr *so, const char *error)
   tcp_session_t *c = aux;
   struct sockaddr_in *si;
   
+  c->tcp_resolver = NULL;
+
   if(error != NULL) {
     syslog(LOG_ERR, "%s: Unable to resolve \"%s\" -- %s",
           c->tcp_name, c->tcp_hostname, error);
@@ -569,7 +571,8 @@ tcp_session_peer_resolved(void *aux, struct sockaddr *so, const char *error)
 static void
 tcp_session_try_connect(tcp_session_t *c)
 {
-  async_resolve(c->tcp_hostname, tcp_session_peer_resolved, c);
+  c->tcp_resolver = 
+    async_resolve(c->tcp_hostname, tcp_session_peer_resolved, c);
 }
 
 
@@ -589,7 +592,7 @@ tcp_client_reconnect_timeout(void *aux, int64_t now)
  */
 void *
 tcp_create_client(const char *hostname, int port, size_t session_size,
-                 const char *name, tcp_callback_t *cb)
+                 const char *name, tcp_callback_t *cb, int enabled)
 {
   tcp_session_t *c = calloc(1, session_size);
 
@@ -597,8 +600,10 @@ tcp_create_client(const char *hostname, int port, size_t session_size,
   c->tcp_name = strdup(name);
   c->tcp_port = port;
   c->tcp_hostname = strdup(hostname);
+  c->tcp_enabled = enabled;
 
-  tcp_session_try_connect(c);
+  if(c->tcp_enabled)
+    tcp_session_try_connect(c);
   return c;
 }
 
@@ -689,6 +694,9 @@ tcp_create_server(int port, size_t session_size, const char *name,
 void
 tcp_destroy_client(tcp_session_t *ses)
 {
+  if(ses->tcp_resolver != NULL)
+    async_resolve_cancel(ses->tcp_resolver);
+
   if(ses->tcp_dispatch_handle != NULL)
     tcp_disconnect(ses, 0);
 
@@ -697,3 +705,29 @@ tcp_destroy_client(tcp_session_t *ses)
   free(ses->tcp_hostname);
   free(ses);
 }
+
+/**
+ *
+ */
+void
+tcp_enable_disable(tcp_session_t *ses, int enabled)
+{
+  if(ses->tcp_enabled == enabled)
+    return;
+
+  ses->tcp_enabled = enabled;
+
+  if(enabled) {
+    tcp_session_try_connect(ses);
+  } else {
+    if(ses->tcp_resolver != NULL) {
+      async_resolve_cancel(ses->tcp_resolver);
+      ses->tcp_resolver = NULL;
+    }
+
+    if(ses->tcp_dispatch_handle != NULL)
+      tcp_disconnect(ses, 0);
+
+    dtimer_disarm(&ses->tcp_timer);
+  }
+}
diff --git a/tcp.h b/tcp.h
index ef95a8f4dbae06d614a89ebbac8116bcc31958d6..95d5054be2cf1a8cd1cebd4da84811d78c0909af 100644 (file)
--- a/tcp.h
+++ b/tcp.h
@@ -64,11 +64,13 @@ typedef struct tcp_session {
 
   /* These are only used when we spawn as a client */
 
+  int tcp_enabled;
   dtimer_t tcp_timer;
   char *tcp_name;
   int tcp_port;
   char *tcp_hostname;
-  
+  void *tcp_resolver;
+
   /* Output queueing */
 
   int tcp_blocked;
@@ -113,8 +115,10 @@ void tcp_qput(tcp_queue_t *tq, const uint8_t *buf, size_t len);
 void tcp_output_queue(tcp_session_t *ses, tcp_queue_t *dst, tcp_queue_t *src);
 
 void *tcp_create_client(const char *hostname, int port, size_t session_size,
-                       const char *name, tcp_callback_t *cb);
+                       const char *name, tcp_callback_t *cb, int enabled);
 
 void tcp_destroy_client(tcp_session_t *ses);
 
+void tcp_enable_disable(tcp_session_t *ses, int enabled);
+
 #endif /* TCP_H_ */