Changeset ee1c2d9 in mainline for uspace/app/nterm/conn.c


Ignore:
Timestamp:
2015-06-13T18:30:18Z (9 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a157846
Parents:
0453261 (diff), 2f9a8e8 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

mainline changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/nterm/conn.c

    r0453261 ree1c2d9  
    3838#include <fibril.h>
    3939#include <inet/dnsr.h>
    40 #include <net/inet.h>
    41 #include <net/socket.h>
     40#include <inet/endpoint.h>
     41#include <inet/tcp.h>
    4242#include <stdio.h>
    4343#include <stdlib.h>
     
    4848#include "nterm.h"
    4949
    50 static int conn_fd;
    51 static fid_t rcv_fid;
     50static tcp_t *tcp;
     51static tcp_conn_t *conn;
    5252
    5353#define RECV_BUF_SIZE 1024
    5454static uint8_t recv_buf[RECV_BUF_SIZE];
    5555
    56 static int rcv_fibril(void *arg)
     56static void conn_conn_reset(tcp_conn_t *);
     57static void conn_data_avail(tcp_conn_t *);
     58
     59static tcp_cb_t conn_cb = {
     60        .conn_reset = conn_conn_reset,
     61        .data_avail = conn_data_avail
     62};
     63
     64static void conn_conn_reset(tcp_conn_t *conn)
    5765{
    58         ssize_t nr;
     66        printf("\n[Connection reset]\n");
     67}
     68
     69static void conn_data_avail(tcp_conn_t *conn)
     70{
     71        int rc;
     72        size_t nrecv;
    5973
    6074        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 %d]\n", rc);
    6378                        break;
     79                }
    6480
    65                 nterm_received(recv_buf, nr);
     81                nterm_received(recv_buf, nrecv);
     82
     83                if (nrecv != RECV_BUF_SIZE)
     84                        break;
    6685        }
    67 
    68         if (nr == ENOTCONN)
    69                 printf("\n[Other side has closed the connection]\n");
    70         else
    71                 printf("'\n[Receive errror (%s)]\n", str_error(nr));
    72 
    73         exit(0);
    74         return 0;
    7586}
    7687
    7788int conn_open(const char *host, const char *port_s)
    7889{
    79         int conn_fd = -1;
    80         struct sockaddr *saddr = NULL;
    81         socklen_t saddrlen;
    82        
     90        inet_ep2_t epp;
     91
    8392        /* Interpret as address */
    8493        inet_addr_t iaddr;
    8594        int rc = inet_addr_parse(host, &iaddr);
    86        
     95
    8796        if (rc != EOK) {
    8897                /* Interpret as a host name */
    8998                dnsr_hostinfo_t *hinfo = NULL;
    9099                rc = dnsr_name2host(host, &hinfo, ip_any);
    91                
     100
    92101                if (rc != EOK) {
    93102                        printf("Error resolving host '%s'.\n", host);
    94103                        goto error;
    95104                }
    96                
     105
    97106                iaddr = hinfo->addr;
    98107        }
    99        
     108
    100109        char *endptr;
    101110        uint16_t port = strtol(port_s, &endptr, 10);
     
    104113                goto error;
    105114        }
    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
    114120        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);
    121123        if (rc != EOK)
    122124                goto error;
    123        
    124         rcv_fid = fibril_create(rcv_fibril, NULL);
    125         if (rcv_fid == 0)
     125
     126        rc = tcp_conn_create(tcp, &epp, &conn_cb, NULL, &conn);
     127        if (rc != EOK)
    126128                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
    131134        return EOK;
    132135error:
    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
    139139        return EIO;
    140140}
     
    142142int conn_send(void *data, size_t size)
    143143{
    144         int rc = send(conn_fd, data, size, 0);
     144        int rc = tcp_conn_send(conn, data, size);
    145145        if (rc != EOK)
    146146                return EIO;
    147        
     147
    148148        return EOK;
    149149}
Note: See TracChangeset for help on using the changeset viewer.