Changeset fab2746 in mainline for uspace/app/nterm/conn.c
- Timestamp:
- 2015-04-08T21:25:30Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 99ea91b2
- Parents:
- ba0eac5
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nterm/conn.c
rba0eac5 rfab2746 38 38 #include <fibril.h> 39 39 #include <inet/dnsr.h> 40 #include < net/inet.h>41 #include < net/socket.h>40 #include <inet/endpoint.h> 41 #include <inet/tcp.h> 42 42 #include <stdio.h> 43 43 #include <stdlib.h> … … 48 48 #include "nterm.h" 49 49 50 static int conn_fd;51 static fid_t rcv_fid;50 static tcp_t *tcp; 51 static tcp_conn_t *conn; 52 52 53 53 #define RECV_BUF_SIZE 1024 54 54 static uint8_t recv_buf[RECV_BUF_SIZE]; 55 55 56 static int rcv_fibril(void *arg) 56 static void conn_conn_reset(tcp_conn_t *); 57 static void conn_data_avail(tcp_conn_t *); 58 59 static tcp_cb_t conn_cb = { 60 .conn_reset = conn_conn_reset, 61 .data_avail = conn_data_avail 62 }; 63 64 static void conn_conn_reset(tcp_conn_t *conn) 57 65 { 58 ssize_t nr; 66 printf("\n[Connection reset]\n"); 67 } 68 69 static void conn_data_avail(tcp_conn_t *conn) 70 { 71 int rc; 72 size_t nrecv; 59 73 60 74 while (true) { 61 nr = recv(conn_fd, recv_buf, RECV_BUF_SIZE, 0); 62 if (nr < 0) 75 rc = tcp_conn_recv(conn, recv_buf, RECV_BUF_SIZE, &nrecv); 76 if (rc != EOK) { 77 printf("\n[Receive error]\n"); 63 78 break; 79 } 64 80 65 nterm_received(recv_buf, nr); 81 nterm_received(recv_buf, nrecv); 82 83 if (nrecv != RECV_BUF_SIZE) 84 break; 66 85 } 67 68 if (nr == ENOTCONN)69 printf("\n[Other side has closed the connection]\n");70 else71 printf("'\n[Receive errror (%s)]\n", str_error(nr));72 73 exit(0);74 return 0;75 86 } 76 87 77 88 int conn_open(const char *host, const char *port_s) 78 89 { 79 int conn_fd = -1; 80 struct sockaddr *saddr = NULL; 81 socklen_t saddrlen; 82 90 inet_ep2_t epp; 91 83 92 /* Interpret as address */ 84 93 inet_addr_t iaddr; 85 94 int rc = inet_addr_parse(host, &iaddr); 86 95 87 96 if (rc != EOK) { 88 97 /* Interpret as a host name */ 89 98 dnsr_hostinfo_t *hinfo = NULL; 90 99 rc = dnsr_name2host(host, &hinfo, ip_any); 91 100 92 101 if (rc != EOK) { 93 102 printf("Error resolving host '%s'.\n", host); 94 103 goto error; 95 104 } 96 105 97 106 iaddr = hinfo->addr; 98 107 } 99 108 100 109 char *endptr; 101 110 uint16_t port = strtol(port_s, &endptr, 10); … … 104 113 goto error; 105 114 } 106 107 rc = inet_addr_sockaddr(&iaddr, port, &saddr, &saddrlen); 108 if (rc != EOK) { 109 assert(rc == ENOMEM); 110 printf("Out of memory.\n"); 111 return ENOMEM; 112 } 113 115 116 inet_ep2_init(&epp); 117 epp.remote.addr = iaddr; 118 epp.remote.port = port; 119 114 120 printf("Connecting to host %s port %u\n", host, port); 115 116 conn_fd = socket(saddr->sa_family, SOCK_STREAM, 0); 117 if (conn_fd < 0) 118 goto error; 119 120 rc = connect(conn_fd, saddr, saddrlen); 121 122 rc = tcp_create(&tcp); 121 123 if (rc != EOK) 122 124 goto error; 123 124 rc v_fid = fibril_create(rcv_fibril, NULL);125 if (rc v_fid == 0)125 126 rc = tcp_conn_create(tcp, &epp, &conn_cb, NULL, &conn); 127 if (rc != EOK) 126 128 goto error; 127 128 fibril_add_ready(rcv_fid); 129 130 free(saddr); 129 130 rc = tcp_conn_wait_connected(conn); 131 if (rc != EOK) 132 goto error; 133 131 134 return EOK; 132 135 error: 133 if (conn_fd >= 0) { 134 closesocket(conn_fd); 135 conn_fd = -1; 136 } 137 free(saddr); 138 136 tcp_conn_destroy(conn); 137 tcp_destroy(tcp); 138 139 139 return EIO; 140 140 } … … 142 142 int conn_send(void *data, size_t size) 143 143 { 144 int rc = send(conn_fd, data, size, 0);144 int rc = tcp_conn_send(conn, data, size); 145 145 if (rc != EOK) 146 146 return EIO; 147 147 148 148 return EOK; 149 149 }
Note:
See TracChangeset
for help on using the changeset viewer.