Changeset 4059 for neercs


Ignore:
Timestamp:
Nov 29, 2009, 12:29:51 PM (10 years ago)
Author:
Jean-Yves Lamoureux
Message:
  • Moved socket-related stuff to struct comm
Location:
neercs/trunk/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • neercs/trunk/src/attach.c

    r3996 r4059  
    8787
    8888    myaddr.sun_family = AF_UNIX;
    89     strncpy(myaddr.sun_path, screen_list->socket_path[socktype],
     89    strncpy(myaddr.sun_path, screen_list->comm.socket_path[socktype],
    9090            sizeof(myaddr.sun_path) - 1);
    9191
    92     unlink(screen_list->socket_path[socktype]);
     92    unlink(screen_list->comm.socket_path[socktype]);
    9393
    9494    if (bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr_un)) < 0)
    9595    {
    96         free(screen_list->socket_path[socktype]);
    97         screen_list->socket_path[socktype] = NULL;
     96        free(screen_list->comm.socket_path[socktype]);
     97        screen_list->comm.socket_path[socktype] = NULL;
    9898        close(sock);
    9999        perror("create_socket:bind");
     
    102102    fcntl(sock, F_SETFL, O_NONBLOCK);
    103103
    104     debug("Listening on %s (%d)", screen_list->socket_path[socktype], sock);
    105 
    106     screen_list->socket[socktype] = sock;
     104    debug("Listening on %s (%d)", screen_list->comm.socket_path[socktype], sock);
     105
     106    screen_list->comm.socket[socktype] = sock;
    107107
    108108    return 0;
     
    162162    memset(&addr, 0, sizeof(addr));
    163163    addr.sun_family = AF_UNIX;
    164     strcpy(addr.sun_path, screen_list->socket_path[socktype]);
     164    strcpy(addr.sun_path, screen_list->comm.socket_path[socktype]);
    165165    if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    166166    {
    167167        debug("Failed to connect to %s: %s",
    168               screen_list->socket_path[socktype], strerror(errno));
     168              screen_list->comm.socket_path[socktype], strerror(errno));
    169169        close(sock);
    170170        return NULL;
     
    172172    fcntl(sock, F_SETFL, O_NONBLOCK);
    173173
    174     screen_list->socket[socktype] = sock;
     174    screen_list->comm.socket[socktype] = sock;
    175175
    176176    if (socktype == SOCK_SERVER)
    177177    {
    178         return socket_to_session(screen_list->socket_path[socktype]);
     178        return socket_to_session(screen_list->comm.socket_path[socktype]);
    179179    }
    180180    else
     
    193193    buf[bytes] = '\0';
    194194    debug("Requesting attach: %s", buf);
    195     return write(screen_list->socket[SOCK_SERVER], buf, strlen(buf)) <= 0;
     195    return write(screen_list->comm.socket[SOCK_SERVER], buf, strlen(buf)) <= 0;
    196196}
    197197
     
    202202    char *ret = NULL;
    203203
    204     sockets = list_sockets(screen_list->socket_dir, screen_list->session_name);
     204    sockets = list_sockets(screen_list->comm.socket_dir, screen_list->comm.session_name);
    205205    if (sockets)
    206206    {
     
    384384void attach(struct screen_list *screen_list)
    385385{
    386     screen_list->socket_path[SOCK_SERVER] = select_socket(screen_list);
    387 
    388     if (screen_list->socket_path[SOCK_SERVER])
     386    screen_list->comm.socket_path[SOCK_SERVER] = select_socket(screen_list);
     387
     388    if (screen_list->comm.socket_path[SOCK_SERVER])
    389389    {
    390390        char *session;
     
    401401            caca_set_cursor(screen_list->dp, 1);
    402402
    403             screen_list->socket_path[SOCK_CLIENT] =
    404                 build_socket_path(screen_list->socket_dir, session,
     403            screen_list->comm.socket_path[SOCK_CLIENT] =
     404                build_socket_path(screen_list->comm.socket_dir, session,
    405405                                  SOCK_CLIENT);
    406406            create_socket(screen_list, SOCK_CLIENT);
    407407            request_attach(screen_list);
    408             if (screen_list->session_name)
    409                 free(screen_list->session_name);
    410             screen_list->session_name = session;
     408            if (screen_list->comm.session_name)
     409                free(screen_list->comm.session_name);
     410            screen_list->comm.session_name = session;
    411411        }
    412412        else
    413413        {
    414414            fprintf(stderr, "Failed to attach!\n");
    415             free(screen_list->socket_path[SOCK_SERVER]);
    416             screen_list->socket_path[SOCK_SERVER] = NULL;
     415            free(screen_list->comm.socket_path[SOCK_SERVER]);
     416            screen_list->comm.socket_path[SOCK_SERVER] = NULL;
    417417            screen_list->attach = 0;
    418418        }
  • neercs/trunk/src/client.c

    r4057 r4059  
    7979        debug("Sending key press to server: %s", buf);
    8080        debug("Sending '%s', %d bytes\n", buf, strlen(buf));
    81         return write(screen_list->socket[SOCK_SERVER], buf, strlen(buf)+1) <= 0;
     81        return write(screen_list->comm.socket[SOCK_SERVER], buf, strlen(buf)+1) <= 0;
    8282    }
    8383    else if (t & CACA_EVENT_RESIZE)
     
    9191        buf[bytes] = '\0';
    9292        debug("Sending '%s', %d bytes\n", buf, strlen(buf));
    93         return write(screen_list->socket[SOCK_SERVER], buf, strlen(buf)+1) <= 0;
     93        return write(screen_list->comm.socket[SOCK_SERVER], buf, strlen(buf)+1) <= 0;
    9494    }
    9595    else if (t & CACA_EVENT_MOUSE_PRESS)
     
    103103        buf[bytes] = '\0';
    104104        debug("Sending '%s', %d bytes\n", buf, strlen(buf));
    105         return write(screen_list->socket[SOCK_SERVER], buf, strlen(buf)+1) <= 0;
     105        return write(screen_list->comm.socket[SOCK_SERVER], buf, strlen(buf)+1) <= 0;
    106106    }
    107107    else if (t & CACA_EVENT_QUIT)
    108         return write(screen_list->socket[SOCK_SERVER], "QUIT",
     108        return write(screen_list->comm.socket[SOCK_SERVER], "QUIT",
    109109                     strlen("QUIT")) <= 0;
    110110   
     
    119119    bytes = snprintf(buf, sizeof(buf) - 1, "DELAY %10d", screen_list->delay);
    120120    buf[bytes] = '\0';
    121     return write(screen_list->socket[SOCK_SERVER], buf, strlen(buf)) <= 0;
     121    return write(screen_list->comm.socket[SOCK_SERVER], buf, strlen(buf)) <= 0;
    122122}
    123123
     
    143143            goto end;
    144144        }
    145         if (screen_list->socket[SOCK_CLIENT]
     145        if (screen_list->comm.socket[SOCK_CLIENT]
    146146            && (n =
    147                 read(screen_list->socket[SOCK_CLIENT], buf,
     147                read(screen_list->comm.socket[SOCK_CLIENT], buf,
    148148                     NEERCS_RECV_BUFSIZE - 1)) > 0)
    149149        {
     
    176176                    }
    177177                    buf = buf2;
    178                     fcntl(screen_list->socket[SOCK_CLIENT], F_SETFL, 0);
    179                     lb = read(screen_list->socket[SOCK_CLIENT], buf + l2,
     178                    fcntl(screen_list->comm.socket[SOCK_CLIENT], F_SETFL, 0);
     179                    lb = read(screen_list->comm.socket[SOCK_CLIENT], buf + l2,
    180180                              NEERCS_RECV_BUFSIZE - 1);
    181181                    if (lb < 0)
     
    198198                    }
    199199                }
    200                 fcntl(screen_list->socket[SOCK_CLIENT], F_SETFL, O_NONBLOCK);
     200                fcntl(screen_list->comm.socket[SOCK_CLIENT], F_SETFL, O_NONBLOCK);
    201201            }
    202202            else if (!strncmp("REFRESH ", buf, 8))
  • neercs/trunk/src/configuration.c

    r4058 r4059  
    471471int set_socket_dir(const char *argv, struct screen_list *screen_list)
    472472{
    473     screen_list->socket_dir = strdup(argv);
     473    screen_list->comm.socket_dir = strdup(argv);
    474474    return 0;
    475475}
     
    560560char *get_socket_dir(struct screen_list *screen_list)
    561561{
    562     return screen_list->socket_dir;
     562    return screen_list->comm.socket_dir;
    563563}
    564564
  • neercs/trunk/src/main.c

    r3974 r4059  
    130130
    131131    /* Build default session name */
    132     if (!screen_list->session_name)
     132    if (!screen_list->comm.session_name)
    133133    {
    134134        char mypid[32];         /* FIXME Compute the length of PID_MAX ? */
    135135        snprintf(mypid, 31, "%d", getpid());
    136136        mypid[31] = '\0';
    137         screen_list->session_name = strdup(mypid);
    138         if (!screen_list->session_name)
     137        screen_list->comm.session_name = strdup(mypid);
     138        if (!screen_list->comm.session_name)
    139139        {
    140140            fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__,
     
    143143        }
    144144    }
    145     if (!screen_list->socket_path[SOCK_CLIENT])
    146         screen_list->socket_path[SOCK_CLIENT] =
    147             build_socket_path(screen_list->socket_dir,
    148                               screen_list->session_name, SOCK_CLIENT);
    149 
    150     if (!screen_list->socket_path[SOCK_SERVER])
    151         screen_list->socket_path[SOCK_SERVER] =
    152             build_socket_path(screen_list->socket_dir,
    153                               screen_list->session_name, SOCK_SERVER);
     145    if (!screen_list->comm.socket_path[SOCK_CLIENT])
     146        screen_list->comm.socket_path[SOCK_CLIENT] =
     147            build_socket_path(screen_list->comm.socket_dir,
     148                              screen_list->comm.session_name, SOCK_CLIENT);
     149
     150    if (!screen_list->comm.socket_path[SOCK_SERVER])
     151        screen_list->comm.socket_path[SOCK_SERVER] =
     152            build_socket_path(screen_list->comm.socket_dir,
     153                              screen_list->comm.session_name, SOCK_SERVER);
    154154
    155155    /* Fork the server if needed */
     
    219219            break;
    220220        case 'S':
    221             if (!screen_list->session_name)
    222                 screen_list->session_name = strdup(myoptarg);
     221            if (!screen_list->comm.session_name)
     222                screen_list->comm.session_name = strdup(myoptarg);
    223223            s += 2;
    224224            break;
     
    273273            if (myoptarg)
    274274            {
    275                 if (screen_list->session_name)
    276                     free(screen_list->session_name);
    277                 screen_list->session_name = strdup(myoptarg);
     275                if (screen_list->comm.session_name)
     276                    free(screen_list->comm.session_name);
     277                screen_list->comm.session_name = strdup(myoptarg);
    278278                s += 1;
    279279            }
  • neercs/trunk/src/neercs.h

    r4058 r4059  
    165165};
    166166
     167struct comm
     168{
     169    /* Detaching */
     170    int attached;                /* Are we attached to a terminal */
     171    int socket[2];               /* Sockets to write to the server / to the client */
     172    char *socket_path[2];        /* Sockets to write to the server / to the client */
     173    char *socket_dir;            /* Where to create the socket */
     174    char *session_name;          /* Name of the session */
     175};
     176
    167177struct screen_list
    168178{
     
    180190    long long unsigned int last_refresh_time;
    181191
    182 
    183     /* Detaching */
    184     int attached;                /* Are we attached to a terminal */
    185     int socket[2];               /* Sockets to write to the server / to the client */
    186     char *socket_path[2];        /* Sockets to write to the server / to the client */
    187     char *socket_dir;            /* Where to create the socket */
    188     char *session_name;          /* Name of the session */
    189 
     192    struct comm comm;
     193   
    190194    /* Lock */
    191195    int locked;
  • neercs/trunk/src/screen_list.c

    r4058 r4059  
    7777    screen_list->lock_offset = 0;
    7878    screen_list->lock_on_detach = 0;
    79     screen_list->attached = 1;
    80     screen_list->socket[SOCK_SERVER] = 0;
    81     screen_list->socket[SOCK_CLIENT] = 0;
    82     screen_list->socket_dir = NULL;
    83     screen_list->socket_path[SOCK_SERVER] = NULL;
    84     screen_list->socket_path[SOCK_CLIENT] = NULL;
    85     screen_list->session_name = NULL;
     79    screen_list->comm.attached = 1;
     80    screen_list->comm.socket[SOCK_SERVER] = 0;
     81    screen_list->comm.socket[SOCK_CLIENT] = 0;
     82    screen_list->comm.socket_dir = NULL;
     83    screen_list->comm.socket_path[SOCK_SERVER] = NULL;
     84    screen_list->comm.socket_path[SOCK_CLIENT] = NULL;
     85    screen_list->comm.session_name = NULL;
    8686    screen_list->default_shell = NULL;
    8787    screen_list->user_path = NULL;
     
    160160    }
    161161   
    162     if (screen_list->socket_path[SOCK_SERVER])
     162    if (screen_list->comm.socket_path[SOCK_SERVER])
    163163    {
    164164        /* FIXME test that we are the server */
    165165        if (!screen_list->dp)
    166             unlink(screen_list->socket_path[SOCK_SERVER]);
    167         free(screen_list->socket_path[SOCK_SERVER]);
    168     }
    169    
    170     if (screen_list->socket_path[SOCK_CLIENT])
     166            unlink(screen_list->comm.socket_path[SOCK_SERVER]);
     167        free(screen_list->comm.socket_path[SOCK_SERVER]);
     168    }
     169   
     170    if (screen_list->comm.socket_path[SOCK_CLIENT])
    171171    {
    172172        /* FIXME test that we are the client */
    173173        if (screen_list->dp)
    174174
    175             unlink(screen_list->socket_path[SOCK_CLIENT]);
    176         free(screen_list->socket_path[SOCK_CLIENT]);
    177     }
    178    
    179     if (screen_list->socket[SOCK_CLIENT])
    180         close(screen_list->socket[SOCK_CLIENT]);
    181    
    182     if (screen_list->socket[SOCK_SERVER])
    183         close(screen_list->socket[SOCK_SERVER]);
     175            unlink(screen_list->comm.socket_path[SOCK_CLIENT]);
     176        free(screen_list->comm.socket_path[SOCK_CLIENT]);
     177    }
     178   
     179    if (screen_list->comm.socket[SOCK_CLIENT])
     180        close(screen_list->comm.socket[SOCK_CLIENT]);
     181   
     182    if (screen_list->comm.socket[SOCK_SERVER])
     183        close(screen_list->comm.socket[SOCK_SERVER]);
    184184   
    185185    if (screen_list->screen)
     
    210210        free(screen_list->recurrent_list);
    211211   
    212     if (screen_list->session_name)
    213         free(screen_list->session_name);
     212    if (screen_list->comm.session_name)
     213        free(screen_list->comm.session_name);
    214214   
    215215    if (screen_list->title)
  • neercs/trunk/src/server.c

    r4058 r4059  
    4646{
    4747    int ret;
    48     if (!screen_list->socket[SOCK_CLIENT])
     48    if (!screen_list->comm.socket[SOCK_CLIENT])
    4949        connect_socket(screen_list, SOCK_CLIENT);
    50     if (!screen_list->socket[SOCK_CLIENT])
     50    if (!screen_list->comm.socket[SOCK_CLIENT])
    5151        ret = -1;
    5252    else
    53         ret = write(screen_list->socket[SOCK_CLIENT], msg, size);
     53        ret = write(screen_list->comm.socket[SOCK_CLIENT], msg, size);
    5454    if (ret < 0 && errno != EAGAIN)
    5555    {
    5656        fprintf(stderr, "Failed to send message to client: %s\n",
    5757                strerror(errno));
    58         if (screen_list->attached)
     58        if (screen_list->comm.attached)
    5959            detach(screen_list);
    6060    }
     
    6868    char *title = NULL;
    6969
    70     if (screen_list->attached)
     70    if (screen_list->comm.attached)
    7171    {
    7272        if (screen_list->pty < screen_list->count &&
     
    109109        return 0;
    110110#endif
    111     if (!screen_list->socket[SOCK_CLIENT])
     111    if (!screen_list->comm.socket[SOCK_CLIENT])
    112112        connect_socket(screen_list, SOCK_CLIENT);
    113     if (screen_list->socket[SOCK_CLIENT])
     113    if (screen_list->comm.socket[SOCK_CLIENT])
    114114    {
    115115        size_t bufsize, towrite;
     
    121121        int x, y, i;
    122122
    123         getsockopt(screen_list->socket[SOCK_CLIENT], SOL_SOCKET, SO_SNDBUF,
     123        getsockopt(screen_list->comm.socket[SOCK_CLIENT], SOL_SOCKET, SO_SNDBUF,
    124124                   &bufsize, &optlen);
    125125        bufsize /= 2;
     
    159159                debug("Wrote %d, %d remaining", written, towrite);
    160160                /* Block to write the end of the message */
    161                 fcntl(screen_list->socket[SOCK_CLIENT], F_SETFL, 0);
     161                fcntl(screen_list->comm.socket[SOCK_CLIENT], F_SETFL, 0);
    162162                n = send_to_client((char *)buf + written,
    163163                                   towrite > bufsize ? bufsize : towrite,
     
    173173                towrite -= n;
    174174            }
    175             fcntl(screen_list->socket[SOCK_CLIENT], F_SETFL, O_NONBLOCK);
     175            fcntl(screen_list->comm.socket[SOCK_CLIENT], F_SETFL, O_NONBLOCK);
    176176            free(buf);
    177177        }
     
    190190int detach(struct screen_list *screen_list)
    191191{
    192     screen_list->attached = 0;
     192    screen_list->comm.attached = 0;
    193193    if (screen_list->lock_on_detach)
    194194        screen_list->locked = 1;
    195     if (screen_list->socket[SOCK_CLIENT])
     195    if (screen_list->comm.socket[SOCK_CLIENT])
    196196    {
    197197        send_to_client("DETACH", 6, screen_list);
    198         close(screen_list->socket[SOCK_CLIENT]);
    199         screen_list->socket[SOCK_CLIENT] = 0;
     198        close(screen_list->comm.socket[SOCK_CLIENT]);
     199        screen_list->comm.socket[SOCK_CLIENT] = 0;
    200200    }
    201201    return 0;
     
    215215
    216216    /* Check if we got something from the client */
    217     while (screen_list->socket[SOCK_SERVER]
     217    while (screen_list->comm.socket[SOCK_SERVER]
    218218           && (n =
    219                read(screen_list->socket[SOCK_SERVER], buf,
     219               read(screen_list->comm.socket[SOCK_SERVER], buf,
    220220                    sizeof(buf) - 1)) > 0)
    221221    {
     
    296296{
    297297    screen_list->last_key_time = 0;
    298     screen_list->attached = 0;
     298    screen_list->comm.attached = 0;
    299299    screen_list->command = 0;
    300300    screen_list->was_in_bell = 0;
     
    325325static void refresh_screen(struct screen_list *screen_list, int refresh)
    326326{
    327     if (!screen_list->attached)
     327    if (!screen_list->comm.attached)
    328328    {
    329329        /* No need to refresh Don't use the CPU too much Would be better to
     
    461461{
    462462    /* If we were attached to someone else, detach first */
    463     if (screen_list->attached)
     463    if (screen_list->comm.attached)
    464464        detach(screen_list);
    465     screen_list->attached = 1;
     465    screen_list->comm.attached = 1;
    466466    caca_free_canvas(screen_list->cv);
    467467    screen_list->cv = caca_create_canvas(atoi(buf + 7), atoi(buf + 18));
Note: See TracChangeset for help on using the changeset viewer.