void ( * closed ) ( struct tcp_connection *conn );
void ( * connected ) ( struct tcp_connection *conn );
void ( * acked ) ( struct tcp_connection *conn, size_t len );
- void ( * newdata ) ( struct tcp_connection *conn );
+ void ( * newdata ) ( struct tcp_connection *conn,
+ void *data, size_t len );
void ( * senddata ) ( struct tcp_connection *conn );
};
struct tcp_operations *tcp_op;
};
-static int tcp_connect ( struct tcp_connection *conn ) {
+int tcp_connect ( struct tcp_connection *conn ) {
struct uip_conn *uip_conn;
u16_t ipaddr[2];
return 0;
}
-static void tcp_send ( struct tcp_connection *conn, const void *data,
+void tcp_send ( struct tcp_connection *conn, const void *data,
size_t len ) {
assert ( conn = *( ( void ** ) uip_conn->appstate ) );
uip_send ( ( void * ) data, len );
}
-static void tcp_close ( struct tcp_connection *conn ) {
+void tcp_close ( struct tcp_connection *conn ) {
assert ( conn = *( ( void ** ) uip_conn->appstate ) );
uip_close();
}
if ( uip_acked() )
op->acked ( conn, uip_conn->len );
if ( uip_newdata() )
- op->newdata ( conn );
+ op->newdata ( conn, ( void * ) uip_appdata, uip_len );
if ( uip_rexmit() || uip_newdata() || uip_acked() ||
uip_connected() || uip_poll() )
op->senddata ( conn );
(type *)( (char *)__mptr - offsetof(type,member) );})
enum hello_state {
- HELLO_SENDING_MESSAGE = 0,
+ HELLO_SENDING_MESSAGE = 1,
HELLO_SENDING_ENDL,
};
struct tcp_connection tcp;
const char *message;
enum hello_state state;
- int remaining;
- void ( *callback ) ( struct hello_request *hello );
+ size_t remaining;
+ void ( *callback ) ( char *data, size_t len );
int complete;
};
static void hello_connected ( struct tcp_connection *conn ) {
struct hello_request *hello = tcp_to_hello ( conn );
+
+ printf ( "Connection established\n" );
+ hello->state = HELLO_SENDING_MESSAGE;
}
static void hello_acked ( struct tcp_connection *conn, size_t len ) {
hello->message += len;
hello->remaining -= len;
- if ( hello->remaining <= 0 ) {
+ if ( hello->remaining == 0 ) {
switch ( hello->state ) {
case HELLO_SENDING_MESSAGE:
hello->state = HELLO_SENDING_ENDL;
hello->remaining = 2;
break;
case HELLO_SENDING_ENDL:
- tcp_close ( conn );
+ /* Nothing to do once we've finished sending
+ * the end-of-line indicator.
+ */
break;
default:
assert ( 0 );
}
}
-static void hello_newdata ( struct tcp_connection *conn ) {
+static void hello_newdata ( struct tcp_connection *conn, void *data,
+ size_t len ) {
struct hello_request *hello = tcp_to_hello ( conn );
+
+ hello->callback ( data, len );
}
static void hello_senddata ( struct tcp_connection *conn ) {
return optind;
}
-static void test_hello_callback ( struct hello_request *hello ) {
-
+static void test_hello_callback ( char *data, size_t len ) {
+ int i;
+ char c;
+
+ for ( i = 0 ; i < len ; i++ ) {
+ c = data[i];
+ if ( c == '\r' ) {
+ /* Print nothing */
+ } else if ( ( c == '\n' ) || ( c >= 32 ) || ( c <= 126 ) ) {
+ putchar ( c );
+ } else {
+ putchar ( '.' );
+ }
+ }
}
static int test_hello ( int argc, char **argv ) {