Changes in uspace/srv/hid/remcons/remcons.c [1c635d6:8dc9b72] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/hid/remcons/remcons.c
r1c635d6 r8dc9b72 44 44 #include <fibril_synch.h> 45 45 #include <task.h> 46 #include < net/in.h>47 #include < net/inet.h>48 #include < net/socket.h>46 #include <inet/addr.h> 47 #include <inet/endpoint.h> 48 #include <inet/tcp.h> 49 49 #include <io/console.h> 50 50 #include <inttypes.h> … … 99 99 }; 100 100 101 static void remcons_new_conn(tcp_listener_t *lst, tcp_conn_t *conn); 102 103 static tcp_listen_cb_t listen_cb = { 104 .new_conn = remcons_new_conn 105 }; 106 107 static tcp_cb_t conn_cb = { 108 .connected = NULL 109 }; 110 101 111 static telnet_user_t *srv_to_user(con_srv_t *srv) 102 112 { … … 111 121 112 122 /* Force character mode. */ 113 send(user->socket, (void *)telnet_force_character_mode_command,114 telnet_force_character_mode_command_count , 0);123 (void) tcp_conn_send(user->conn, (void *)telnet_force_character_mode_command, 124 telnet_force_character_mode_command_count); 115 125 116 126 return EOK; … … 272 282 } 273 283 274 /** Fibril for each accepted socket. 275 * 276 * @param arg Corresponding @c telnet_user_t structure. 277 */ 278 static int network_user_fibril(void *arg) 279 { 280 telnet_user_t *user = arg; 284 /** Handle network connection. 285 * 286 * @param lst Listener 287 * @param conn Connection 288 */ 289 static void remcons_new_conn(tcp_listener_t *lst, tcp_conn_t *conn) 290 { 291 telnet_user_t *user = telnet_user_create(conn); 292 assert(user); 293 294 con_srvs_init(&user->srvs); 295 user->srvs.ops = &con_ops; 296 user->srvs.sarg = user; 297 user->srvs.abort_timeout = 1000; 298 299 telnet_user_add(user); 281 300 282 301 int rc = loc_service_register(user->service_name, &user->service_id); … … 284 303 telnet_user_error(user, "Unable to register %s with loc: %s.", 285 304 user->service_name, str_error(rc)); 286 return EOK;305 return; 287 306 } 288 307 289 308 telnet_user_log(user, "Service %s registerd with id %" PRIun ".", 290 309 user->service_name, user->service_id); 291 310 292 311 fid_t spawn_fibril = fibril_create(spawn_task_fibril, user); 293 312 assert(spawn_fibril); 294 313 fibril_add_ready(spawn_fibril); 295 314 296 315 /* Wait for all clients to exit. */ 297 316 fibril_mutex_lock(&user->guard); 298 317 while (!user_can_be_destroyed_no_lock(user)) { 299 318 if (user->task_finished) { 300 closesocket(user->socket);319 user->conn = NULL; 301 320 user->socket_closed = true; 302 321 user->srvs.aborted = true; … … 310 329 } 311 330 fibril_mutex_unlock(&user->guard); 312 331 313 332 rc = loc_service_unregister(user->service_id); 314 333 if (rc != EOK) { … … 320 339 telnet_user_log(user, "Destroying..."); 321 340 telnet_user_destroy(user); 322 323 return EOK;324 341 } 325 342 326 343 int main(int argc, char *argv[]) 327 344 { 328 int port = 2223; 329 345 int rc; 346 tcp_listener_t *lst; 347 tcp_t *tcp; 348 inet_ep_t ep; 349 330 350 async_set_client_connection(client_connection); 331 intrc = loc_server_register(NAME);351 rc = loc_server_register(NAME); 332 352 if (rc != EOK) { 333 353 fprintf(stderr, "%s: Unable to register server\n", NAME); 334 354 return rc; 335 355 } 336 337 struct sockaddr_in addr; 338 339 addr.sin_family = AF_INET; 340 addr.sin_port = htons(port); 341 342 rc = inet_pton(AF_INET, "127.0.0.1", (void *) 343 &addr.sin_addr.s_addr); 344 if (rc != EOK) { 345 fprintf(stderr, "Error parsing network address: %s.\n", 346 str_error(rc)); 347 return 2; 348 } 349 350 int listen_sd = socket(PF_INET, SOCK_STREAM, 0); 351 if (listen_sd < 0) { 352 fprintf(stderr, "Error creating listening socket: %s.\n", 353 str_error(listen_sd)); 354 return 3; 355 } 356 357 rc = bind(listen_sd, (struct sockaddr *) &addr, sizeof(addr)); 358 if (rc != EOK) { 359 fprintf(stderr, "Error binding socket: %s.\n", 360 str_error(rc)); 361 return 4; 362 } 363 364 rc = listen(listen_sd, BACKLOG_SIZE); 365 if (rc != EOK) { 366 fprintf(stderr, "listen() failed: %s.\n", str_error(rc)); 367 return 5; 356 357 rc = tcp_create(&tcp); 358 if (rc != EOK) { 359 fprintf(stderr, "%s: Error initializing TCP.\n", NAME); 360 return rc; 361 } 362 363 inet_ep_init(&ep); 364 ep.port = 2223; 365 366 rc = tcp_listener_create(tcp, &ep, &listen_cb, NULL, &conn_cb, NULL, 367 &lst); 368 if (rc != EOK) { 369 fprintf(stderr, "%s: Error creating listener.\n", NAME); 370 return rc; 368 371 } 369 372 370 373 printf("%s: HelenOS Remote console service\n", NAME); 371 374 task_retval(0); 372 373 while (true) { 374 struct sockaddr_in raddr; 375 socklen_t raddr_len = sizeof(raddr); 376 int conn_sd = accept(listen_sd, (struct sockaddr *) &raddr, 377 &raddr_len); 378 379 if (conn_sd < 0) { 380 fprintf(stderr, "accept() failed: %s.\n", 381 str_error(rc)); 382 continue; 383 } 384 385 telnet_user_t *user = telnet_user_create(conn_sd); 386 assert(user); 387 388 con_srvs_init(&user->srvs); 389 user->srvs.ops = &con_ops; 390 user->srvs.sarg = user; 391 user->srvs.abort_timeout = 1000; 392 393 telnet_user_add(user); 394 395 fid_t fid = fibril_create(network_user_fibril, user); 396 assert(fid); 397 fibril_add_ready(fid); 398 } 399 375 async_manager(); 376 377 /* Not reached */ 400 378 return 0; 401 379 }
Note:
See TracChangeset
for help on using the changeset viewer.