Changes in uspace/srv/hid/remcons/remcons.c [8dc9b72:1c635d6] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/hid/remcons/remcons.c
r8dc9b72 r1c635d6 44 44 #include <fibril_synch.h> 45 45 #include <task.h> 46 #include < inet/addr.h>47 #include < inet/endpoint.h>48 #include < inet/tcp.h>46 #include <net/in.h> 47 #include <net/inet.h> 48 #include <net/socket.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_conn105 };106 107 static tcp_cb_t conn_cb = {108 .connected = NULL109 };110 111 101 static telnet_user_t *srv_to_user(con_srv_t *srv) 112 102 { … … 121 111 122 112 /* Force character mode. */ 123 (void) tcp_conn_send(user->conn, (void *)telnet_force_character_mode_command,124 telnet_force_character_mode_command_count );113 send(user->socket, (void *)telnet_force_character_mode_command, 114 telnet_force_character_mode_command_count, 0); 125 115 126 116 return EOK; … … 282 272 } 283 273 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); 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; 300 281 301 282 int rc = loc_service_register(user->service_name, &user->service_id); … … 303 284 telnet_user_error(user, "Unable to register %s with loc: %s.", 304 285 user->service_name, str_error(rc)); 305 return ;286 return EOK; 306 287 } 307 288 308 289 telnet_user_log(user, "Service %s registerd with id %" PRIun ".", 309 290 user->service_name, user->service_id); 310 291 311 292 fid_t spawn_fibril = fibril_create(spawn_task_fibril, user); 312 293 assert(spawn_fibril); 313 294 fibril_add_ready(spawn_fibril); 314 295 315 296 /* Wait for all clients to exit. */ 316 297 fibril_mutex_lock(&user->guard); 317 298 while (!user_can_be_destroyed_no_lock(user)) { 318 299 if (user->task_finished) { 319 user->conn = NULL;300 closesocket(user->socket); 320 301 user->socket_closed = true; 321 302 user->srvs.aborted = true; … … 329 310 } 330 311 fibril_mutex_unlock(&user->guard); 331 312 332 313 rc = loc_service_unregister(user->service_id); 333 314 if (rc != EOK) { … … 339 320 telnet_user_log(user, "Destroying..."); 340 321 telnet_user_destroy(user); 322 323 return EOK; 341 324 } 342 325 343 326 int main(int argc, char *argv[]) 344 327 { 345 int rc; 346 tcp_listener_t *lst; 347 tcp_t *tcp; 348 inet_ep_t ep; 349 328 int port = 2223; 329 350 330 async_set_client_connection(client_connection); 351 rc = loc_server_register(NAME);331 int rc = loc_server_register(NAME); 352 332 if (rc != EOK) { 353 333 fprintf(stderr, "%s: Unable to register server\n", NAME); 354 334 return rc; 355 335 } 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; 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; 371 368 } 372 369 373 370 printf("%s: HelenOS Remote console service\n", NAME); 374 371 task_retval(0); 375 async_manager(); 376 377 /* Not reached */ 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 378 400 return 0; 379 401 }
Note:
See TracChangeset
for help on using the changeset viewer.