aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPablo Neira Ayuso <pablo@gnumonks.org>2011-10-17 12:50:58 +0200
committerPablo Neira Ayuso <pablo@gnumonks.org>2011-10-17 12:51:18 +0200
commit98ec00d8d22fa855710a13aa21cfe1f66114a55b (patch)
tree29b937eff0b85144385df2e128f3ec5b99f24dde
parentc43bb089067ca05af4992fbafe46827a1f0a6e9b (diff)
stream: add osmo_stream_*_conn_recv
Like c43bb089067ca05af4992fbafe46827a1f0a6e9b but for stream sockets.
-rw-r--r--examples/stream-client.c15
-rw-r--r--examples/stream-server.c15
-rw-r--r--include/osmocom/netif/stream.h6
-rw-r--r--src/stream.c101
4 files changed, 84 insertions, 53 deletions
diff --git a/examples/stream-client.c b/examples/stream-client.c
index 7f95800..8836223 100644
--- a/examples/stream-client.c
+++ b/examples/stream-client.c
@@ -43,9 +43,22 @@ static int connect_cb(struct osmo_stream_client_conn *conn)
return 0;
}
-static int read_cb(struct osmo_stream_client_conn *conn, struct msgb *msg)
+static int read_cb(struct osmo_stream_client_conn *conn)
{
+ struct msgb *msg;
+
LOGP(DSTREAMTEST, LOGL_NOTICE, "received message from stream\n");
+
+ msg = msgb_alloc(1024, "STREAMCLIENT/test");
+ if (msg == NULL) {
+ LOGP(DSTREAMTEST, LOGL_ERROR, "cannot allocate message\n");
+ return 0;
+ }
+ if (osmo_stream_client_conn_recv(conn, msg) < 0) {
+ LOGP(DSTREAMTEST, LOGL_ERROR, "cannot receive message\n");
+ return 0;
+ }
+ msgb_free(msg);
return 0;
}
diff --git a/examples/stream-server.c b/examples/stream-server.c
index d8eb380..ec9f903 100644
--- a/examples/stream-server.c
+++ b/examples/stream-server.c
@@ -41,9 +41,22 @@ void sighandler(int foo)
exit(EXIT_SUCCESS);
}
-int read_cb(struct osmo_stream_server_conn *conn, struct msgb *msg)
+int read_cb(struct osmo_stream_server_conn *conn)
{
+ struct msgb *msg;
+
LOGP(DSTREAMTEST, LOGL_NOTICE, "received message from stream\n");
+
+ msg = msgb_alloc(1024, "STREAMSERVER/test");
+ if (msg == NULL) {
+ LOGP(DSTREAMTEST, LOGL_ERROR, "cannot allocate message\n");
+ return 0;
+ }
+ if (osmo_stream_server_conn_recv(conn, msg) < 0) {
+ LOGP(DSTREAMTEST, LOGL_ERROR, "cannot receive message\n");
+ return 0;
+ }
+ msgb_free(msg);
return 0;
}
diff --git a/include/osmocom/netif/stream.h b/include/osmocom/netif/stream.h
index 4363a67..21483f2 100644
--- a/include/osmocom/netif/stream.h
+++ b/include/osmocom/netif/stream.h
@@ -17,11 +17,12 @@ void osmo_stream_server_link_close(struct osmo_stream_server_link *link);
struct osmo_stream_server_conn;
-struct osmo_stream_server_conn *osmo_stream_server_conn_create(void *ctx, struct osmo_stream_server_link *link, int fd, int (*cb)(struct osmo_stream_server_conn *conn, struct msgb *msg), int (*closed_cb)(struct osmo_stream_server_conn *conn), void *data);
+struct osmo_stream_server_conn *osmo_stream_server_conn_create(void *ctx, struct osmo_stream_server_link *link, int fd, int (*cb)(struct osmo_stream_server_conn *conn), int (*closed_cb)(struct osmo_stream_server_conn *conn), void *data);
void *osmo_stream_server_conn_get_data(struct osmo_stream_server_conn *conn);
void osmo_stream_server_conn_destroy(struct osmo_stream_server_conn *conn);
void osmo_stream_server_conn_send(struct osmo_stream_server_conn *conn, struct msgb *msg);
+int osmo_stream_server_conn_recv(struct osmo_stream_server_conn *conn, struct msgb *msg);
struct osmo_stream_client_conn;
@@ -30,7 +31,7 @@ void osmo_stream_client_conn_set_port(struct osmo_stream_client_conn *link, uint
void osmo_stream_client_conn_set_data(struct osmo_stream_client_conn *link, void *data);
void *osmo_stream_client_conn_get_data(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));
-void osmo_stream_client_conn_set_read_cb(struct osmo_stream_client_conn *link, int (*read_cb)(struct osmo_stream_client_conn *link, struct msgb *msgb));
+void osmo_stream_client_conn_set_read_cb(struct osmo_stream_client_conn *link, int (*read_cb)(struct osmo_stream_client_conn *link));
struct osmo_stream_client_conn *osmo_stream_client_conn_create(void *ctx);
void osmo_stream_client_conn_destroy(struct osmo_stream_client_conn *link);
@@ -39,5 +40,6 @@ int osmo_stream_client_conn_open(struct osmo_stream_client_conn *link);
void osmo_stream_client_conn_close(struct osmo_stream_client_conn *link);
void osmo_stream_client_conn_send(struct osmo_stream_client_conn *link, struct msgb *msg);
+int osmo_stream_client_conn_recv(struct osmo_stream_client_conn *conn, struct msgb *msg);
#endif
diff --git a/src/stream.c b/src/stream.c
index aa2e7b3..c4ff89c 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -40,7 +40,7 @@ struct osmo_stream_client_conn {
const char *addr;
uint16_t port;
int (*connect_cb)(struct osmo_stream_client_conn *link);
- int (*read_cb)(struct osmo_stream_client_conn *link, struct msgb *msg);
+ int (*read_cb)(struct osmo_stream_client_conn *link);
int (*write_cb)(struct osmo_stream_client_conn *link);
void *data;
int flags;
@@ -65,31 +65,10 @@ void osmo_stream_client_conn_close(struct osmo_stream_client_conn *link)
static void osmo_stream_client_read(struct osmo_stream_client_conn *link)
{
- struct msgb *msg;
- int ret;
-
LOGP(DLINP, LOGL_DEBUG, "message received\n");
- msg = msgb_alloc(1200, "LAPD/client");
- if (!msg) {
- LOGP(DLINP, LOGL_ERROR, "cannot allocate room for message\n");
- return;
- }
- ret = recv(link->ofd.fd, msg->data, msg->data_len, 0);
- if (ret < 0) {
- if (errno == EPIPE || errno == ECONNRESET) {
- LOGP(DLINP, LOGL_ERROR, "lost connection with server\n");
- }
- osmo_stream_client_retry(link);
- return;
- } else if (ret == 0) {
- LOGP(DLINP, LOGL_ERROR, "connection closed with server\n");
- osmo_stream_client_retry(link);
- return;
- }
- msgb_put(msg, ret);
if (link->read_cb)
- link->read_cb(link, msg);
+ link->read_cb(link);
}
static int osmo_stream_client_write(struct osmo_stream_client_conn *link)
@@ -221,7 +200,7 @@ osmo_stream_client_conn_set_connect_cb(struct osmo_stream_client_conn *link,
void
osmo_stream_client_conn_set_read_cb(struct osmo_stream_client_conn *link,
- int (*read_cb)(struct osmo_stream_client_conn *link, struct msgb *msgb))
+ int (*read_cb)(struct osmo_stream_client_conn *link))
{
link->read_cb = read_cb;
link->flags |= OSMO_STREAM_CLIENT_F_RECONFIG;
@@ -279,6 +258,29 @@ void osmo_stream_client_conn_send(struct osmo_stream_client_conn *link,
link->ofd.when |= BSC_FD_WRITE;
}
+int osmo_stream_client_conn_recv(struct osmo_stream_client_conn *link,
+ struct msgb *msg)
+{
+ int ret;
+
+ ret = recv(link->ofd.fd, msg->data, msg->data_len, 0);
+ if (ret < 0) {
+ if (errno == EPIPE || errno == ECONNRESET) {
+ LOGP(DLINP, LOGL_ERROR,
+ "lost connection with server\n");
+ }
+ osmo_stream_client_retry(link);
+ return ret;
+ } else if (ret == 0) {
+ LOGP(DLINP, LOGL_ERROR, "connection closed with server\n");
+ osmo_stream_client_retry(link);
+ return ret;
+ }
+ msgb_put(msg, ret);
+ LOGP(DLINP, LOGL_DEBUG, "received %d bytes from server\n", ret);
+ return ret;
+}
+
/*
* Server side.
*/
@@ -406,38 +408,16 @@ struct osmo_stream_server_conn {
struct osmo_fd ofd;
struct llist_head tx_queue;
int (*closed_cb)(struct osmo_stream_server_conn *peer);
- int (*cb)(struct osmo_stream_server_conn *peer, struct msgb *msg);
+ int (*cb)(struct osmo_stream_server_conn *peer);
void *data;
};
static void osmo_stream_server_conn_read(struct osmo_stream_server_conn *conn)
{
- struct msgb *msg;
- int ret;
-
LOGP(DLINP, LOGL_DEBUG, "message received\n");
- msg = msgb_alloc(1200, "LAPD/client");
- if (!msg) {
- LOGP(DLINP, LOGL_ERROR, "cannot allocate room for message\n");
- return;
- }
- ret = recv(conn->ofd.fd, msg->data, msg->data_len, 0);
- if (ret < 0) {
- if (errno == EPIPE || errno == ECONNRESET) {
- LOGP(DLINP, LOGL_ERROR, "lost connection with server\n");
- }
- osmo_stream_server_conn_destroy(conn);
- return;
- } else if (ret == 0) {
- LOGP(DLINP, LOGL_ERROR, "connection closed with server\n");
- osmo_stream_server_conn_destroy(conn);
- return;
- }
- msgb_put(msg, ret);
- LOGP(DLINP, LOGL_DEBUG, "received %d bytes from client\n", ret);
if (conn->cb)
- conn->cb(conn, msg);
+ conn->cb(conn);
return;
}
@@ -481,7 +461,7 @@ static int osmo_stream_server_conn_cb(struct osmo_fd *ofd, unsigned int what)
struct osmo_stream_server_conn *
osmo_stream_server_conn_create(void *ctx, struct osmo_stream_server_link *link,
int fd,
- int (*cb)(struct osmo_stream_server_conn *conn, struct msgb *msg),
+ int (*cb)(struct osmo_stream_server_conn *conn),
int (*closed_cb)(struct osmo_stream_server_conn *conn), void *data)
{
struct osmo_stream_server_conn *conn;
@@ -530,3 +510,26 @@ void osmo_stream_server_conn_send(struct osmo_stream_server_conn *conn,
msgb_enqueue(&conn->tx_queue, msg);
conn->ofd.when |= BSC_FD_WRITE;
}
+
+int osmo_stream_server_conn_recv(struct osmo_stream_server_conn *conn,
+ struct msgb *msg)
+{
+ int ret;
+
+ ret = recv(conn->ofd.fd, msg->data, msg->data_len, 0);
+ if (ret < 0) {
+ if (errno == EPIPE || errno == ECONNRESET) {
+ LOGP(DLINP, LOGL_ERROR,
+ "lost connection with server\n");
+ }
+ osmo_stream_server_conn_destroy(conn);
+ return ret;
+ } else if (ret == 0) {
+ LOGP(DLINP, LOGL_ERROR, "connection closed with server\n");
+ osmo_stream_server_conn_destroy(conn);
+ return ret;
+ }
+ msgb_put(msg, ret);
+ LOGP(DLINP, LOGL_DEBUG, "received %d bytes from client\n", ret);
+ return ret;
+}