Changeset 2589
- Timestamp:
- Jul 27, 2008, 4:33:30 AM (15 years ago)
- Location:
- neercs/trunk/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
neercs/trunk/src/attach.c
r2588 r2589 16 16 #include "neercs.h" 17 17 18 char * build_socket_path(char *socket_dir, char *session_name )18 char * build_socket_path(char *socket_dir, char *session_name, int client) 19 19 { 20 20 char *path, *dir; … … 28 28 dir = "/tmp"; 29 29 if(path) 30 snprintf(path, PATH_MAX+1, "%s/neercs.%s .sock", dir, session_name);30 snprintf(path, PATH_MAX+1, "%s/neercs.%s%s.sock", dir, session_name, client?"-clt":""); 31 31 return path; 32 32 } 33 33 34 int create_ socket(struct screen_list* screen_list)34 int create_client_socket(struct screen_list* screen_list) 35 35 { 36 36 int sock; … … 41 41 if(sock < 0) 42 42 { 43 perror("create_ socket:socket");43 perror("create_client_socket:socket"); 44 44 return errno; 45 45 } … … 48 48 49 49 myaddr.sun_family = AF_UNIX; 50 strncpy(myaddr.sun_path, screen_list->socket_path, sizeof(myaddr.sun_path) - 1); 50 strncpy(myaddr.sun_path, screen_list->c_socket_path, sizeof(myaddr.sun_path) - 1); 51 52 unlink(screen_list->c_socket_path); 51 53 52 54 if(bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr_un)) < 0) 53 55 { 54 free(screen_list->socket_path); 55 screen_list->socket_path = NULL; 56 free(screen_list->c_socket_path); 57 screen_list->c_socket_path = NULL; 58 close(sock); 59 perror("create_client_socket:bind"); 60 return errno; 61 } 62 fcntl(sock, F_SETFL, O_NONBLOCK); 63 64 debug("Client listening on %s (%d)", screen_list->c_socket_path, sock); 65 66 screen_list->c_socket = sock; 67 68 return 0; 69 } 70 71 int create_server_socket(struct screen_list* screen_list) 72 { 73 int sock; 74 struct sockaddr_un myaddr; 75 76 sock = socket(AF_UNIX, SOCK_DGRAM, 0); 77 78 if(sock < 0) 79 { 80 perror("create_server_socket:socket"); 81 return errno; 82 } 83 84 memset(&myaddr, 0, sizeof(struct sockaddr_un)); 85 86 myaddr.sun_family = AF_UNIX; 87 strncpy(myaddr.sun_path, screen_list->s_socket_path, sizeof(myaddr.sun_path) - 1); 88 89 unlink(screen_list->c_socket_path); 90 91 if(bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr_un)) < 0) 92 { 93 free(screen_list->s_socket_path); 94 screen_list->s_socket_path = NULL; 56 95 close(sock); 57 96 perror("create_socket:bind"); … … 60 99 fcntl(sock, F_SETFL, O_NONBLOCK); 61 100 62 debug(" Listening on %s (%d)", screen_list->socket_path, sock);63 64 screen_list->s ocket = sock;101 debug("Client listening on %s (%d)", screen_list->s_socket_path, sock); 102 103 screen_list->s_socket = sock; 65 104 66 105 return 0; … … 102 141 } 103 142 104 char * connect_server( char *socket_path,struct screen_list* screen_list)143 char * connect_server(struct screen_list* screen_list) 105 144 { 106 145 int sock; 107 146 struct sockaddr_un addr; 108 109 debug("Connecting to %s", socket_path); 147 char *p, *s; 148 149 debug("Connecting to %s", screen_list->s_socket_path); 110 150 111 151 /* Open the socket */ 112 152 if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { 113 perror(" request_attach:socket");153 perror("connect_server:socket"); 114 154 return NULL; 115 155 } … … 117 157 memset(&addr,0,sizeof(addr)); 118 158 addr.sun_family = AF_UNIX; 119 strcpy(addr.sun_path,s ocket_path);159 strcpy(addr.sun_path,screen_list->s_socket_path); 120 160 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) 121 161 { 122 printf("Failed to connect to %s: %s\n",socket_path, strerror(errno));162 fprintf(stderr, "Failed to connect to %s: %s\n", screen_list->s_socket_path, strerror(errno)); 123 163 return NULL; 124 164 } 125 165 fcntl(sock, F_SETFL, O_NONBLOCK); 126 166 127 screen_list->socket_path = socket_path; 128 screen_list->socket = sock; 129 130 return strdup(socket_path); /* FIXME */ 167 screen_list->s_socket = sock; 168 169 p = strrchr(screen_list->s_socket_path, '/'); 170 p+=8; /* skip neercs. */ 171 s = strdup(p); 172 p = strrchr(s, '.'); 173 *p = '\0'; /* drop .sock */ 174 p = strdup(s); 175 free(s); 176 return p; 177 } 178 179 int connect_client(struct screen_list* screen_list) 180 { 181 int sock; 182 struct sockaddr_un addr; 183 184 debug("Connecting to %s", screen_list->c_socket_path); 185 186 /* Open the socket */ 187 if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { 188 perror("connect_client:socket"); 189 return 1; 190 } 191 192 memset(&addr,0,sizeof(addr)); 193 addr.sun_family = AF_UNIX; 194 strcpy(addr.sun_path,screen_list->c_socket_path); 195 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) 196 { 197 fprintf(stderr, "Failed to connect to %s: %s\n", screen_list->c_socket_path, strerror(errno)); 198 return 1; 199 } 200 fcntl(sock, F_SETFL, O_NONBLOCK); 201 202 screen_list->c_socket = sock; 203 204 return 0; 131 205 } 132 206 133 207 int request_attach(struct screen_list* screen_list) 134 208 { 135 debug("Requesting attach"); 136 137 write(screen_list->socket, "ATTACH", strlen("ATTACH")); 138 139 return 0; 140 } 141 142 int send_event(caca_event_t ev, int sock) 209 char buf[32]; 210 int bytes; 211 212 bytes = snprintf(buf, sizeof(buf)-1, "ATTACH %10d%c%10d", 213 cucul_get_canvas_width(screen_list->cv), 214 ' ', 215 cucul_get_canvas_height(screen_list->cv)); 216 buf[bytes] = '\0'; 217 debug("Requesting attach: %s", buf); 218 return write(screen_list->s_socket, buf, strlen(buf)) <= 0; 219 } 220 221 int send_event(caca_event_t ev, struct screen_list* screen_list) 143 222 { 144 223 enum caca_event_type t; … … 153 232 buf[bytes] = '\0'; 154 233 debug("Sending key press to server: %s", buf); 155 return write(s ock, buf, strlen(buf)) <= 0;234 return write(screen_list->s_socket, buf, strlen(buf)) <= 0; 156 235 } 157 236 else if(t & CACA_EVENT_RESIZE) 158 return write(sock, "RESIZE", strlen("RESIZE")) <= 0; 237 { 238 char buf[32]; 239 int bytes; 240 bytes = snprintf(buf, sizeof(buf)-1, "RESIZE %10d%c%10d", 241 caca_get_event_resize_width(&ev), 242 ' ', 243 caca_get_event_resize_height(&ev)); 244 buf[bytes] = '\0'; 245 debug("Sending resize to server: %s", buf); 246 return write(screen_list->s_socket, buf, strlen(buf)) <= 0; 247 } 159 248 else if(t & CACA_EVENT_QUIT) 160 return write(s ock, "QUIT", strlen("QUIT")) <= 0;161 162 return 0; 163 } 164 249 return write(screen_list->s_socket, "QUIT", strlen("QUIT")) <= 0; 250 251 return 0; 252 } 253 -
neercs/trunk/src/main.c
r2588 r2589 83 83 char *user_path = NULL, *user_dir = NULL; 84 84 int i, args, s=0; 85 int refresh = 1;86 85 long long unsigned int last_key_time = 0; 87 86 int mainret = 0; … … 253 252 return -1; 254 253 } 254 255 255 sockets = list_sockets(screen_list->socket_dir, screen_list->session_name); 256 256 if(sockets && sockets[0]) … … 259 259 for(i=0; sockets[i]; i++); 260 260 i--; 261 session = connect_server(sockets[i], screen_list); 261 screen_list->s_socket_path = sockets[i]; 262 session = connect_server(screen_list); 262 263 if(session) 263 264 { 265 /* Create main canvas and associated caca window */ 266 screen_list->cv = cucul_create_canvas(0, 0); 267 screen_list->dp = caca_create_display(screen_list->cv); 268 if(!screen_list->dp) 269 return 1; 270 caca_set_cursor(screen_list->dp, 1); 271 272 screen_list->c_socket_path = build_socket_path(screen_list->socket_dir, session, 1); 273 create_client_socket(screen_list); 264 274 request_attach(screen_list); 265 275 if(screen_list->session_name) … … 270 280 { 271 281 fprintf(stderr, "Failed to attach!\n"); 282 free(screen_list->s_socket_path); 283 screen_list->s_socket_path = NULL; 272 284 attach = 0; 273 285 } … … 298 310 } 299 311 } 312 if(!screen_list->c_socket_path) 313 screen_list->c_socket_path = 314 build_socket_path(screen_list->socket_dir, screen_list->session_name, 1); 315 316 if(!screen_list->s_socket_path) 317 screen_list->s_socket_path = 318 build_socket_path(screen_list->socket_dir, screen_list->session_name, 0); 300 319 301 320 /* Fork the server if needed */ … … 303 322 { 304 323 pid_t pid; 305 306 screen_list->socket_path = build_socket_path(screen_list->socket_path, screen_list->session_name);307 324 308 325 pid = fork(); … … 337 354 return server_main(to_grab, to_start, screen_list); 338 355 } 339 340 free(connect_server(screen_list ->socket_path, screen_list));341 } 342 343 /* Create main canvas and associated caca window */344 screen_list->cv = cucul_create_canvas(0, 0);345 screen_list->dp = caca_create_display(screen_list->cv);346 if(!screen_list->dp)347 return 1;348 caca_set_cursor(screen_list->dp, 1); 349 screen_list->width = cucul_get_canvas_width(screen_list->cv);350 screen_list->height = cucul_get_canvas_height(screen_list->cv) - ((screen_list->mini*6) + (screen_list->status));356 create_client_socket(screen_list); 357 free(connect_server(screen_list)); 358 359 /* Create main canvas and associated caca window */ 360 screen_list->cv = cucul_create_canvas(0, 0); 361 screen_list->dp = caca_create_display(screen_list->cv); 362 if(!screen_list->dp) 363 return 1; 364 caca_set_cursor(screen_list->dp, 1); 365 366 request_attach(screen_list); 367 } 351 368 352 369 last_key_time = get_us(); … … 357 374 int ret = 0; 358 375 ssize_t n; 359 char buf[ 4097];360 361 while (screen_list-> socket && (n = read(screen_list->socket, buf, sizeof(buf)-1)) > 0)376 char buf[128*1024]; 377 378 while (screen_list->c_socket && (n = read(screen_list->c_socket, buf, sizeof(buf)-1)) > 0) 362 379 { 363 380 buf[n] = 0; … … 368 385 break; 369 386 } 370 else if(!strncmp("REFRESH", buf, 7)) 371 { 372 /* FIXME update the canvas first */ 387 else if(!strncmp("REFRESH ", buf, 8)) 388 { 389 cucul_import_memory(screen_list->cv, buf+8, n-8, "caca"); 390 373 391 caca_refresh_display(screen_list->dp); 374 392 } … … 382 400 caca_refresh_display(screen_list->dp); 383 401 } 402 else 403 { 404 debug("Unknown message received from server: %s", buf); 405 } 384 406 } 385 407 if(ret) … … 388 410 ret = caca_get_event(screen_list->dp, CACA_EVENT_ANY, &ev, 100000); 389 411 if(ret) 390 ret = send_event(ev, screen_list ->socket);412 ret = send_event(ev, screen_list); 391 413 392 414 if(ret) … … 405 427 } 406 428 407 if(screen_list->socket_path) { 408 free(screen_list->socket_path); 409 } 410 411 if(screen_list->socket) 412 close(screen_list->socket); 429 if(screen_list->s_socket_path) 430 free(screen_list->s_socket_path); 431 432 if(screen_list->c_socket_path) 433 { 434 unlink(screen_list->c_socket_path); 435 free(screen_list->c_socket_path); 436 } 437 438 if(screen_list->c_socket) 439 close(screen_list->c_socket); 440 441 if(screen_list->s_socket) 442 close(screen_list->s_socket); 413 443 414 444 if(screen_list->screen) free(screen_list->screen); … … 479 509 screen_list->lock_offset = 0; 480 510 screen_list->attached = 1; 481 screen_list->socket = 0; 511 screen_list->s_socket = 0; 512 screen_list->c_socket = 0; 482 513 screen_list->socket_dir = NULL; 483 screen_list->socket_path = NULL; 514 screen_list->s_socket_path = NULL; 515 screen_list->c_socket_path = NULL; 484 516 screen_list->session_name = NULL; 485 517 screen_list->default_shell = NULL; -
neercs/trunk/src/neercs.h
r2588 r2589 147 147 /* Detaching */ 148 148 int attached; /* Are we attached to a terminal */ 149 int socket; /* Socket to ask for attaching */ 150 char *socket_path; /* Socket to ask for attaching */ 149 int s_socket; /* Socket to write to the server */ 150 int c_socket; /* Socket to write to the client */ 151 char *s_socket_path; /* Socket to write to the server */ 152 char *c_socket_path; /* Socket to write to the client */ 151 153 char *socket_dir; /* Where to create the socket */ 152 154 char *session_name; /* Name of the session */ … … 218 220 int detach(struct screen_list* screen_list); 219 221 int request_attach(struct screen_list* screen_list); 220 char * build_socket_path(char *socket_dir, char *session_name); 221 int create_socket(struct screen_list* screen_list); 222 char * connect_server(char *socket_path, struct screen_list* screen_list); 222 char * build_socket_path(char *socket_dir, char *session_name, int client); 223 int create_client_socket(struct screen_list* screen_list); 224 int create_server_socket(struct screen_list* screen_list); 225 int connect_client(struct screen_list* screen_list); 226 char * connect_server(struct screen_list* screen_list); 223 227 char ** list_sockets(char *socket_dir, char *session_name); 224 228 int server_main(int *to_grab, char **to_start, struct screen_list *screen_list); 225 int send_event(caca_event_t ev, int sock);229 int send_event(caca_event_t ev, struct screen_list* screen_list); 226 230 227 231 /* Screens management */ -
neercs/trunk/src/server.c
r2588 r2589 35 35 #include "neercs.h" 36 36 37 static int send_to_client(const char * msg, int sock) 38 { 39 if(strcmp(msg, "REFRESH")) 40 debug("Sending message (%s) to client on socket %d", msg, sock); 41 return 0; 42 } 43 44 static int set_title(const char * title, int sock) 37 static int send_to_client(const char * msg, struct screen_list* screen_list) 38 { 39 int ret; 40 if(!screen_list->c_socket) 41 connect_client(screen_list); 42 debug("Sending message (%s) to client on socket %d", msg, screen_list->c_socket); 43 if(!screen_list->c_socket) 44 ret = -1; 45 else 46 ret = write(screen_list->c_socket, msg, strlen(msg)); 47 if(ret < 0) 48 fprintf(stderr, "Failed to send message to client: %s\n", strerror(errno)); 49 return ret; 50 } 51 52 static int set_title(const char * title, struct screen_list* screen_list) 45 53 { 46 54 char buf[1024]; … … 50 58 buf[bytes] = '\0'; 51 59 52 return send_to_client(buf, s ock);53 } 54 55 static int set_cursor(int state, int sock)60 return send_to_client(buf, screen_list); 61 } 62 63 static int set_cursor(int state, struct screen_list* screen_list) 56 64 { 57 65 char buf[16]; … … 61 69 buf[bytes] = '\0'; 62 70 63 return send_to_client(buf, sock); 64 } 65 66 static int request_refresh(int sock) 67 { 68 return send_to_client("REFRESH", sock); 71 return send_to_client(buf, screen_list); 72 } 73 74 static int request_refresh(struct screen_list* screen_list) 75 { 76 int bytes; 77 void *buf; 78 char *buf2; 79 debug("Resquesting refresh"); 80 buf = cucul_export_memory (screen_list->cv, "caca", &bytes); 81 buf2 = malloc(bytes+8); 82 memcpy(buf2, "REFRESH ", 8); 83 memcpy(buf2+8, buf, bytes); 84 if(!screen_list->c_socket) 85 connect_client(screen_list); 86 if(screen_list->c_socket) 87 write(screen_list->c_socket, buf2, bytes+8); 88 free(buf); 89 free(buf2); 90 return 0; 69 91 } 70 92 … … 72 94 { 73 95 screen_list->attached = 0; 74 return send_to_client("DETACH", screen_list ->socket);96 return send_to_client("DETACH", screen_list); 75 97 } 76 98 … … 85 107 86 108 /* Create socket and bind it */ 87 create_socket(screen_list); 109 create_server_socket(screen_list); 110 connect_client(screen_list); 88 111 89 112 screen_list->width = screen_list->height = 10; … … 146 169 147 170 /* Check if we got something from the client */ 148 while (screen_list->s ocket && (n = read(screen_list->socket, buf, sizeof(buf)-1)) > 0)171 while (screen_list->s_socket && (n = read(screen_list->s_socket, buf, sizeof(buf)-1)) > 0) 149 172 { 150 173 buf[n] = 0; 151 174 debug("Received command %s", buf); 152 if(!strncmp("ATTACH ", buf, 7))175 if(!strncmp("ATTACH ", buf, 7)) 153 176 { 154 177 screen_list->attached = 1; 178 cucul_free_canvas(screen_list->cv); 179 screen_list->cv = cucul_create_canvas(atoi(buf+7), atoi(buf+18)); 180 screen_list->width = cucul_get_canvas_width(screen_list->cv); 181 screen_list->height = cucul_get_canvas_height(screen_list->cv) - ((screen_list->mini*6) + (screen_list->status)); 182 update_windows_props(screen_list); 183 cucul_clear_canvas(screen_list->cv); 155 184 refresh = 1; 156 185 } … … 159 188 quit = 1; 160 189 } 161 else if(!strncmp("RESIZE", buf, 6)) 162 { 190 else if(!strncmp("RESIZE ", buf, 7)) 191 { 192 cucul_free_canvas(screen_list->cv); 193 screen_list->cv = cucul_create_canvas(atoi(buf+7), atoi(buf+18)); 194 screen_list->width = cucul_get_canvas_width(screen_list->cv); 195 screen_list->height = cucul_get_canvas_height(screen_list->cv) - ((screen_list->mini*6) + (screen_list->status)); 163 196 update_windows_props(screen_list); 164 197 cucul_clear_canvas(screen_list->cv); … … 181 214 /* Not in command mode */ 182 215 last_key_time = get_us(); 183 set_cursor(1, screen_list ->socket);216 set_cursor(1, screen_list); 184 217 185 218 /* Kill screensaver */ … … 262 295 if(screen_list->attached) 263 296 if(screen_list->screen[screen_list->pty]->title) 264 set_title(screen_list->screen[screen_list->pty]->title, screen_list ->socket);297 set_title(screen_list->screen[screen_list->pty]->title, screen_list); 265 298 else 266 set_title(PACKAGE_STRING, screen_list ->socket);299 set_title(PACKAGE_STRING, screen_list); 267 300 refresh = 1; 268 301 … … 274 307 screensaver_init(screen_list); 275 308 screen_list->in_screensaver = 1; 276 set_cursor(0, screen_list ->socket);309 set_cursor(0, screen_list); 277 310 } 278 311 draw_screensaver(screen_list); … … 291 324 { 292 325 if(screen_list->attached) 293 request_refresh(screen_list ->socket);326 request_refresh(screen_list); 294 327 refresh = 0; 295 328 } … … 313 346 } 314 347 315 if(screen_list->socket_path) { 316 unlink(screen_list->socket_path); 317 free(screen_list->socket_path); 318 } 319 320 if(screen_list->socket) 321 close(screen_list->socket); 348 if(screen_list->s_socket_path) 349 { 350 unlink(screen_list->s_socket_path); 351 free(screen_list->s_socket_path); 352 } 353 354 if(screen_list->c_socket_path) 355 free(screen_list->c_socket_path); 356 357 if(screen_list->s_socket) 358 close(screen_list->s_socket); 359 360 if(screen_list->c_socket) 361 close(screen_list->c_socket); 322 362 323 363 if(screen_list->screen) free(screen_list->screen);
Note: See TracChangeset
for help on using the changeset viewer.