aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPablo Neira Ayuso <pablo@gnumonks.org>2011-11-08 10:10:10 +0100
committerPablo Neira Ayuso <pablo@netfilter.org>2011-11-08 11:13:22 +0100
commitd3ba14648c8fc9d9f853c3aebd1476dc8d46e4d4 (patch)
treec28de3e42888f0833553099be0f226438cc8aae2
parente1a7ede826a0f29c1ca6a4b7ac20ff1d1264e4a2 (diff)
stream: allow to set reconnect timeout
This patch allows to set reconnect timeout. If zero, it will try immediately. If negative, it will skip retrying.
-rw-r--r--include/osmocom/netif/stream.h1
-rw-r--r--src/stream.c27
2 files changed, 21 insertions, 7 deletions
diff --git a/include/osmocom/netif/stream.h b/include/osmocom/netif/stream.h
index c669011..a43516e 100644
--- a/include/osmocom/netif/stream.h
+++ b/include/osmocom/netif/stream.h
@@ -31,6 +31,7 @@ struct osmo_stream_client_conn;
void osmo_stream_client_conn_set_addr(struct osmo_stream_client_conn *link, const char *addr);
void osmo_stream_client_conn_set_port(struct osmo_stream_client_conn *link, uint16_t port);
void osmo_stream_client_conn_set_data(struct osmo_stream_client_conn *link, void *data);
+void osmo_stream_client_conn_set_reconnect_timeout(struct osmo_stream_client_conn *link, int timeout);
void *osmo_stream_client_conn_get_data(struct osmo_stream_client_conn *link);
struct osmo_fd *osmo_stream_client_conn_get_ofd(struct osmo_stream_client_conn *link);
void osmo_stream_client_conn_set_connect_cb(struct osmo_stream_client_conn *link, int (*connect_cb)(struct osmo_stream_client_conn *link));
diff --git a/src/stream.c b/src/stream.c
index 6aa85e1..46c1046 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -44,16 +44,22 @@ struct osmo_stream_client_conn {
int (*write_cb)(struct osmo_stream_client_conn *link);
void *data;
int flags;
+ int reconnect_timeout;
};
void osmo_stream_client_conn_close(struct osmo_stream_client_conn *link);
-static void osmo_stream_client_retry(struct osmo_stream_client_conn *link)
+static void osmo_stream_client_reconnect(struct osmo_stream_client_conn *link)
{
+ if (link->reconnect_timeout < 0) {
+ LOGP(DLINP, LOGL_DEBUG, "not reconnecting, disabled.\n");
+ return;
+ }
LOGP(DLINP, LOGL_DEBUG, "connection closed\n");
osmo_stream_client_conn_close(link);
- LOGP(DLINP, LOGL_DEBUG, "retrying in 5 seconds...\n");
- osmo_timer_schedule(&link->timer, 5, 0);
+ LOGP(DLINP, LOGL_DEBUG, "retrying in %d seconds...\n",
+ link->reconnect_timeout);
+ osmo_timer_schedule(&link->timer, link->reconnect_timeout, 0);
link->state = STREAM_CLIENT_LINK_STATE_CONNECTING;
}
@@ -95,7 +101,7 @@ static int osmo_stream_client_write(struct osmo_stream_client_conn *link)
ret = send(link->ofd.fd, msg->data, msg->len, 0);
if (ret < 0) {
if (errno == EPIPE || errno == ENOTCONN) {
- osmo_stream_client_retry(link);
+ osmo_stream_client_reconnect(link);
}
LOGP(DLINP, LOGL_ERROR, "error to send\n");
}
@@ -113,7 +119,7 @@ static int osmo_stream_client_fd_cb(struct osmo_fd *ofd, unsigned int what)
case STREAM_CLIENT_LINK_STATE_CONNECTING:
ret = getsockopt(ofd->fd, SOL_SOCKET, SO_ERROR, &error, &len);
if (ret >= 0 && error > 0) {
- osmo_stream_client_retry(link);
+ osmo_stream_client_reconnect(link);
return 0;
}
ofd->when &= ~BSC_FD_WRITE;
@@ -156,6 +162,7 @@ struct osmo_stream_client_conn *osmo_stream_client_conn_create(void *ctx)
link->state = STREAM_CLIENT_LINK_STATE_CONNECTING;
link->timer.cb = link_timer_cb;
link->timer.data = link;
+ link->reconnect_timeout = 5; /* default is 5 seconds. */
INIT_LLIST_HEAD(&link->tx_queue);
return link;
@@ -177,6 +184,12 @@ osmo_stream_client_conn_set_port(struct osmo_stream_client_conn *link,
link->flags |= OSMO_STREAM_CLIENT_F_RECONFIG;
}
+void osmo_stream_client_conn_set_reconnect_timeout(
+ struct osmo_stream_client_conn *link, int timeout)
+{
+ link->reconnect_timeout = timeout;
+}
+
void
osmo_stream_client_conn_set_data(struct osmo_stream_client_conn *link,
void *data)
@@ -272,11 +285,11 @@ int osmo_stream_client_conn_recv(struct osmo_stream_client_conn *link,
LOGP(DLINP, LOGL_ERROR,
"lost connection with server\n");
}
- osmo_stream_client_retry(link);
+ osmo_stream_client_reconnect(link);
return ret;
} else if (ret == 0) {
LOGP(DLINP, LOGL_ERROR, "connection closed with server\n");
- osmo_stream_client_retry(link);
+ osmo_stream_client_reconnect(link);
return ret;
}
msgb_put(msg, ret);