Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 1267 lines (910 sloc) 50.91 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267

/* vim: set et ts=4 sw=4 ft=cpp:
*
* Copyright (C) 2011, 2012 James McLaughlin. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#ifndef LacewingIncluded
#define LacewingIncluded

#include <stdarg.h>
#include <stdio.h>

#ifndef _MSC_VER

#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif

#include <inttypes.h>
#define lw_i64 int64_t
#define lw_iptr intptr_t
#define lw_i32 int32_t
#define lw_i16 int16_t
#define lw_i8 int8_t

#define lw_PRId64 PRId64
#define lw_PRIu64 PRIu64

#else

#ifdef _WIN64
#define lw_iptr __int64
#else
#define lw_iptr __int32
#endif

#define lw_i64 __int64
#define lw_i32 __int32
#define lw_i16 __int16
#define lw_i8 __int8

#define lw_PRId64 "I64d"
#define lw_PRIu64 "I64u"
#endif

#ifndef _WIN32
#ifndef LacewingHandler
#define LacewingHandler
#endif

#ifndef LacewingFunction
#define LacewingFunction
#endif
#else

    /* For the definition of HANDLE and OVERLAPPED (for Pump) */

    #ifndef _INC_WINDOWS
#include <winsock2.h>
#include <windows.h>
#endif

#define LacewingHandler __cdecl

#endif

#ifndef LacewingFunction
#define LacewingFunction __declspec (dllimport)
#endif

typedef lw_i8 lw_bool;

#define lw_true ((lw_bool) 1)
#define lw_false ((lw_bool) 0)

#ifdef _WIN32
    typedef HANDLE lw_fd;
#else
    typedef int lw_fd;
#endif

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

#define LacewingFlat(c) \
typedef struct { void * reserved; void * tag; } c

LacewingFunction const char* lw_version ();
LacewingFunction lw_i64 lw_file_last_modified (const char * filename);
LacewingFunction lw_bool lw_file_exists (const char * filename);
LacewingFunction size_t lw_file_size (const char * filename);
LacewingFunction lw_bool lw_path_exists (const char * filename);
LacewingFunction void lw_temp_path (char * buffer);
LacewingFunction const char* lw_guess_mime_type (const char * filename);
LacewingFunction void lw_md5 (char * output, const char * input, size_t length);
LacewingFunction void lw_md5_hex (char * output, const char * input, size_t length);
LacewingFunction void lw_sha1 (char * output, const char * input, size_t length);
LacewingFunction void lw_sha1_hex (char * output, const char * input, size_t length);
LacewingFunction void lw_dump (const char * buffer, size_t size);
LacewingFunction lw_bool lw_random (char * buffer, size_t size);

/* Thread */

  LacewingFlat (lw_thread);

  LacewingFunction lw_thread* lw_thread_new (const char * name, void * function);
  LacewingFunction void lw_thread_delete (lw_thread *);
  LacewingFunction void lw_thread_start (lw_thread *, void * parameter);
  LacewingFunction lw_bool lw_thread_started (lw_thread *);
  LacewingFunction long lw_thread_join (lw_thread *);

/* Address */

  LacewingFlat (lw_addr);

  LacewingFunction lw_addr* lw_addr_new (const char * hostname, const char * service);
  LacewingFunction lw_addr* lw_addr_new_port (const char * hostname, long port);
  LacewingFunction lw_addr* lw_addr_new_ex (const char * hostname, const char * service, long hints);
  LacewingFunction lw_addr* lw_addr_new_port_ex (const char * hostname, long port, long hints);
  LacewingFunction lw_addr* lw_addr_copy (lw_addr *);
  LacewingFunction void lw_addr_delete (lw_addr *);
  LacewingFunction long lw_addr_port (lw_addr *);
  LacewingFunction void lw_addr_set_port (lw_addr *, long port);
  LacewingFunction lw_bool lw_addr_is_ready (lw_addr *);
  LacewingFunction lw_bool lw_addr_is_ipv6 (lw_addr *);
  LacewingFunction lw_bool lw_addr_is_equal (lw_addr *, lw_addr *);
  LacewingFunction const char* lw_addr_tostring (lw_addr *);

/* Filter */

  LacewingFlat (lw_filter);

  LacewingFunction lw_filter* lw_filter_new ();
  LacewingFunction void lw_filter_delete (lw_filter *);
  LacewingFunction lw_filter* lw_filter_copy (lw_filter *);
  LacewingFunction void lw_filter_set_remote (lw_filter *, lw_addr *);
  LacewingFunction lw_addr* lw_filter_get_remote (lw_filter *);
  LacewingFunction void lw_filter_set_local (lw_filter *, lw_addr *);
  LacewingFunction lw_addr* lw_filter_get_local (lw_filter *);
  LacewingFunction void lw_filter_set_local_port (lw_filter *, long port);
  LacewingFunction long lw_filter_get_local_port (lw_filter *);
  LacewingFunction void lw_filter_set_remote_port (lw_filter *, long port);
  LacewingFunction long lw_filter_get_remote_port (lw_filter *);
  LacewingFunction void lw_filter_set_reuse (lw_filter *);
  LacewingFunction lw_bool lw_filter_is_reuse_set (lw_filter *);
  LacewingFunction void lw_filter_set_ipv6 (lw_filter *, lw_bool);
  LacewingFunction lw_bool lw_filter_is_ipv6 (lw_filter *);

/* Pump */

  LacewingFlat (lw_pump);
  LacewingFlat (lw_pump_watch);

  LacewingFunction void lw_pump_delete (lw_pump *);
  LacewingFunction void lw_pump_add_user (lw_pump *);
  LacewingFunction void lw_pump_remove_user (lw_pump *);
  LacewingFunction lw_bool lw_pump_in_use (lw_pump *);
  LacewingFunction void lw_pump_remove (lw_pump *, lw_pump_watch *);
  LacewingFunction void lw_pump_post (lw_pump *, void * fn, void * param);
  LacewingFunction lw_bool lw_pump_is_eventpump (lw_pump *);

#ifdef _WIN32

    typedef void (LacewingHandler * lw_pump_callback)
        (void * tag, OVERLAPPED *, unsigned int bytes, int error);

    LacewingFunction lw_pump_watch* lw_pump_add
                          (lw_pump *, HANDLE, void * tag, lw_pump_callback);

    LacewingFunction void lw_pump_update_callbacks
                          (lw_pump *, lw_pump_watch *, void * tag, lw_pump_callback);
#else

    typedef void (LacewingHandler * lw_pump_callback) (void * tag);

    LacewingFunction lw_pump_watch* lw_pump_add
                          (lw_pump *, int FD, void * tag, lw_pump_callback onReadReady,
                              lw_pump_callback onWriteReady, lw_bool edge_triggered);

    LacewingFunction void lw_pump_update_callbacks
                          (lw_pump *, lw_pump_watch *, void * tag, lw_pump_callback onReadReady,
                              lw_pump_callback onWriteReady, lw_bool edge_triggered);
#endif

/* EventPump */

  LacewingFunction lw_pump* lw_pump_new ();
  LacewingFunction void lw_pump_tick (lw_pump *);
  LacewingFunction void lw_pump_start_event_loop (lw_pump *);
  LacewingFunction void lw_pump_start_sleepy_ticking (lw_pump *, void (LacewingHandler * on_tick_needed) (lw_pump *));
  LacewingFunction void lw_pump_post_eventloop_exit (lw_pump *);

/* Stream */

  LacewingFlat (lw_stream);

  LacewingFunction lw_bool lw_stream_valid (lw_stream *);
  LacewingFunction size_t lw_stream_bytes_left (lw_stream *);
  LacewingFunction void lw_stream_read (lw_stream *, size_t bytes);
  LacewingFunction void lw_stream_begin_queue (lw_stream *);
  LacewingFunction size_t lw_stream_queued (lw_stream *);
  LacewingFunction void lw_stream_end_queue (lw_stream *, int head_buffers, const char ** buffers, size_t * lengths);
  LacewingFunction void lw_stream_write (lw_stream *, const char * buffer, size_t length);
  LacewingFunction size_t lw_stream_write_partial (lw_stream *, const char * buffer, size_t length);
  LacewingFunction void lw_stream_write_text (lw_stream *, const char * buffer);
  LacewingFunction void lw_stream_writef (lw_stream *, const char * format, ...);
  LacewingFunction void lw_stream_write_stream (lw_stream *, lw_stream * src, size_t size, lw_bool delete_when_finished);
  LacewingFunction void lw_stream_write_file (lw_stream * stream, const char * filename);
  LacewingFunction void lw_stream_add_filter_upstream (lw_stream * stream, lw_stream * filter, lw_bool close_together);
  LacewingFunction void lw_stream_add_filter_downstream (lw_stream * stream, lw_stream * filter, lw_bool close_together);
  LacewingFunction lw_bool lw_stream_is_transparent (lw_stream * stream);
  LacewingFunction void* lw_stream_cast (lw_stream * stream, void * type);
  LacewingFunction void lw_stream_close (lw_stream * stream);

  typedef void (LacewingHandler * lw_stream_handler_data) (lw_stream *, void * tag, char * buffer, size_t length);

  LacewingFunction void lw_stream_add_handler_data (lw_stream *, lw_stream_handler_data, void * tag);
  LacewingFunction void lw_stream_remove_handler_data (lw_stream *, lw_stream_handler_data, void * tag);

/* FDStream */

  LacewingFunction lw_stream* lw_fdstream_new (lw_pump *);
  LacewingFunction void lw_fdstream_set_fd (lw_stream *, lw_fd fd, lw_pump_watch * watch);
  LacewingFunction void lw_fdstream_cork (lw_stream *);
  LacewingFunction void lw_fdstream_uncork (lw_stream *);
  LacewingFunction void lw_fdstream_nagle (lw_stream *, lw_bool nagle);

/* File */

  LacewingFunction lw_stream * lw_file_new (lw_pump *);

  LacewingFunction lw_stream * lw_file_open_new
      (lw_pump *, const char * filename, const char * mode);

  LacewingFunction lw_bool lw_file_open
      (lw_stream * file, const char * filename, const char * mode);

/* Pipe */
  
  LacewingFunction lw_stream* lw_pipe_new ();
  LacewingFunction lw_stream* lw_pipe_new_pump (lw_pump *);

/* Timer */

  LacewingFlat (lw_timer);
  
  LacewingFunction lw_timer* lw_timer_new ();
  LacewingFunction void lw_timer_delete (lw_timer *);
  LacewingFunction void lw_timer_start (lw_timer *, long milliseconds);
  LacewingFunction lw_bool lw_timer_started (lw_timer *);
  LacewingFunction void lw_timer_stop (lw_timer *);
  LacewingFunction void lw_timer_force_tick (lw_timer *);

  typedef void (LacewingHandler * lw_timer_handler_tick) (lw_timer *);
  LacewingFunction void lw_timer_ontick (lw_timer *, lw_timer_handler_tick);

/* Sync */

  LacewingFlat (lw_sync);
  LacewingFlat (lw_sync_lock);
  
  LacewingFunction lw_sync* lw_sync_new ();
  LacewingFunction void lw_sync_delete (lw_sync *);
  LacewingFunction lw_sync_lock* lw_sync_lock_new (lw_sync *);
  LacewingFunction void lw_sync_lock_delete (lw_sync_lock *);
  LacewingFunction void lw_sync_lock_release (lw_sync_lock *);

/* Event */

  LacewingFlat (lw_event);

  LacewingFunction lw_event* lw_event_new ();
  LacewingFunction void lw_event_delete (lw_event *);
  LacewingFunction void lw_event_signal (lw_event *);
  LacewingFunction void lw_event_unsignal (lw_event *);
  LacewingFunction lw_bool lw_event_signalled (lw_event *);
  LacewingFunction void lw_event_wait (lw_event *, long milliseconds);

/* Error */

  LacewingFlat (lw_error);

  LacewingFunction lw_error* lw_error_new ();
  LacewingFunction void lw_error_delete (lw_error *);
  LacewingFunction void lw_error_add (lw_error *, long);
  LacewingFunction void lw_error_addf (lw_error *, const char * format, ...);
  LacewingFunction const char* lw_error_tostring (lw_error *);
  LacewingFunction lw_error* lw_error_clone (lw_error *);

/* Client */

  LacewingFunction lw_stream* lw_client_new (lw_pump *);
  LacewingFunction void lw_client_connect (lw_stream *, const char * host, long port);
  LacewingFunction void lw_client_connect_addr (lw_stream *, lw_addr *);
  LacewingFunction void lw_client_disconnect (lw_stream *);
  LacewingFunction lw_bool lw_client_connected (lw_stream *);
  LacewingFunction lw_bool lw_client_connecting (lw_stream *);
  LacewingFunction lw_addr* lw_client_server_addr (lw_stream *);
  
  typedef void (LacewingHandler * lw_client_handler_connect) (lw_stream *);
  LacewingFunction void lw_client_onconnect (lw_stream *, lw_client_handler_connect);

  typedef void (LacewingHandler * lw_client_handler_disconnect) (lw_stream *);
  LacewingFunction void lw_client_ondisconnect (lw_stream *, lw_client_handler_disconnect);

  typedef void (LacewingHandler * lw_client_handler_receive) (lw_stream *, const char * buffer, long size);
  LacewingFunction void lw_client_onreceive (lw_stream *, lw_client_handler_receive);

  typedef void (LacewingHandler * lw_client_handler_error) (lw_stream *, lw_error *);
  LacewingFunction void lw_client_onerror (lw_stream *, lw_client_handler_error);

/* Server */

  LacewingFlat (lw_server);

  LacewingFunction lw_server* lw_server_new (lw_pump *);
  LacewingFunction void lw_server_delete (lw_server *);
  LacewingFunction void lw_server_host (lw_server *, long port);
  LacewingFunction void lw_server_host_filter (lw_server *, lw_filter * filter);
  LacewingFunction void lw_server_unhost (lw_server *);
  LacewingFunction lw_bool lw_server_hosting (lw_server *);
  LacewingFunction long lw_server_port (lw_server *);
  LacewingFunction lw_bool lw_server_load_cert_file (lw_server *, const char * filename, const char * passphrase);
  LacewingFunction lw_bool lw_server_load_sys_cert (lw_server *, const char * store_name, const char * common_name, const char * location);
  LacewingFunction lw_bool lw_server_cert_loaded (lw_server *);
  LacewingFunction lw_addr* lw_server_client_address (lw_stream * client);
  LacewingFunction lw_stream* lw_server_client_next (lw_stream * client);

  typedef void (LacewingHandler * lw_server_handler_connect) (lw_server *, lw_stream * client);
  LacewingFunction void lw_server_onconnect (lw_server *, lw_server_handler_connect);

  typedef void (LacewingHandler * lw_server_handler_disconnect) (lw_server *, lw_stream * client);
  LacewingFunction void lw_server_ondisconnect (lw_server *, lw_server_handler_disconnect);

  typedef void (LacewingHandler * lw_server_handler_receive) (lw_server *, lw_stream * client, const char * buffer, size_t size);
  LacewingFunction void lw_server_onreceive (lw_server *, lw_server_handler_receive);
  
  typedef void (LacewingHandler * lw_server_handler_error) (lw_server *, lw_error *);
  LacewingFunction void lw_server_onerror (lw_server *, lw_server_handler_error);

/* UDP */

  LacewingFlat (lw_udp);

  LacewingFunction lw_udp* lw_udp_new (lw_pump *);
  LacewingFunction void lw_udp_delete (lw_udp *);
  LacewingFunction void lw_udp_host (lw_udp *, long port);
  LacewingFunction void lw_udp_host_filter (lw_udp *, lw_filter *);
  LacewingFunction void lw_udp_host_addr (lw_udp *, lw_addr *);
  LacewingFunction lw_bool lw_udp_hosting (lw_udp *);
  LacewingFunction void lw_udp_unhost (lw_udp *);
  LacewingFunction long lw_udp_port (lw_udp *);
  LacewingFunction void lw_udp_write (lw_udp *, lw_addr *, const char * buffer, size_t size);

  typedef void (LacewingHandler * lw_udp_handler_receive)(lw_udp *, lw_addr *, const char * buffer, size_t size);
  LacewingFunction void lw_udp_onreceive (lw_udp *, lw_udp_handler_receive);

  typedef void (LacewingHandler * lw_udp_handler_error) (lw_udp *, lw_error *);
  LacewingFunction void lw_udp_onerror (lw_udp *, lw_udp_handler_error);

/* FlashPolicy */

  LacewingFlat (lw_flashpolicy);

  LacewingFunction lw_flashpolicy* lw_flashpolicy_new (lw_pump *);
  LacewingFunction void lw_flashpolicy_delete (lw_flashpolicy *);
  LacewingFunction void lw_flashpolicy_host (lw_flashpolicy *, const char * filename);
  LacewingFunction void lw_flashpolicy_host_filter (lw_flashpolicy *, const char * filename, lw_filter *);
  LacewingFunction void lw_flashpolicy_unhost (lw_flashpolicy *);
  LacewingFunction lw_bool lw_flashpolicy_hosting (lw_flashpolicy *);

  typedef void (LacewingHandler * lw_flashpolicy_handler_error) (lw_flashpolicy *, lw_error *);
  LacewingFunction void lw_flashpolicy_onerror (lw_flashpolicy *, lw_flashpolicy_handler_error);

/* Webserver */

  LacewingFlat (lw_ws);
  LacewingFlat (lw_ws_req);
  LacewingFlat (lw_ws_req_hdr);
  LacewingFlat (lw_ws_req_param);
  LacewingFlat (lw_ws_req_cookie);
  LacewingFlat (lw_ws_req_ssnitem);
  LacewingFlat (lw_ws_upload);
  LacewingFlat (lw_ws_upload_hdr);

  LacewingFunction lw_ws* lw_ws_new (lw_pump *);
  LacewingFunction void lw_ws_delete (lw_ws *);
  LacewingFunction void lw_ws_host (lw_ws *, long port);
  LacewingFunction void lw_ws_host_secure (lw_ws *, long port);
  LacewingFunction void lw_ws_host_filter (lw_ws *, lw_filter *);
  LacewingFunction void lw_ws_host_secure_filter (lw_ws *, lw_filter *);
  LacewingFunction void lw_ws_unhost (lw_ws *);
  LacewingFunction void lw_ws_unhost_secure (lw_ws *);
  LacewingFunction lw_bool lw_ws_hosting (lw_ws *);
  LacewingFunction lw_bool lw_ws_hosting_secure (lw_ws *);
  LacewingFunction long lw_ws_port (lw_ws *);
  LacewingFunction long lw_ws_port_secure (lw_ws *);
  LacewingFunction lw_bool lw_ws_load_cert_file (lw_ws *, const char * filename, const char * passphrase);
  LacewingFunction lw_bool lw_ws_load_sys_cert (lw_ws *, const char * store_name, const char * common_name, const char * location);
  LacewingFunction lw_bool lw_ws_cert_loaded (lw_ws *);
  LacewingFunction void lw_ws_close_session (lw_ws *, const char * id);
  LacewingFunction void lw_ws_enable_manual_finish (lw_ws *);
  LacewingFunction long lw_ws_idle_timeout (lw_ws *);
  LacewingFunction void lw_ws_set_idle_timeout (lw_ws *, long seconds);
  LacewingFunction lw_addr* lw_ws_req_addr (lw_stream * req);
  LacewingFunction lw_bool lw_ws_req_secure (lw_stream * req);
  LacewingFunction const char* lw_ws_req_url (lw_stream * req);
  LacewingFunction const char* lw_ws_req_hostname (lw_stream * req);
  LacewingFunction void lw_ws_req_disconnect (lw_stream * req);
  LacewingFunction void lw_ws_req_set_redirect (lw_stream * req, const char * url);
  LacewingFunction void lw_ws_req_set_status (lw_stream * req, long code, const char * message);
  LacewingFunction void lw_ws_req_set_mime_type (lw_stream * req, const char * mime_type);
  LacewingFunction void lw_ws_req_set_mime_type_ex (lw_stream * req, const char * mime_type, const char * charset);
  LacewingFunction void lw_ws_req_guess_mime_type (lw_stream * req, const char * filename);
  LacewingFunction void lw_ws_req_finish (lw_stream * req);
  LacewingFunction lw_i64 lw_ws_req_last_modified (lw_stream * req);
  LacewingFunction void lw_ws_req_set_last_modified (lw_stream * req, lw_i64);
  LacewingFunction void lw_ws_req_set_unmodified (lw_stream * req);
  LacewingFunction void lw_ws_req_set_header (lw_stream * req, const char * name, const char * value);
  LacewingFunction const char* lw_ws_req_header (lw_stream * req, const char * name);
  LacewingFunction lw_ws_req_hdr* lw_ws_req_first_header (lw_stream * req);
  LacewingFunction const char* lw_ws_req_hdr_name (lw_ws_req_hdr *);
  LacewingFunction const char* lw_ws_req_hdr_value (lw_ws_req_hdr *);
  LacewingFunction lw_ws_req_hdr* lw_ws_req_hdr_next (lw_ws_req_hdr *);
  LacewingFunction lw_ws_req_param* lw_ws_req_first_GET (lw_stream * req);
  LacewingFunction lw_ws_req_param* lw_ws_req_first_POST (lw_stream * req);
  LacewingFunction const char* lw_ws_req_param_name (lw_ws_req_param *);
  LacewingFunction const char* lw_ws_req_param_value (lw_ws_req_param *);
  LacewingFunction lw_ws_req_param* lw_ws_req_param_next (lw_ws_req_param *);
  LacewingFunction lw_ws_req_cookie* lw_ws_req_first_cookie (lw_stream * req);
  LacewingFunction const char* lw_ws_req_cookie_name (lw_ws_req_cookie *);
  LacewingFunction const char* lw_ws_req_cookie_value (lw_ws_req_cookie *);
  LacewingFunction lw_ws_req_cookie* lw_ws_req_cookie_next (lw_ws_req_cookie *);
  LacewingFunction void lw_ws_req_set_cookie (lw_stream * req, const char * name, const char * value);
  LacewingFunction void lw_ws_req_set_cookie_ex (lw_stream * req, const char * name, const char * value, const char * attributes);
  LacewingFunction const char* lw_ws_req_get_cookie (lw_stream * req, const char * name);
  LacewingFunction const char* lw_ws_req_session_id (lw_stream * req);
  LacewingFunction void lw_ws_req_session_write (lw_stream * req, const char * name, const char * value);
  LacewingFunction const char* lw_ws_req_session_read (lw_stream * req, const char * name);
  LacewingFunction void lw_ws_req_session_close (lw_stream * req);
  LacewingFunction lw_ws_req_ssnitem* lw_ws_req_first_session_item (lw_stream * req);
  LacewingFunction const char* lw_ws_req_ssnitem_name (lw_ws_req_ssnitem *);
  LacewingFunction const char* lw_ws_req_ssnitem_value (lw_ws_req_ssnitem *);
  LacewingFunction lw_ws_req_ssnitem* lw_ws_req_ssnitem_next (lw_ws_req_ssnitem *);
  LacewingFunction const char* lw_ws_req_GET (lw_stream * req, const char * name);
  LacewingFunction const char* lw_ws_req_POST (lw_stream * req, const char * name);
  LacewingFunction const char* lw_ws_req_body (lw_stream * req);
  LacewingFunction void lw_ws_req_disable_cache (lw_stream * req);
  LacewingFunction long lw_ws_req_idle_timeout (lw_stream * req);
  LacewingFunction void lw_ws_req_set_idle_timeout (lw_stream * req, long seconds);
/*LacewingFunction void lw_ws_req_enable_dl_resuming (lw_stream * req);
LacewingFunction lw_i64 lw_ws_req_reqrange_begin (lw_stream * req);
LacewingFunction lw_i64 lw_ws_req_reqrange_end (lw_stream * req);
LacewingFunction void lw_ws_req_set_outgoing_range (lw_stream * req, lw_i64 begin, lw_i64 end);*/
  LacewingFunction const char* lw_ws_upload_form_el_name (lw_ws_upload *);
  LacewingFunction const char* lw_ws_upload_filename (lw_ws_upload *);
  LacewingFunction const char* lw_ws_upload_header (lw_ws_upload *, const char * name);
  LacewingFunction void lw_ws_upload_set_autosave (lw_ws_upload *);
  LacewingFunction const char* lw_ws_upload_autosave_fname (lw_ws_upload *);
  LacewingFunction lw_ws_upload_hdr* lw_ws_upload_first_header (lw_ws_upload *);
  LacewingFunction const char* lw_ws_upload_hdr_name (lw_ws_upload_hdr *);
  LacewingFunction const char* lw_ws_upload_hdr_value (lw_ws_upload_hdr *);
  LacewingFunction lw_ws_upload_hdr* lw_ws_upload_hdr_next (lw_ws_upload_hdr *);

  typedef void (LacewingHandler * lw_ws_handler_get) (lw_ws *, lw_stream * req);
  LacewingFunction void lw_ws_onget (lw_ws *, lw_ws_handler_get);

  typedef void (LacewingHandler * lw_ws_handler_post) (lw_ws *, lw_stream * req);
  LacewingFunction void lw_ws_onpost (lw_ws *, lw_ws_handler_post);

  typedef void (LacewingHandler * lw_ws_handler_head) (lw_ws *, lw_stream * req);
  LacewingFunction void lw_ws_onhead (lw_ws *, lw_ws_handler_head);

  typedef void (LacewingHandler * lw_ws_handler_error) (lw_ws *, lw_error *);
  LacewingFunction void lw_ws_onerror (lw_ws *, lw_ws_handler_error);

  typedef void (LacewingHandler * lw_ws_handler_disconnect) (lw_ws *, lw_stream * req);
  LacewingFunction void lw_ws_ondisconnect (lw_ws *, lw_ws_handler_disconnect);

  typedef void (LacewingHandler * lw_ws_handler_upload_start) (lw_ws *, lw_ws_req *, lw_ws_upload *);
  LacewingFunction void lw_ws_onuploadstart (lw_ws *, lw_ws_handler_upload_start);

  typedef void (LacewingHandler * lw_ws_handler_upload_chunk) (lw_ws *, lw_ws_req *, lw_ws_upload *, const char * buffer, size_t size);
  LacewingFunction void lw_ws_onuploadchunk (lw_ws *, lw_ws_handler_upload_chunk);

  typedef void (LacewingHandler * lw_ws_handler_upload_done) (lw_ws *, lw_ws_req *, lw_ws_upload *);
  LacewingFunction void lw_ws_onuploaddone (lw_ws *, lw_ws_handler_upload_done);

  typedef void (LacewingHandler * lw_ws_handler_upload_post) (lw_ws *, lw_ws_req *, lw_ws_upload * uploads [], long upload_count);
  LacewingFunction void lw_ws_onuploadpost (lw_ws *, lw_ws_handler_upload_post);


#ifdef __cplusplus
}

#define LacewingClass \
struct Internal; Internal * internal;

#define LacewingClassTag \
struct Internal; Internal * internal; void * Tag;

namespace Lacewing
{

struct Error
{
    LacewingClassTag;

    LacewingFunction Error ();
    LacewingFunction ~Error ();
    
    LacewingFunction void Add (const char * Format, ...);
    LacewingFunction void Add (int);
    LacewingFunction void Add (const char * Format, va_list);

    LacewingFunction int Size ();

    LacewingFunction const char * ToString ();
    LacewingFunction operator const char * ();

    LacewingFunction Lacewing::Error * Clone ();
};

struct Event
{
    LacewingClassTag;

    LacewingFunction Event ();
    LacewingFunction ~Event ();

    LacewingFunction void Signal ();
    LacewingFunction void Unsignal ();
    
    LacewingFunction bool Signalled ();

    LacewingFunction bool Wait (int Timeout = -1);
};

struct Pump
{
private:

    LacewingClassTag;

public:

    LacewingFunction Pump ();
    LacewingFunction virtual ~Pump ();

    LacewingFunction void AddUser ();
    LacewingFunction void RemoveUser ();

    LacewingFunction bool InUse ();

    struct Watch;

#ifdef _WIN32

        typedef void (* Callback)
            (void * Tag, OVERLAPPED *, unsigned int BytesTransferred, int Error);

        virtual Watch * Add (HANDLE, void * tag, Callback) = 0;

        virtual void UpdateCallbacks (Watch *, void * tag, Callback) = 0;

#else

        typedef void (* Callback) (void * Tag);

        virtual Watch * Add
            (int FD, void * Tag, Callback onReadReady,
                Callback onWriteReady = 0, bool edge_triggered = true) = 0;

        virtual void UpdateCallbacks
            (Watch *, void * tag, Callback onReadReady,
                Callback onWriteReady = 0, bool edge_triggered = true) = 0;

#endif
 
    virtual void Remove (Watch *) = 0;

    virtual void Post (void * Function, void * Parameter = 0) = 0;

    LacewingFunction virtual bool IsEventPump ();
};

struct EventPump : public Pump
{
    LacewingClassTag;

    LacewingFunction EventPump (int MaxHint = 1024);
    LacewingFunction ~EventPump ();

    LacewingFunction Lacewing::Error * Tick ();
    LacewingFunction Lacewing::Error * StartEventLoop ();

    LacewingFunction Lacewing::Error * StartSleepyTicking
        (void (LacewingHandler * onTickNeeded) (Lacewing::EventPump &EventPump));

    LacewingFunction void PostEventLoopExit ();

#ifdef _WIN32

        LacewingFunction Watch * Add (HANDLE, void * tag, Callback);

        LacewingFunction void UpdateCallbacks (Watch *, void * tag, Callback);

#else

        LacewingFunction Watch * Add
            (int FD, void * Tag, Callback onReadReady,
                Callback onWriteReady = 0, bool edge_triggered = true);

        LacewingFunction void UpdateCallbacks
            (Watch *, void * tag, Callback onReadReady,
                Callback onWriteReady = 0, bool edge_triggered = true);

#endif

    LacewingFunction void Remove (Watch *);

    LacewingFunction void Post (void * Function, void * Parameter = 0);

    LacewingFunction bool IsEventPump ();
};

struct Thread
{
    LacewingClassTag;

    LacewingFunction Thread (const char * Name, void * Function);
    LacewingFunction ~ Thread ();

    LacewingFunction void Start (void * Parameter = 0);
    LacewingFunction bool Started ();

    LacewingFunction int Join ();
};

struct Timer
{
    LacewingClassTag;

    LacewingFunction Timer (Pump &);
    LacewingFunction ~Timer ();

    LacewingFunction void Start (int Milliseconds);
    LacewingFunction void Stop ();
    LacewingFunction bool Started ();
    
    LacewingFunction void ForceTick ();
    
    typedef void (LacewingHandler * HandlerTick) (Lacewing::Timer &Timer);
    LacewingFunction void onTick (HandlerTick);
};

struct Sync
{
    LacewingClassTag;

    LacewingFunction Sync ();
    LacewingFunction ~Sync ();

    struct Lock
    {
        void * internal;

        LacewingFunction Lock (Lacewing::Sync &);
        LacewingFunction ~Lock ();
        
        LacewingFunction void Release ();
    };
};

struct Stream
{
    LacewingClass;

    LacewingFunction Stream ();
    LacewingFunction Stream (Pump &);

    LacewingFunction virtual ~ Stream ();

    typedef void (LacewingHandler * HandlerData)
        (Stream &, void * tag, const char * buffer, size_t size);

    LacewingFunction void AddHandlerData (HandlerData, void * tag = 0);
    LacewingFunction void RemoveHandlerData (HandlerData, void * tag = 0);

    typedef void (LacewingHandler * HandlerClose) (Stream &, void * Tag);

    LacewingFunction void AddHandlerClose (HandlerClose, void * tag);
    LacewingFunction void RemoveHandlerClose (HandlerClose, void * tag);

    LacewingFunction virtual size_t BytesLeft (); /* if -1, Read() does nothing */
    LacewingFunction virtual void Read (size_t bytes = -1); /* -1 = until EOF */

    LacewingFunction void BeginQueue ();
    LacewingFunction size_t Queued ();

    /* When EndQueue is called, one or more head buffers may optionally be
* written _before_ the queued data. This is used for e.g. including HTTP
* headers before the (already buffered) response body.
*/

    LacewingFunction void EndQueue ();

    LacewingFunction void EndQueue
        (int head_buffers, const char ** buffers, size_t * lengths);

    LacewingFunction void Write
        (const char * buffer, size_t size = -1);

    LacewingFunction size_t WritePartial
        (const char * buffer, size_t size = -1);

    inline Stream &operator << (char * s)
    { Write (s);
        return *this;
    }

    inline Stream &operator << (const char * s)
    { Write (s);
        return *this;
    }

    inline Stream &operator << (lw_i64 v)
    { char buffer [128];
        sprintf (buffer, "%" lw_PRId64, v);
        Write (buffer);
        return *this;
    }

    LacewingFunction void Write
        (Stream &, size_t size = -1, bool delete_when_finished = false);

    LacewingFunction void WriteFile (const char * filename);

    LacewingFunction void AddFilterUpstream
      (Stream &, bool delete_with_stream = false, bool close_together = false);

    LacewingFunction void AddFilterDownstream
      (Stream &, bool delete_with_stream = false, bool close_together = false);

    LacewingFunction virtual bool IsTransparent ();

    LacewingFunction void Close ();

    /* Since we don't compile with RTTI (and this is the only place it would be needed) */

    LacewingFunction virtual void * Cast (void * type);

protected:
 
    LacewingFunction void Data (const char * buffer, size_t size);

    const static int Retry_Now = 1, Retry_Never = 2, Retry_MoreData = 3;
    LacewingFunction virtual void Retry (int when = Retry_Now);

    LacewingFunction virtual size_t Put (const char * buffer, size_t size) = 0;

    LacewingFunction virtual size_t Put (Stream &, size_t size);
};
              
struct Pipe : public Stream
{
    LacewingFunction Pipe ();
    LacewingFunction Pipe (Lacewing::Pump &);

    LacewingFunction ~ Pipe ();

    LacewingFunction size_t Put (const char * buffer, size_t size);

    LacewingFunction bool IsTransparent ();
};

/* TODO : Seek method? */

struct FDStream : public Stream
{
private:

    LacewingClass;

public:

    using Stream::Write;

    LacewingFunction FDStream (Lacewing::Pump &);
    LacewingFunction virtual ~ FDStream ();

    LacewingFunction void SetFD
        (lw_fd, Pump::Watch * watch = 0, bool auto_close = false);

    LacewingFunction bool Valid ();

    LacewingFunction virtual size_t BytesLeft ();
    LacewingFunction virtual void Read (size_t Bytes = -1);

    LacewingFunction void Cork ();
    LacewingFunction void Uncork ();

    LacewingFunction void Nagle (bool);

    LacewingFunction void Close ();

    LacewingFunction virtual void * Cast (void *);

protected:

    LacewingFunction size_t Put (const char * buffer, size_t size);
    LacewingFunction size_t Put (Stream &, size_t size);
};

struct File : public FDStream
{
    LacewingClassTag;

    LacewingFunction File
        (Lacewing::Pump &);

    LacewingFunction File
        (Lacewing::Pump &, const char * filename, const char * mode = "rb");
    
    LacewingFunction virtual ~ File ();

    LacewingFunction bool Open (const char * filename, const char * mode = "rb");
    LacewingFunction bool OpenTemp ();

    LacewingFunction const char * Name ();
};

struct Address
{
    LacewingClassTag;

    const static int HINT_TCP = 1;
    const static int HINT_UDP = 2;
    const static int HINT_IPv4 = 4;

    LacewingFunction Address (Address &);
    LacewingFunction Address (const char * Hostname, int Port = 0, int Hints = 0);
    LacewingFunction Address (const char * Hostname, const char * Service, int Hints = 0);

    LacewingFunction ~ Address ();

    LacewingFunction int Port ();
    LacewingFunction void Port (int);
    
    LacewingFunction bool IPv6 ();

    LacewingFunction bool Ready ();
    LacewingFunction Lacewing::Error * Resolve ();

    LacewingFunction const char * ToString ();
    LacewingFunction operator const char * ();

    LacewingFunction bool operator == (Address &);
    LacewingFunction bool operator != (Address &);
};

struct Filter
{
    LacewingClassTag;

    LacewingFunction Filter ();
    LacewingFunction Filter (Filter &);

    LacewingFunction ~Filter ();

    LacewingFunction void Local (Lacewing::Address *);
    LacewingFunction void Remote (Lacewing::Address *);

    LacewingFunction Lacewing::Address * Local ();
    LacewingFunction Lacewing::Address * Remote ();

    LacewingFunction void LocalPort (int Port);
    LacewingFunction int LocalPort ();

    LacewingFunction void RemotePort (int Port);
    LacewingFunction int RemotePort ();

    LacewingFunction void Reuse (bool Enabled);
    LacewingFunction bool Reuse ();

    LacewingFunction void IPv6 (bool Enabled);
    LacewingFunction bool IPv6 ();
};

struct Client : public FDStream
{
    LacewingClassTag;

    LacewingFunction Client (Pump &);
    LacewingFunction ~Client ();

    LacewingFunction void Connect (const char * Host, int Port);
    LacewingFunction void Connect (Address &);

    LacewingFunction bool Connected ();
    LacewingFunction bool Connecting ();
    
    LacewingFunction Lacewing::Address &ServerAddress ();

    typedef void (LacewingHandler * HandlerConnect)
        (Lacewing::Client &client);

    typedef void (LacewingHandler * HandlerDisconnect)
        (Lacewing::Client &client);

    typedef void (LacewingHandler * HandlerReceive)
        (Lacewing::Client &client, const char * buffer, size_t size);

    typedef void (LacewingHandler * HandlerError)
        (Lacewing::Client &client, Lacewing::Error &error);

    LacewingFunction void onConnect (HandlerConnect);
    LacewingFunction void onDisconnect (HandlerDisconnect);
    LacewingFunction void onReceive (HandlerReceive);
    LacewingFunction void onError (HandlerError);
};

struct Server
{
    LacewingClassTag;

    LacewingFunction Server (Pump &);
    LacewingFunction ~Server ();

    LacewingFunction void Host (int Port);
    LacewingFunction void Host (Lacewing::Filter &Filter);

    LacewingFunction void Unhost ();
    LacewingFunction bool Hosting ();
    LacewingFunction int Port ();

    LacewingFunction bool LoadCertificateFile
        (const char * filename, const char * passphrase = "");

    LacewingFunction bool LoadSystemCertificate
        (const char * storeName, const char * common_name,
         const char * location = "CurrentUser");

    LacewingFunction bool CertificateLoaded ();

    LacewingFunction bool CanNPN ();
    LacewingFunction void AddNPN (const char *);

    struct Client : public FDStream
    {
        Client (Lacewing::Pump &, lw_fd);

        LacewingClassTag;

        LacewingFunction Lacewing::Address &GetAddress ();

        LacewingFunction Client * Next ();

        LacewingFunction const char * NPN ();
    };

    LacewingFunction int ClientCount ();
    LacewingFunction Client * FirstClient ();

    typedef void (LacewingHandler * HandlerConnect)
        (Lacewing::Server &server, Lacewing::Server::Client &client);

    typedef void (LacewingHandler * HandlerDisconnect)
        (Lacewing::Server &server, Lacewing::Server::Client &client);

    typedef void (LacewingHandler * HandlerReceive)
        (Lacewing::Server &server, Lacewing::Server::Client &client,
             const char * data, size_t size);

    typedef void (LacewingHandler * HandlerError)
        (Lacewing::Server &server, Lacewing::Error &error);
    
    LacewingFunction void onConnect (HandlerConnect);
    LacewingFunction void onDisconnect (HandlerDisconnect);
    LacewingFunction void onReceive (HandlerReceive);
    LacewingFunction void onError (HandlerError);
};

struct UDP
{
    LacewingClassTag;

    LacewingFunction UDP (Pump &);
    LacewingFunction ~UDP ();

    LacewingFunction void Host (int Port);
    LacewingFunction void Host (Lacewing::Filter &Filter);
    LacewingFunction void Host (Address &); /* Use Port () afterwards to get the port number */

    LacewingFunction bool Hosting ();
    LacewingFunction void Unhost ();

    LacewingFunction int Port ();

    LacewingFunction void Write (Lacewing::Address &Address, const char * Data, size_t Size = -1);

    typedef void (LacewingHandler * HandlerReceive)
        (Lacewing::UDP &UDP, Lacewing::Address &from, char * data, size_t size);

    typedef void (LacewingHandler * HandlerError)
        (Lacewing::UDP &UDP, Lacewing::Error &error);
    
    LacewingFunction void onReceive (HandlerReceive);
    LacewingFunction void onError (HandlerError);
};

struct Webserver
{
    LacewingClassTag;

    LacewingFunction Webserver (Pump &);
    LacewingFunction ~Webserver ();

    LacewingFunction void Host (int Port = 80);
    LacewingFunction void HostSecure (int Port = 443);
    
    LacewingFunction void Host (Lacewing::Filter &Filter);
    LacewingFunction void HostSecure (Lacewing::Filter &Filter);
    
    LacewingFunction void Unhost ();
    LacewingFunction void UnhostSecure ();

    LacewingFunction bool Hosting ();
    LacewingFunction bool HostingSecure ();

    LacewingFunction int Port ();
    LacewingFunction int SecurePort ();

    LacewingFunction bool LoadCertificateFile
        (const char * filename, const char * passphrase = "");

    LacewingFunction bool LoadSystemCertificate
        (const char * storeName, const char * common_name,
         const char * location = "CurrentUser");

    LacewingFunction bool CertificateLoaded ();

    LacewingFunction void EnableManualRequestFinish ();

    LacewingFunction int IdleTimeout ();
    LacewingFunction void IdleTimeout (int Seconds);

    struct Request : public Stream
    {
        LacewingClassTag;

        LacewingFunction Request (Pump &);
        LacewingFunction ~ Request ();

        LacewingFunction Address &GetAddress ();

        LacewingFunction bool Secure ();

        LacewingFunction const char * URL ();
        LacewingFunction const char * Hostname ();
        
        LacewingFunction void Disconnect ();

        LacewingFunction void SetRedirect (const char * URL);
        LacewingFunction void Status (int Code, const char * Message);

        LacewingFunction void SetMimeType (const char * MimeType, const char * Charset = "UTF-8");
        LacewingFunction void GuessMimeType (const char * Filename);

        LacewingFunction void Finish ();

        LacewingFunction int IdleTimeout ();
        LacewingFunction void IdleTimeout (int Seconds);

        LacewingFunction lw_i64 LastModified ();
        LacewingFunction void LastModified (lw_i64 Time);
        LacewingFunction void SetUnmodified ();
    
        LacewingFunction void DisableCache ();

        LacewingFunction void EnableDownloadResuming ();
        LacewingFunction lw_i64 RequestedRangeBegin ();
        LacewingFunction lw_i64 RequestedRangeEnd ();
        LacewingFunction void SetOutgoingRange (lw_i64 Begin, lw_i64 End);
        

        /** Headers **/

        struct Header
        {
            LacewingFunction const char * Name ();
            LacewingFunction const char * Value ();

            LacewingFunction Header * Next ();
        };

        LacewingFunction struct Header * FirstHeader ();

        LacewingFunction const char * Header (const char * Name);
        LacewingFunction void Header (const char * Name, const char * Value);

        /* Does not overwrite an existing header with the same name */

        LacewingFunction void AddHeader
            (const char * Name, const char * Value);

    
        /** Cookies **/

        struct Cookie
        {
            LacewingFunction const char * Name ();
            LacewingFunction const char * Value ();

            LacewingFunction Cookie * Next ();
        };

        LacewingFunction struct Cookie * FirstCookie ();

        LacewingFunction const char * Cookie (const char * Name);
        LacewingFunction void Cookie (const char * Name, const char * Value);
        LacewingFunction void Cookie (const char * Name, const char * Value, const char * Attributes);

    
        /** Sessions **/

        struct SessionItem
        {
            LacewingFunction const char * Name ();
            LacewingFunction const char * Value ();

            LacewingFunction SessionItem * Next ();
        };

        LacewingFunction SessionItem * FirstSessionItem ();

        LacewingFunction const char * Session ();
        LacewingFunction const char * Session (const char * Key);
        LacewingFunction void Session (const char * Key, const char * Value);

        LacewingFunction void CloseSession ();

            
        /** GET/POST data **/

        struct Parameter
        {
            LacewingFunction const char * Name ();
            LacewingFunction const char * Value ();
            LacewingFunction const char * ContentType ();

            LacewingFunction Parameter * Next ();
        };

        LacewingFunction Parameter * GET ();
        LacewingFunction Parameter * POST ();

        LacewingFunction const char * GET (const char * Name);
        LacewingFunction const char * POST (const char * Name);

        LacewingFunction const char * Body ();
    };

    LacewingFunction void CloseSession (const char * ID);

    struct Upload
    {
        LacewingClassTag;

        LacewingFunction const char * FormElementName ();
        LacewingFunction const char * Filename ();
        LacewingFunction void SetAutoSave ();
        LacewingFunction const char * GetAutoSaveFilename ();

        LacewingFunction const char * Header (const char * Name);
        
        struct Header
        {
            LacewingFunction const char * Name ();
            LacewingFunction const char * Value ();

            LacewingFunction Header * Next ();
        };

        LacewingFunction struct Header * FirstHeader ();
    };

    typedef void (LacewingHandler * HandlerGet) (Lacewing::Webserver &Webserver, Lacewing::Webserver::Request &Request);
    typedef void (LacewingHandler * HandlerPost) (Lacewing::Webserver &Webserver, Lacewing::Webserver::Request &Request);
    typedef void (LacewingHandler * HandlerHead) (Lacewing::Webserver &Webserver, Lacewing::Webserver::Request &Request);
    typedef void (LacewingHandler * HandlerDisconnect) (Lacewing::Webserver &Webserver, Lacewing::Webserver::Request &Request);
    typedef void (LacewingHandler * HandlerError) (Lacewing::Webserver &Webserver, Lacewing::Error &);

    typedef void (LacewingHandler * HandlerUploadStart) (Lacewing::Webserver &Webserver, Lacewing::Webserver::Request &Request,
                                                                    Lacewing::Webserver::Upload &Upload);

    typedef void (LacewingHandler * HandlerUploadChunk) (Lacewing::Webserver &Webserver, Lacewing::Webserver::Request &Request,
                                                                    Lacewing::Webserver::Upload &Upload, const char * Data, size_t Size);
     
    typedef void (LacewingHandler * HandlerUploadDone) (Lacewing::Webserver &Webserver, Lacewing::Webserver::Request &Request,
                                                                    Lacewing::Webserver::Upload &Upload);

    typedef void (LacewingHandler * HandlerUploadPost) (Lacewing::Webserver &Webserver, Lacewing::Webserver::Request &Request,
                                                                    Lacewing::Webserver::Upload * Uploads [], int UploadCount);

    LacewingFunction void onGet (HandlerGet);
    LacewingFunction void onUploadStart (HandlerUploadStart);
    LacewingFunction void onUploadChunk (HandlerUploadChunk);
    LacewingFunction void onUploadDone (HandlerUploadDone);
    LacewingFunction void onUploadPost (HandlerUploadPost);
    LacewingFunction void onPost (HandlerPost);
    LacewingFunction void onHead (HandlerHead);
    LacewingFunction void onDisconnect (HandlerDisconnect);
    LacewingFunction void onError (HandlerError);
};
    
struct FlashPolicy
{
    LacewingClassTag;

    LacewingFunction FlashPolicy (Pump &);
    LacewingFunction ~FlashPolicy ();

    LacewingFunction void Host (const char * Filename);
    LacewingFunction void Host (const char * Filename, Lacewing::Filter &Filter);

    LacewingFunction void Unhost ();

    LacewingFunction bool Hosting ();

    typedef void (LacewingHandler * HandlerError)
        (Lacewing::FlashPolicy &FlashPolicy, Lacewing::Error &);

    LacewingFunction void onError (HandlerError);
};

}

#endif /* __cplusplus */
#endif /* LacewingIncluded */
Something went wrong with that request. Please try again.