Ignore:
Timestamp:
Jul 31, 2008, 1:34:55 AM (12 years ago)
Author:
Pascal Terjan
Message:
  • Factorize some code in attach.c
File:
1 edited

Legend:

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

    r2611 r2614  
    1717#include "neercs.h"
    1818
    19 char * build_socket_path(char *socket_dir, char *session_name, int client)
     19char * build_socket_path(char *socket_dir, char *session_name, enum socket_type socktype)
    2020{
    2121    char *path, *dir;
     
    2929        dir = "/tmp";
    3030    if(path)
    31         snprintf(path, PATH_MAX+1, "%s/neercs.%s%s.sock", dir, session_name, client?"":".srv");
     31        snprintf(path, PATH_MAX+1, "%s/neercs.%s%s.sock", dir, session_name, socktype?"":".srv");
    3232    return path;
    3333}
    3434
    35 int create_client_socket(struct screen_list* screen_list)
     35int create_socket(struct screen_list* screen_list, enum socket_type socktype)
    3636{
    3737    int sock;
     
    4242    if(sock < 0)
    4343    {
    44         perror("create_client_socket:socket");
     44        perror("create_socket:socket");
    4545        return errno;
    4646    }
     
    4949
    5050    myaddr.sun_family = AF_UNIX;
    51     strncpy(myaddr.sun_path, screen_list->c_socket_path, sizeof(myaddr.sun_path) - 1);
     51    strncpy(myaddr.sun_path, screen_list->socket_path[socktype], sizeof(myaddr.sun_path) - 1);
    5252
    53     unlink(screen_list->c_socket_path);
     53    unlink(screen_list->socket_path[socktype]);
    5454
    5555    if(bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr_un)) < 0)
    5656    {
    57         free(screen_list->c_socket_path);
    58         screen_list->c_socket_path = NULL;
    59         close(sock);
    60         perror("create_client_socket:bind");
    61         return errno;
    62     }
    63     fcntl(sock, F_SETFL, O_NONBLOCK);
    64 
    65     debug("Client listening on %s (%d)", screen_list->c_socket_path, sock);
    66 
    67     screen_list->c_socket = sock;
    68 
    69     return 0;
    70 }
    71 
    72 int create_server_socket(struct screen_list* screen_list)
    73 {
    74     int sock;
    75     struct sockaddr_un myaddr;
    76 
    77     sock = socket(AF_UNIX, SOCK_DGRAM, 0);
    78 
    79     if(sock < 0)
    80     {
    81         perror("create_server_socket:socket");
    82         return errno;
    83     }
    84 
    85     memset(&myaddr, 0, sizeof(struct sockaddr_un));
    86 
    87     myaddr.sun_family = AF_UNIX;
    88     strncpy(myaddr.sun_path, screen_list->s_socket_path, sizeof(myaddr.sun_path) - 1);
    89 
    90     unlink(screen_list->s_socket_path);
    91 
    92     if(bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr_un)) < 0)
    93     {
    94         free(screen_list->s_socket_path);
    95         screen_list->s_socket_path = NULL;
     57        free(screen_list->socket_path[socktype]);
     58        screen_list->socket_path[socktype] = NULL;
    9659        close(sock);
    9760        perror("create_socket:bind");
     
    10063    fcntl(sock, F_SETFL, O_NONBLOCK);
    10164
    102     debug("Server listening on %s (%d)", screen_list->s_socket_path, sock);
     65    debug("Listening on %s (%d)", screen_list->socket_path[socktype], sock);
    10366
    104     screen_list->s_socket = sock;
     67    screen_list->socket[socktype] = sock;
    10568
    10669    return 0;
     
    142105}
    143106
    144 char * connect_server(struct screen_list* screen_list)
     107char * connect_socket(struct screen_list* screen_list, enum socket_type socktype)
    145108{
    146109    int sock;
     
    148111    char *p, *s;
    149112
    150     debug("Connecting to %s", screen_list->s_socket_path);
     113    debug("Connecting to %s", screen_list->socket_path[socktype]);
    151114
    152115    /* Open the socket */
     
    158121    memset(&addr,0,sizeof(addr));
    159122    addr.sun_family = AF_UNIX;
    160     strcpy(addr.sun_path,screen_list->s_socket_path);
     123    strcpy(addr.sun_path,screen_list->socket_path[socktype]);
    161124    if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    162125    {
    163         fprintf(stderr, "Failed to connect to %s: %s\n", screen_list->s_socket_path, strerror(errno));
     126        fprintf(stderr, "Failed to connect to %s: %s\n", screen_list->socket_path[socktype], strerror(errno));
    164127        return NULL;
    165128    }
    166129    fcntl(sock, F_SETFL, O_NONBLOCK);
    167130
    168     screen_list->s_socket = sock;
     131    screen_list->socket[socktype] = sock;
    169132
    170     p = strrchr(screen_list->s_socket_path, '/');
    171     p+=8; /* skip neercs. */
    172     s = strdup(p);
    173     p = strrchr(s, '.');
    174     *p = '\0'; /* drop .sock */
    175     p = strrchr(s, '.');
    176     *p = '\0'; /* drop .srv */
    177     p = strdup(s);
    178     free(s);
    179     return p;
    180 }
    181 
    182 int connect_client(struct screen_list* screen_list)
    183 {
    184     int sock;
    185     struct sockaddr_un addr;
    186 
    187     debug("Connecting to %s", screen_list->c_socket_path);
    188 
    189     /* Open the socket */
    190     if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
    191         perror("connect_client:socket");
    192         return 1;
     133    if(socktype == SOCK_SERVER)
     134    {
     135        p = strrchr(screen_list->socket_path[socktype], '/');
     136        p+=8; /* skip neercs. */
     137        s = strdup(p);
     138        p = strrchr(s, '.');
     139        *p = '\0'; /* drop .sock */
     140        p = strrchr(s, '.');
     141        *p = '\0'; /* drop .srv */
     142        p = strdup(s);
     143        free(s);
     144        return p;
    193145    }
    194 
    195     memset(&addr,0,sizeof(addr));
    196     addr.sun_family = AF_UNIX;
    197     strcpy(addr.sun_path,screen_list->c_socket_path);
    198     if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    199     {
    200         fprintf(stderr, "Failed to connect to %s: %s\n", screen_list->c_socket_path, strerror(errno));
    201         return 1;
    202     }
    203     fcntl(sock, F_SETFL, O_NONBLOCK);
    204 
    205     screen_list->c_socket = sock;
    206 
    207     return 0;
     146    else
     147        return NULL;
    208148}
    209149
     
    219159    buf[bytes] = '\0';
    220160    debug("Requesting attach: %s", buf);
    221     return write(screen_list->s_socket, buf, strlen(buf)) <= 0;
     161    return write(screen_list->socket[SOCK_SERVER], buf, strlen(buf)) <= 0;
    222162}
    223163
     
    235175        buf[bytes] = '\0';
    236176        debug("Sending key press to server: %s", buf);
    237         return write(screen_list->s_socket, buf, strlen(buf)) <= 0;
     177        return write(screen_list->socket[SOCK_SERVER], buf, strlen(buf)) <= 0;
    238178    }
    239179    else if(t & CACA_EVENT_RESIZE)
     
    247187        buf[bytes] = '\0';
    248188        debug("Sending resize to server: %s", buf);
    249         return write(screen_list->s_socket, buf, strlen(buf)) <= 0;
     189        return write(screen_list->socket[SOCK_SERVER], buf, strlen(buf)) <= 0;
    250190    }
    251191    else if(t & CACA_EVENT_QUIT)
    252         return write(screen_list->s_socket, "QUIT", strlen("QUIT")) <= 0;
     192        return write(screen_list->socket[SOCK_SERVER], "QUIT", strlen("QUIT")) <= 0;
    253193
    254194    return 0;
Note: See TracChangeset for help on using the changeset viewer.