1 <!-- ##### SECTION Title ##### --> 2 IO Channels 3 4 <!-- ##### SECTION Short_Description ##### --> 5 portable support for using files, pipes and sockets 6 7 <!-- ##### SECTION Long_Description ##### --> 8 <para> 9 The #GIOChannel data type aims to provide a portable method for using file 10 descriptors, pipes, and sockets, and integrating them into the 11 <link linkend="glib-The-Main-Event-Loop">main event loop</link>. 12 Currently full support is available on UNIX platforms, support for 13 Windows is only partially complete. 14 </para> 15 <para> 16 To create a new #GIOChannel on UNIX systems use g_io_channel_unix_new(). 17 This works for plain file descriptors, pipes and sockets. 18 Alternatively, a channel can be created for a file in a system independent 19 manner using g_io_channel_new_file(). 20 </para> 21 <para> 22 Once a #GIOChannel has been created, it can be used in a generic manner 23 with the functions g_io_channel_read_chars(), g_io_channel_write_chars(), 24 g_io_channel_seek_position(), and g_io_channel_shutdown(). 25 </para> 26 <para> 27 To add a #GIOChannel to the 28 <link linkend="glib-The-Main-Event-Loop">main event loop</link> 29 use g_io_add_watch() or g_io_add_watch_full(). Here you specify which events 30 you are interested in on the #GIOChannel, and provide a function to be 31 called whenever these events occur. 32 </para> 33 <para> 34 #GIOChannel instances are created with an initial reference count of 1. 35 g_io_channel_ref() and g_io_channel_unref() can be used to increment or 36 decrement the reference count respectively. When the reference count falls 37 to 0, the #GIOChannel is freed. (Though it isn't closed automatically, 38 unless it was created using g_io_channel_new_from_file().) 39 Using g_io_add_watch() or g_io_add_watch_full() increments a channel's 40 reference count. 41 </para> 42 <para> 43 The new functions g_io_channel_read_chars(), g_io_channel_read_line(), 44 g_io_channel_read_line_string(), g_io_channel_read_to_end(), 45 g_io_channel_write_chars(), g_io_channel_seek_position(), 46 and g_io_channel_flush() should not be mixed with the 47 deprecated functions g_io_channel_read(), g_io_channel_write(), 48 and g_io_channel_seek() on the same channel. 49 </para> 50 51 <!-- ##### SECTION See_Also ##### --> 52 <para> 53 <variablelist> 54 55 <varlistentry> 56 <term>gtk_input_add_full(), gtk_input_remove(), gdk_input_add(), 57 gdk_input_add_full(), gdk_input_remove()</term> 58 <listitem><para> 59 Convenience functions for creating #GIOChannel instances and adding them to the 60 <link linkend="glib-The-Main-Event-Loop">main event loop</link>. 61 </para></listitem> 62 </varlistentry> 63 64 </variablelist> 65 </para> 66 67 <!-- ##### SECTION Stability_Level ##### --> 68 69 70 <!-- ##### STRUCT GIOChannel ##### --> 71 <para> 72 A data structure representing an IO Channel. The fields should be considered 73 private and should only be accessed with the following functions. 74 </para> 75 76 77 <!-- ##### FUNCTION g_io_channel_unix_new ##### --> 78 <para> 79 Creates a new #GIOChannel given a file descriptor. 80 On UNIX systems this works for plain files, pipes, and sockets. 81 </para> 82 <para> 83 The returned #GIOChannel has a reference count of 1. 84 </para> 85 <para> 86 The default encoding for #GIOChannel is UTF-8. If your application 87 is reading output from a command using via pipe, you may need to 88 set the encoding to the encoding of the current locale (see 89 g_get_charset()) with the g_io_channel_set_encoding() function. 90 </para> 91 <para> 92 If you want to read raw binary data without interpretation, then 93 call the g_io_channel_set_encoding() function with %NULL for the 94 encoding argument. 95 </para> 96 <para> 97 This function is available in GLib on Windows, too, but you should 98 avoid using it on Windows. The domain of file descriptors and sockets 99 overlap. There is no way for GLib to know which one you mean in case 100 the argument you pass to this function happens to be both a valid file 101 descriptor and socket. If that happens a warning is issued, and GLib 102 assumes that it is the file descriptor you mean. 103 </para> 104 105 @fd: a file descriptor. 106 @Returns: a new #GIOChannel. 107 108 109 <!-- ##### FUNCTION g_io_channel_unix_get_fd ##### --> 110 <para> 111 Returns the file descriptor of the #GIOChannel. 112 </para> 113 <para> 114 On Windows this function returns the file descriptor or socket of the #GIOChannel. 115 </para> 116 117 @channel: a #GIOChannel, created with g_io_channel_unix_new(). 118 @Returns: the file descriptor of the #GIOChannel. 119 120 121 <!-- ##### FUNCTION g_io_channel_win32_new_fd ##### --> 122 <para> 123 Creates a new #GIOChannel given a file descriptor on Windows. 124 This works for file descriptors from the C runtime. 125 </para> 126 <para> 127 This function works for file descriptors as returned by the open(), 128 creat(), pipe() and fileno() calls in the Microsoft C runtime. In 129 order to meaningfully use this function your code should use the same 130 C runtime as GLib uses, which is msvcrt.dll. Note that in current 131 Microsoft compilers it is near impossible to convince it to build code 132 that would use msvcrt.dll. The last Microsoft compiler version that 133 supported using msvcrt.dll as the C runtime was version 6. The GNU 134 compiler and toolchain for Windows, also known as Mingw, fully 135 supports msvcrt.dll. 136 </para> 137 <para> 138 If you have created a #GIOChannel for a file descriptor and started 139 watching (polling) it, you shouldn't call read() on the file 140 descriptor. This is because adding polling for a file descriptor is 141 implemented in GLib on Windows by starting a thread that sits blocked 142 in a read() from the file descriptor most of the time. All reads from 143 the file descriptor should be done by this internal GLib thread. Your 144 code should call only g_io_channel_read(). 145 </para> 146 <para> 147 This function is available only in GLib on Windows. 148 </para> 149 150 @fd: a C library file descriptor. 151 @Returns: a new #GIOChannel. 152 153 154 <!-- ##### FUNCTION g_io_channel_win32_new_socket ##### --> 155 <para> 156 Creates a new #GIOChannel given a socket on Windows. 157 </para> 158 <para> 159 This function works for sockets created by Winsock. 160 It's available only in GLib on Windows. 161 </para> 162 <para> 163 Polling a #GSource created to watch a channel for a socket 164 puts the socket in non-blocking mode. This is a side-effect 165 of the implementation and unavoidable. 166 </para> 167 168 @socket: a Winsock socket 169 @Returns: a new #GIOChannel 170 171 172 <!-- ##### FUNCTION g_io_channel_win32_new_messages ##### --> 173 <para> 174 Creates a new #GIOChannel given a window handle on Windows. 175 </para> 176 <para> 177 This function creates a #GIOChannel that can be used to poll for 178 Windows messages for the window in question. 179 </para> 180 181 @hwnd: a window handle. 182 @Returns: a new #GIOChannel. 183 184 185 <!-- ##### FUNCTION g_io_channel_init ##### --> 186 <para> 187 188 </para> 189 190 @channel: 191 192 193 <!-- ##### FUNCTION g_io_channel_new_file ##### --> 194 <para> 195 </para> 196 197 @filename: 198 @mode: 199 @error: 200 @Returns: 201 202 203 <!-- ##### FUNCTION g_io_channel_read_chars ##### --> 204 <para> 205 206 </para> 207 208 @channel: 209 @buf: 210 @count: 211 @bytes_read: 212 @error: 213 @Returns: 214 215 216 <!-- ##### FUNCTION g_io_channel_read_unichar ##### --> 217 <para> 218 219 </para> 220 221 @channel: 222 @thechar: 223 @error: 224 @Returns: 225 226 227 <!-- ##### FUNCTION g_io_channel_read_line ##### --> 228 <para> 229 230 </para> 231 232 @channel: 233 @str_return: 234 @length: 235 @terminator_pos: 236 @error: 237 @Returns: 238 239 240 <!-- ##### FUNCTION g_io_channel_read_line_string ##### --> 241 <para> 242 243 </para> 244 245 @channel: 246 @buffer: 247 @terminator_pos: 248 @error: 249 @Returns: 250 251 252 <!-- ##### FUNCTION g_io_channel_read_to_end ##### --> 253 <para> 254 255 </para> 256 257 @channel: 258 @str_return: 259 @length: 260 @error: 261 @Returns: 262 263 264 <!-- ##### FUNCTION g_io_channel_write_chars ##### --> 265 <para> 266 267 </para> 268 269 @channel: 270 @buf: 271 @count: 272 @bytes_written: 273 @error: 274 @Returns: 275 276 277 <!-- ##### FUNCTION g_io_channel_write_unichar ##### --> 278 <para> 279 280 </para> 281 282 @channel: 283 @thechar: 284 @error: 285 @Returns: 286 287 288 <!-- ##### FUNCTION g_io_channel_flush ##### --> 289 <para> 290 291 </para> 292 293 @channel: 294 @error: 295 @Returns: 296 297 298 <!-- ##### FUNCTION g_io_channel_seek_position ##### --> 299 <para> 300 301 </para> 302 303 @channel: 304 @offset: 305 @type: 306 @error: 307 @Returns: 308 309 310 <!-- ##### ENUM GSeekType ##### --> 311 <para> 312 An enumeration specifying the base position for a g_io_channel_seek_position() 313 operation. 314 </para> 315 316 @G_SEEK_CUR: the current position in the file. 317 @G_SEEK_SET: the start of the file. 318 @G_SEEK_END: the end of the file. 319 320 <!-- ##### FUNCTION g_io_channel_shutdown ##### --> 321 <para> 322 323 </para> 324 325 @channel: 326 @flush: 327 @err: 328 @Returns: 329 330 331 <!-- ##### ENUM GIOStatus ##### --> 332 <para> 333 Stati returned by most of the #GIOFuncs functions. 334 </para> 335 336 @G_IO_STATUS_ERROR: An error occurred. 337 @G_IO_STATUS_NORMAL: Success. 338 @G_IO_STATUS_EOF: End of file. 339 @G_IO_STATUS_AGAIN: Resource temporarily unavailable. 340 341 <!-- ##### ENUM GIOChannelError ##### --> 342 <para> 343 Error codes returned by #GIOChannel operations. 344 </para> 345 346 @G_IO_CHANNEL_ERROR_FBIG: File too large. 347 @G_IO_CHANNEL_ERROR_INVAL: Invalid argument. 348 @G_IO_CHANNEL_ERROR_IO: IO error. 349 @G_IO_CHANNEL_ERROR_ISDIR: File is a directory. 350 @G_IO_CHANNEL_ERROR_NOSPC: No space left on device. 351 @G_IO_CHANNEL_ERROR_NXIO: No such device or address. 352 @G_IO_CHANNEL_ERROR_OVERFLOW: Value too large for defined datatype. 353 @G_IO_CHANNEL_ERROR_PIPE: Broken pipe. 354 @G_IO_CHANNEL_ERROR_FAILED: Some other error. 355 356 <!-- ##### MACRO G_IO_CHANNEL_ERROR ##### --> 357 <para> 358 Error domain for #GIOChannel operations. Errors in this domain will 359 be from the #GIOChannelError enumeration. See #GError for information on 360 error domains. 361 </para> 362 363 364 365 <!-- ##### FUNCTION g_io_channel_error_from_errno ##### --> 366 <para> 367 368 </para> 369 370 @en: 371 @Returns: 372 373 374 <!-- ##### FUNCTION g_io_channel_ref ##### --> 375 <para> 376 377 </para> 378 379 @channel: 380 @Returns: 381 382 383 <!-- ##### FUNCTION g_io_channel_unref ##### --> 384 <para> 385 386 </para> 387 388 @channel: 389 390 391 <!-- ##### FUNCTION g_io_create_watch ##### --> 392 <para> 393 394 </para> 395 396 @channel: 397 @condition: 398 @Returns: 399 400 401 <!-- ##### FUNCTION g_io_add_watch ##### --> 402 <para> 403 404 </para> 405 406 @channel: 407 @condition: 408 @func: 409 @user_data: 410 @Returns: 411 412 413 <!-- ##### FUNCTION g_io_add_watch_full ##### --> 414 <para> 415 416 </para> 417 418 @channel: 419 @priority: 420 @condition: 421 @func: 422 @user_data: 423 @notify: 424 @Returns: 425 426 427 <!-- ##### ENUM GIOCondition ##### --> 428 <para> 429 A bitwise combination representing a condition to watch for on 430 an event source. 431 </para> 432 433 @G_IO_IN: There is data to read. 434 @G_IO_OUT: Data can be written (without blocking). 435 @G_IO_PRI: There is urgent data to read. 436 @G_IO_ERR: Error condition. 437 @G_IO_HUP: Hung up (the connection has been broken, usually for pipes 438 and sockets). 439 @G_IO_NVAL: Invalid request. The file descriptor is not open. 440 441 <!-- ##### USER_FUNCTION GIOFunc ##### --> 442 <para> 443 Specifies the type of function passed to g_io_add_watch() or 444 g_io_add_watch_full(), which is called when the requested 445 condition on a #GIOChannel is satisfied. 446 </para> 447 448 @source: the #GIOChannel event source 449 @condition: the condition which has been satisfied 450 @data: user data set in g_io_add_watch() or g_io_add_watch_full() 451 @Returns: the function should return %FALSE if the event source 452 should be removed 453 454 455 <!-- ##### STRUCT GIOFuncs ##### --> 456 <para> 457 A table of functions used to handle different types of #GIOChannel 458 in a generic way. 459 </para> 460 461 @io_read: 462 @io_write: 463 @io_seek: 464 @io_close: 465 @io_create_watch: 466 @io_free: 467 @io_set_flags: 468 @io_get_flags: 469 470 <!-- ##### FUNCTION g_io_channel_get_buffer_size ##### --> 471 <para> 472 473 </para> 474 475 @channel: 476 @Returns: 477 478 479 <!-- ##### FUNCTION g_io_channel_set_buffer_size ##### --> 480 <para> 481 482 </para> 483 484 @channel: 485 @size: 486 487 488 <!-- ##### FUNCTION g_io_channel_get_buffer_condition ##### --> 489 <para> 490 491 </para> 492 493 @channel: 494 @Returns: 495 496 497 <!-- ##### FUNCTION g_io_channel_get_flags ##### --> 498 <para> 499 500 </para> 501 502 @channel: 503 @Returns: 504 505 506 <!-- ##### FUNCTION g_io_channel_set_flags ##### --> 507 <para> 508 509 </para> 510 511 @channel: 512 @flags: 513 @error: 514 @Returns: 515 516 517 <!-- ##### ENUM GIOFlags ##### --> 518 <para> 519 Specifies properties of a #GIOChannel. Some of the flags can only 520 be read with g_io_channel_get_flags(), but not changed with 521 g_io_channel_set_flags(). 522 </para> 523 524 @G_IO_FLAG_APPEND: turns on append mode, corresponds to %O_APPEND 525 (see the documentation of the UNIX open() syscall). 526 @G_IO_FLAG_NONBLOCK: turns on nonblocking mode, corresponds to 527 %O_NONBLOCK/%O_NDELAY (see the documentation of the UNIX open() 528 syscall). 529 @G_IO_FLAG_IS_READABLE: indicates that the io channel is readable. 530 This flag can not be changed. 531 @G_IO_FLAG_IS_WRITEABLE: indicates that the io channel is writable. 532 This flag can not be changed. 533 @G_IO_FLAG_IS_SEEKABLE: indicates that the io channel is seekable, 534 i.e. that g_io_channel_seek_position() can be used on it. 535 This flag can not be changed. 536 @G_IO_FLAG_MASK: 537 @G_IO_FLAG_GET_MASK: 538 @G_IO_FLAG_SET_MASK: 539 540 <!-- ##### FUNCTION g_io_channel_get_line_term ##### --> 541 <para> 542 543 </para> 544 545 @channel: 546 @length: 547 @Returns: 548 549 550 <!-- ##### FUNCTION g_io_channel_set_line_term ##### --> 551 <para> 552 553 </para> 554 555 @channel: 556 @line_term: 557 @length: 558 559 560 <!-- ##### FUNCTION g_io_channel_get_buffered ##### --> 561 <para> 562 563 </para> 564 565 @channel: 566 @Returns: 567 568 569 <!-- ##### FUNCTION g_io_channel_set_buffered ##### --> 570 <para> 571 572 </para> 573 574 @channel: 575 @buffered: 576 577 578 <!-- ##### FUNCTION g_io_channel_get_encoding ##### --> 579 <para> 580 581 </para> 582 583 @channel: 584 @Returns: 585 586 587 <!-- ##### FUNCTION g_io_channel_set_encoding ##### --> 588 <para> 589 590 </para> 591 592 @channel: 593 @encoding: 594 @error: 595 @Returns: 596 597 598 <!-- ##### FUNCTION g_io_channel_get_close_on_unref ##### --> 599 <para> 600 601 </para> 602 603 @channel: 604 @Returns: 605 606 607 <!-- ##### FUNCTION g_io_channel_set_close_on_unref ##### --> 608 <para> 609 610 </para> 611 612 @channel: 613 @do_close: 614 615 616 <!-- ##### FUNCTION g_io_channel_read ##### --> 617 <para> 618 </para> 619 620 @channel: 621 @buf: 622 @count: 623 @bytes_read: 624 @Returns: 625 626 627 <!-- ##### ENUM GIOError ##### --> 628 <para> 629 #GIOError is only used by the deprecated functions g_io_channel_read(), 630 g_io_channel_write(), and g_io_channel_seek(). 631 </para> 632 633 @G_IO_ERROR_NONE: no error 634 @G_IO_ERROR_AGAIN: an EAGAIN error occurred 635 @G_IO_ERROR_INVAL: an EINVAL error occurred 636 @G_IO_ERROR_UNKNOWN: another error occurred 637 638 <!-- ##### FUNCTION g_io_channel_write ##### --> 639 <para> 640 </para> 641 642 @channel: 643 @buf: 644 @count: 645 @bytes_written: 646 @Returns: 647 648 649 <!-- ##### FUNCTION g_io_channel_seek ##### --> 650 <para> 651 </para> 652 653 @channel: 654 @offset: 655 @type. 656 @type: 657 @Returns: 658 659 660 <!-- ##### FUNCTION g_io_channel_close ##### --> 661 <para> 662 </para> 663 664 @channel: 665 666 667