SfxStd.h 46.8 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 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303
#ifndef __SFX__SfxStd__MODULE__
#define __SFX__SfxStd__MODULE__
#ifdef __cplusplus
extern "C"
{
#endif
/*
基本类型/C函数定义     sfx
宏定义                 SFX
结构体/枚举定义        Sfx

枚举/宏定义/函数指针 全部大写,用下划线分隔
*/

#define SFX_LINUX
#define SFX_DEBUG

#define SFX_HOME_PAGE      "www.sfxlab.cn/www.sfxlab.cn"

#define SFX_MERGE_2(x,y)           x##y
#define SFX_MERGE_1(x,y)           SFX_MERGE_2(x,y)

//#define SFX_DEBUG_PRINT_INFO
//#define SFX_MEMORY_DEBUG_MONITOR

#ifdef SFX_WIN
#undef SFX_LINUX
#include <WinSock2.h>
#pragma comment( lib, "ws2_32.lib" )
#include <Windows.h>
#include <string.h>
#include <stdio.h>
#endif


#ifdef SFX_LINUX
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define sprintf_s snprintf
#endif

typedef char                   sfx_8;
typedef short                  sfx_16;
typedef int                    sfx_32;
typedef void                   sfx_void;
typedef float                  sfx_f32;
typedef double                 sfx_f64;
typedef unsigned char          sfx_bool;
typedef unsigned char          sfx_u8;
typedef unsigned short         sfx_u16;
typedef unsigned int           sfx_u32;
typedef unsigned long long     sfx_u64;
typedef unsigned long          sfx_ulong;
#ifdef SFX_WIN
typedef HWND                   sfx_uih;
#endif


#define SFX_RLT_ERROR           (-1)
#define SFX_RLT_OK              0
#define SFX_RLT_NEED_DATA       1
#define SFX_RLT_TRYAGAIN        2
#define SFX_RLT_COMPLETED       3
#define SFX_RLT_INSTABLE        4
#define SFX_RLT_NOT_FOUND       5
#define SFX_RLT_NOT_SUPPORT     6
#define SFX_RLT_DONT_CARE       7
#define SFX_RLT_PARAMS_ERROR    8
#define SFX_RLT_NEED_MORE_RES   9
#define SFX_RLT_ALREADY_EXIST   10
#define SFX_RLT_OFFLINE         11
#define SFX_RLT_ACTIVE_EXIT     82
#define SFX_RLT_HTTP_MIN_DEF    100
#define SFX_RLT_HTTP_MAX_DEF    599

#define sfx_isHttpState( x ) ( x >= SFX_RLT_HTTP_MIN_DEF && x <= SFX_RLT_HTTP_MAX_DEF )


#define SFX_DEFAULT_PAGE_SIZE            0x00001000
#define SFX_DEFAULT_PATH_SIZE            0x00000800   // 2048
#define SFX_DEFAULT_URL_SIZE             0x00000200   // 512
#define SFX_DEFAULT_MIN_PATH             0x00000100   // 256
#define SFX_DEFAULT_MIN_SIZE             0x00000080   // 128
#define SFX_DEFAULT_MINI_SIZE            0x00000020

#define SFX_DELAY_LIMIT_TIME             0x0000000A   // 10
#define SFX_IP_SIZE                      0x00000020

#define sfx_null                0
#define sfx_false               0
#define sfx_true                1
#define sfx_max                 ( sfx_u32 )(-1)
#define sfx_invalid             (-1)

#include <assert.h>
#define sfx_assert( x )         assert( x )

#ifdef SFX_WIN
#define sfx_inline              __inline
#define sfx_stdcall             __stdcall
typedef SOCKET                 sfx_socket;
typedef struct sockaddr        sfx_sockaddr;
typedef struct sockaddr_in     sfx_sockaddr_in;
typedef HANDLE                 sfx_thread;
typedef HANDLE                 sfx_event;
typedef CRITICAL_SECTION       sfx_mutex;
typedef long volatile          sfx_atom;
#endif


#ifdef SFX_LINUX
//#define sfx_inline             static inline
#define sfx_inline             inline
#define sfx_stdcall            __attribute__((stdcall))
#define sfx_fun_export         extern "C" __attribute__ ((visibility("default")))
typedef int                    sfx_socket;
typedef struct sockaddr        sfx_sockaddr;
typedef struct sockaddr_in     sfx_sockaddr_in;
typedef pthread_t              sfx_thread;
typedef pthread_mutex_t        sfx_mutex;
typedef pthread_cond_t        sfx_event;
typedef void *                   sfx_uih;
//typedef void * ( * sfx_thread_proc )( sfx_void * param );
#endif

#define sfx_invalid_id         ( -1 )
#define sfx_invalid_thread     ( ( sfx_thread )0 )
#define sfx_invalid_socket     ( ( sfx_socket )-1 )
#define sfx_invalid_file        sfx_null   

typedef sfx_void sfx_port_mgr;
typedef sfx_void * ( sfx_stdcall * sfx_thread_proc )( sfx_void * param );

#define SFX_ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
#define SFX_MIN( a, b ) ( a > b ? b : a )
#define SFX_MAX( a, b ) ( a > b ? a : b )

typedef struct SfxUserInfo
{
	sfx_u8 encrypt;
	sfx_8 pwd[SFX_DEFAULT_MINI_SIZE];
	sfx_8 user[SFX_DEFAULT_MINI_SIZE];
}SfxUserInfo;

typedef enum SfxModuleControl
{
	SFX_MODULE_CONTROL_UNKNOW = 0,
	SFX_MODULE_CONTROL_ERROR_CODE,
	SFX_MODULE_CONTROL_SET_AUTH,
	SFX_MODULE_CONTROL_GET_AUTH,
	SFX_MODULE_CONTROL_GET_STATISTICS
}SfxModuleControl;
	

typedef enum SfxHttpMethod
{
	SFX_HTTP_MTD_UNKNOW = 0,
	SFX_HTTP_MTD_OPTIONS,
	SFX_HTTP_MTD_DELETE,
	SFX_HTTP_MTD_HEAD,
	SFX_HTTP_MTD_POST,
	SFX_HTTP_MTD_PUT,
	SFX_HTTP_MTD_GET,
	SFX_HTTP_MTD_MAX
}SfxHttpMethod;


typedef enum SfxHttpContentType
{
	SFX_HTTP_CNT_TYPE_UNKNOW = 0,
	SFX_HTTP_CNT_TYPE_TEXT,              // "text/html"
	SFX_HTTP_CNT_TYPE_JSON,              // "application/json"
	SFX_HTTP_CNT_TYPE_JPG,				 // "image/jpeg"
	SFX_HTTP_CNT_TYPE_OS,                // "application/octet-stream"
	SFX_HTTP_CNT_TYPE_MAX
}SfxHttpContentType;

SfxHttpMethod sfxFindHttpMethod( const sfx_8 * method );
SfxHttpContentType sfxFindHttpContentType( const sfx_8 * contentType );

#define sfxReverseU16( _u16 ) \
	( ( ( _u16 >> 8 ) & 0xFF ) | ( ( _u16 & 0xFF ) << 8 ) )

#define sfxReverseU24( _u24 ) \
	( ( ( _u24 & 0xFF ) << 16 ) | \
	  ( ( ( _u24 >> 8 ) & 0xFF ) << 8 ) | \
      ( ( _u24 >> 16 ) & 0xFF ) )

#define sfxReverseU32( _u32 ) \
	( ( ( _u32 & 0xFF ) << 24 ) | \
	  ( ( ( _u32 >> 8 ) & 0xFF ) << 16 ) | \
      ( ( ( _u32 >> 16 ) & 0xFF ) << 8 ) | \
	  ( ( _u32 >> 24 ) & 0xFF ) )

#define SFX_REVERSE_2( src, dst ) \
	( ( sfx_u8 * )dst )[0] = ( ( sfx_u8 * )src )[1]; \
	( ( sfx_u8 * )dst )[1] = ( ( sfx_u8 * )src )[0]; 

#define SFX_REVERSE_3( src, dst ) \
	( ( sfx_u8 * )dst )[0] = ( ( sfx_u8 * )src )[2]; \
	( ( sfx_u8 * )dst )[1] = ( ( sfx_u8 * )src )[1]; \
	( ( sfx_u8 * )dst )[2] = ( ( sfx_u8 * )src )[0]; 

#define SFX_REVERSE_4( src, dst ) \
	( ( sfx_u8 * )dst )[0] = ( ( sfx_u8 * )src )[3]; \
	( ( sfx_u8 * )dst )[1] = ( ( sfx_u8 * )src )[2]; \
	( ( sfx_u8 * )dst )[2] = ( ( sfx_u8 * )src )[1]; \
	( ( sfx_u8 * )dst )[3] = ( ( sfx_u8 * )src )[0]; 

#define SFX_REVERSE_8( src, dst ) \
	( ( sfx_u8 * )dst )[0] = ( ( sfx_u8 * )src )[7]; \
	( ( sfx_u8 * )dst )[1] = ( ( sfx_u8 * )src )[6]; \
	( ( sfx_u8 * )dst )[2] = ( ( sfx_u8 * )src )[5]; \
	( ( sfx_u8 * )dst )[3] = ( ( sfx_u8 * )src )[4]; \
	( ( sfx_u8 * )dst )[4] = ( ( sfx_u8 * )src )[3]; \
	( ( sfx_u8 * )dst )[5] = ( ( sfx_u8 * )src )[2]; \
	( ( sfx_u8 * )dst )[6] = ( ( sfx_u8 * )src )[1]; \
	( ( sfx_u8 * )dst )[7] = ( ( sfx_u8 * )src )[0]; 


#define SFX_U16_COMPARE_LESS( src, dst ) \
	( ( ( ( sfx_16 )src >= 0 && ( sfx_16 )dst >= 0 ) && src < dst ) \
	|| ( ( ( sfx_16 )src < 0 && ( sfx_16 )dst < 0 ) && src < dst ) \
	|| ( ( sfx_16 )src < 0 && ( sfx_16 )dst > 0 ) \
	|| ( ( sfx_16 )src > 0 && ( sfx_16 )dst < 0 ) ) 

#define sfx_timer sfx_void *
#define sfx_timer_id sfx_void *
typedef enum SfxTimerEvent
{
	SFX_TIMER_EVENT_UNKNOW = 0,
	SFX_TIMER_EVENT_READY,
	SFX_TIMER_EVENT_NORMAL,
	SFX_TIMER_EVENT_DESTROY
}SfxTimerEvent;

typedef enum SfxTimerType
{
	SFX_TIMER_TYPE_UNKNOW = 0,
	SFX_TIMER_TYPE_TIMEOUT = 0x01,
	SFX_TIMER_TYPE_INTERVAL = 0x02,
	SFX_TIMER_TYPE_ALL = 0xFF,
}SfxTimerType;


const sfx_8 * sfxGetHttpMethod( SfxHttpMethod method );
const sfx_8 * sfxGetHttpContent( SfxHttpContentType content );

typedef struct SfxThreadParam
{
    sfx_u32 id;
    sfx_void * param;
    sfx_void * userPtr;
}SfxThreadParam;
typedef sfx_32 ( sfx_stdcall * sfx_timer_handler )( const sfx_void * userPtr, SfxTimerEvent evt, sfx_u64 count, sfx_u32 ms );

sfx_timer sfxCreateTimer( SfxTimerType type );
sfx_timer_id sfxInsertTimer( sfx_timer timer, SfxTimerType type, const sfx_void * userPtr, sfx_timer_handler handler, sfx_u32 delta );
sfx_32 sfxRemoveTimer( sfx_timer timer, sfx_timer_id id );
sfx_void sfxDestroyTimer( sfx_timer timer );

sfx_u32 sfxGetProcessId();
sfx_thread sfxCreateThread( sfx_thread_proc proc, sfx_void * param );
sfx_32 sfxDestroyThread( sfx_thread tid );
sfx_32 sfxWaitThread( sfx_thread tid );

sfx_32 sfxCreateMutex( sfx_mutex ** mtx );
sfx_void sfxDestroyMutex( sfx_mutex * mtx );
sfx_32 sfxLockMutex( sfx_mutex * mtx );
sfx_32 sfxTryLockMutex( sfx_mutex * mtx );
sfx_32 sfxUnlockMutex( sfx_mutex * mtx );

sfx_32 sfxCreateEvent( sfx_event ** evt, sfx_bool signal );
sfx_void sfxDestroyEvent( sfx_event * evt );
sfx_32 sfxSignalEvent( sfx_event * evt );
sfx_32 sfxWaitEvent( sfx_event * evt, sfx_u32 timeout );

sfx_void sfxSetupExceptionDump( const sfx_8 * path, const sfx_8 * module );

sfx_inline sfx_void * sfx_malloc( sfx_u32 size );
sfx_inline sfx_void sfx_free( sfx_void * buf );


sfx_inline sfx_bool sfx_isEmptyStr( const sfx_8 * str )
{
	if ( sfx_null == str )
	{
		return sfx_true;
	}

	return str[0] == 0;
}


sfx_void * sfx_malloc_seq( sfx_u32 size );
sfx_u32 sfx_available_seq( const sfx_void * seq );
sfx_u8 * sfx_malloc_seg( const sfx_void * seq, sfx_u32 size );
sfx_void sfx_reduce_seg( const sfx_void * seq, sfx_u32 delta );
sfx_bool sfx_isSegMem(  const sfx_void * seq, sfx_u8 * seg );
sfx_void sfx_free_seg(  const sfx_void * seq, sfx_u8 * seg );
sfx_void sfx_free_seq(  const sfx_void * seq );


sfx_inline sfx_32 sfx_itoa( sfx_32 integer, sfx_8 * str, sfx_u32 len )
{
    if ( len < 12 )
    {
        return -1;
    }

    sprintf_s( str, len, "%d", integer );

    return 0;
}


sfx_inline sfx_u32 sfx_atou32( const sfx_8 * str )
{
	sfx_u32 ret = 0;
	
	if ( sfx_null != str )
	{
#ifdef SFX_WIN
		sscanf_s( str, "%u", &ret );
#endif
#ifdef SFX_LINUX
		sscanf( str, "%u", &ret );
#endif
	}

	return ret;
}


sfx_inline sfx_u32 sfx_ato32( const sfx_8 * str )
{
	sfx_u32 ret = 0;
	
	if ( sfx_null != str )
	{
#ifdef SFX_WIN
		sscanf_s( str, "%d", &ret );
#endif
#ifdef SFX_LINUX
		sscanf( str, "%d", &ret );
#endif
	}

	return ret;
}


sfx_inline sfx_u64 sfx_atou64( const sfx_8 * str )
{
	sfx_u64 ret = 0;

	if ( sfx_null != str )
	{	
#ifdef SFX_WIN
		sscanf_s( str, "%lld", &ret );
#endif
#ifdef SFX_LINUX
		sscanf( str, "%lld", &ret );
#endif
	}

	return ret;
}


sfx_inline sfx_f32 sfx_atof32( const sfx_8 * str )
{
	if ( sfx_null == str )
	{
		return 0;
	}

	return ( sfx_f32 )atof( str );
}


sfx_inline sfx_f64 sfx_atof64( const sfx_8 * str )
{
	if ( sfx_null == str )
	{
		return 0;
	}

	return atof( str );
}

sfx_inline sfx_bool sfx_isNumber( const sfx_8 * str )
{
	sfx_32 pos = 0;

	if ( sfx_null == str || 0 == strlen( str ) )
	{
		return sfx_false;
	}

	while ( 0x00 != str[pos] )
	{
		if ( !( 0x30 <= str[pos] && 0x39 >= str[pos] ) )
		{
			return sfx_false;
		}
		pos++;
	}

	return sfx_true;
}


sfx_inline sfx_void sfx_strrchr( sfx_8 * str, sfx_8 src, sfx_8 dst )
{
    while ( 0 != str[0] )
    {
        if ( str[0] == src )
        {
            str[0] = dst;
        }
        str++;
    }

    return;
}

sfx_inline const sfx_8 * sfx_memcmp( const sfx_8 * src, sfx_u32 srcLen, const sfx_8 * dst, sfx_u32 dstLen )
{
	sfx_u32 index = 0;
	sfx_u32 pos = 0;

	if ( sfx_null == src || sfx_null == dst || 0 == srcLen || 0 == dstLen )
	{
		return sfx_null;
	}

	while ( index < srcLen )
	{
		if ( src[index] == dst[0] && index + dstLen <= srcLen )
		{
			pos = 1;
			index++;
			while ( pos < dstLen )
			{
				if ( src[index] != dst[pos] )
				{
					break;
				}
				index++;
				pos++;
			}
			if ( dstLen == pos )
			{
				return src + index - dstLen;
			}
		}
		index++;
	}

	return 0;
}

sfx_inline sfx_32 sfx_memicmp( const sfx_8 * src, const sfx_8 * dst, sfx_u32 len )
{
	sfx_u32 index = 0;

	if ( sfx_null == src || sfx_null == dst || 0 == len )
	{
		return -1;
	}

	while ( index < len )
	{
		if ( ( src[index] & 0xDF ) != ( dst[index] & 0xDF ) )
		{
			return -1;
		}
		index++;
	}

	return 0;
}


sfx_inline sfx_32 sfx_stricmp( const sfx_8 * src, const sfx_8 * dst )
{
	sfx_32 srcLen = 0;
	sfx_32 dstLen = 0;

	if ( sfx_null == src || sfx_null == dst )
	{
		return -1;
	}

	srcLen = ( sfx_32 )strlen( src );
	dstLen = ( sfx_32 )strlen( dst );
	if ( srcLen != dstLen )
	{
		return -1;
	}

	return sfx_memicmp( src, dst, srcLen );
}


sfx_inline sfx_32 sfx_strcpy( const sfx_8 * src, sfx_8 * dst, sfx_32 size )
{
	sfx_32 len = 0;

	if ( sfx_null == src || sfx_null == dst 
		|| ( len = ( sfx_32 )strlen( src ) + sizeof( sfx_8 ) ) > size )
	{
		return 0;
	}

	memcpy( dst, src, len );
	
	return len;
}

sfx_inline sfx_bool sfx_dir_exist( const sfx_8 * src )
{
#ifdef SFX_WIN
    DWORD dwAttrib = GetFileAttributesA( src );

    return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
#endif
#ifdef SFX_LINUX
	DIR * dir = sfx_null;

	dir = opendir( src );
	if ( sfx_null == dir )
	{
		return sfx_false;
	}
	closedir( dir );
	dir = sfx_null;

	return sfx_true;
#endif
}

typedef sfx_void ( * SFX_ELMS_HANDLER )( sfx_void * elms );

#ifndef __SFX__SfxStack__TYPE__
#define __SFX__SfxStack__TYPE__
#define SFX_STACK_ELMS_HANDLER SFX_ELMS_HANDLER

typedef struct SfxStack
{
	sfx_u32 sp;
	sfx_u32 size;
	sfx_void ** stack;
	SFX_STACK_ELMS_HANDLER hdr;
}SfxStack;

typedef struct SfxSafeStack
{
	SfxStack * stack;
	sfx_mutex * mtx;
}SfxSafeStack;


sfx_inline sfx_32 sfxAllocStack( sfx_u32 size, SfxStack ** stack, SFX_STACK_ELMS_HANDLER hdr );
sfx_inline sfx_void sfxFreeStack( SfxStack * stack );
sfx_inline sfx_void sfxResetStack( SfxStack * stack );
sfx_inline sfx_32 sfxStackPush( SfxStack * stack, sfx_void * elm );
sfx_inline sfx_32 sfxStackPop( SfxStack * stack, sfx_void ** elm );
sfx_inline sfx_bool sfxStackIsEmpty( SfxStack * stack );

sfx_inline sfx_32 sfxAllocStack( sfx_u32 size, SfxStack ** stack, SFX_STACK_ELMS_HANDLER hdr )
{
	*stack = sfx_null;
	if ( sfx_null == hdr )
	{
		return SFX_RLT_ERROR;
	}

#ifdef SFX_DEBUG_PRINT_INFO
	printf( "_sfxAllocStack.\n" );
#endif

	*stack = ( SfxStack * )sfx_malloc( sizeof( SfxStack ) );
	if ( sfx_null == *stack )
	{
		return SFX_RLT_ERROR;
	}

	memset( *stack, 0, sizeof( SfxStack ) );
	( *stack )->size = size;
	size = sizeof( sfx_void * ) * ( *stack)->size;
	( *stack )->stack = ( sfx_void ** )sfx_malloc( size );
	if ( ( *stack )->stack )
	{
		memset( ( *stack )->stack, 0, size );
		( *stack )->hdr = hdr;

		return SFX_RLT_OK;
	}

	sfx_free( *stack );
	*stack = sfx_null;

	return SFX_RLT_ERROR;
}

sfx_inline sfx_void sfxFreeStack( SfxStack * stack )
{
	if ( sfx_null == stack )
	{
		return;
	}

#ifdef SFX_DEBUG_PRINT_INFO
	printf( "_sfxFreeStack.\n" );
#endif

	sfxResetStack( stack );

	sfx_free( stack->stack );
	stack->stack = sfx_null;

	sfx_free( stack );
	stack = sfx_null;

	return;
}


sfx_inline sfx_void sfxResetStack( SfxStack * stack )
{
	if ( sfx_null == stack )
	{
		return;
	}

	while ( stack->sp )
	{
		stack->hdr( stack->stack[--stack->sp] );
		stack->stack[stack->sp] = sfx_null;
	}

	return;
}


sfx_inline sfx_32 sfxStackPush( SfxStack * stack, sfx_void * elm )
{
	if ( stack->sp < stack->size )
	{
		stack->stack[stack->sp++] = elm;
		elm = sfx_null;

		return SFX_RLT_OK;
	}

	return SFX_RLT_ERROR;
}


sfx_inline sfx_32 sfxStackPop( SfxStack * stack, sfx_void ** elm )
{
	*elm = sfx_null;
	if ( stack->sp )
	{
		*elm = stack->stack[--stack->sp];
		stack->stack[stack->sp] = sfx_null;

		return SFX_RLT_OK;
	}

	return SFX_RLT_ERROR;
}


sfx_inline sfx_bool sfxStackIsEmpty( SfxStack * stack )
{
	return 0 == stack->sp;
}


sfx_inline sfx_32 sfxAllocSafeStack( sfx_u32 size, SfxSafeStack ** stack, SFX_STACK_ELMS_HANDLER hdr );
sfx_inline sfx_void sfxFreeSafeStack( SfxSafeStack * stack );
sfx_inline sfx_32 sfxSafeStackPush( SfxSafeStack * stack, sfx_void * elm );
sfx_inline sfx_32 sfxSafeStackPop( SfxSafeStack * stack, sfx_void ** elm );
sfx_inline sfx_bool sfxSafeStackIsEmpty( SfxSafeStack * stack );

sfx_inline sfx_32 sfxAllocSafeStack( sfx_u32 size, SfxSafeStack ** stack, SFX_STACK_ELMS_HANDLER hdr )
{
	*stack = ( SfxSafeStack * )sfx_malloc( sizeof( SfxSafeStack ) );
	if ( sfx_null == *stack )
	{
		return SFX_RLT_ERROR;
	}

	sfxCreateMutex( &( *stack )->mtx );
	if ( sfx_null == ( *stack )->mtx )
	{
		sfx_free( *stack );
		*stack = sfx_null;

		return SFX_RLT_ERROR;
	}

	sfxAllocStack( size, &( *stack )->stack, hdr );
	if ( sfx_null == ( *stack )->stack )
	{
		return SFX_RLT_OK;
	}

	sfxDestroyMutex( ( *stack )->mtx );
	( *stack )->mtx = sfx_null;

	sfx_free( *stack );
	*stack = sfx_null;	

	return SFX_RLT_ERROR;
}

sfx_inline sfx_void sfxFreeSafeStack( SfxSafeStack * stack )
{
	if ( sfx_null == stack )
	{
		return;
	}

	sfxLockMutex( stack->mtx );
	sfxFreeStack( stack->stack );
	stack->stack = sfx_null;
	sfxUnlockMutex( stack->mtx );
		
	sfxDestroyMutex( stack->mtx );
	stack->mtx = sfx_null;
	
	sfx_free( stack );
	stack = sfx_null;

	return;
}

sfx_inline sfx_32 sfxSafeStackPush( SfxSafeStack * stack, sfx_void * elm )
{
	sfx_32 ret = SFX_RLT_OK;

	sfxLockMutex( stack->mtx );
	ret = sfxStackPush( stack->stack, elm );
	sfxUnlockMutex( stack->mtx );

	return ret;
}

sfx_inline sfx_32 sfxSafeStackPop( SfxSafeStack * stack, sfx_void ** elm )
{
	sfx_32 ret = SFX_RLT_OK;

	sfxLockMutex( stack->mtx );
	ret = sfxStackPop( stack->stack, elm );
	sfxUnlockMutex( stack->mtx );

	return ret;
}

sfx_inline sfx_bool sfxSafeStackIsEmpty( SfxSafeStack * stack )
{
	sfx_bool ret = sfx_false;

	sfxLockMutex( stack->mtx );
	ret = sfxStackIsEmpty( stack->stack );
	sfxUnlockMutex( stack->mtx );

	return ret;
}
#endif


#ifndef __SFX__SfxRing__TYPE__
#define __SFX__SfxRing__TYPE__
#define SFX_RING_ELMS_HANDLER SFX_ELMS_HANDLER


typedef struct SfxRing
{
	volatile sfx_32 read;
	volatile sfx_32 write;
	volatile sfx_32 size;
	sfx_void ** elms;
	SFX_RING_ELMS_HANDLER hdr;
}SfxRing;

sfx_inline sfx_32 sfxAllocRing( sfx_u32 size, SfxRing ** ring, SFX_RING_ELMS_HANDLER hdr );
sfx_inline sfx_void sfxResetRing( SfxRing * ring );
sfx_inline sfx_void sfxFreeRing( SfxRing * ring );
sfx_inline sfx_32 sfxRingPush( SfxRing * ring, sfx_void * elm );
sfx_inline sfx_32 sfxRingPop( SfxRing * ring, sfx_void ** elm );
sfx_inline sfx_bool sfxRingIsEmpty( SfxRing * ring );

sfx_inline sfx_32 sfxAllocRing( sfx_u32 size, SfxRing ** ring, SFX_RING_ELMS_HANDLER hdr )
{
	sfx_32 ret = SFX_RLT_ERROR;
	
	*ring = sfx_null;
	if ( sfx_null == hdr )
	{
		return ret;
	}

#ifdef SFX_DEBUG_PRINT_INFO 
	printf( "_sfxAllocRing.\n" );
#endif

	*ring = ( SfxRing * )sfx_malloc( sizeof( SfxRing ) );
	if ( *ring )
	{
		memset( *ring, 0, sizeof( SfxRing ) );
		( *ring )->size = size;
		( *ring )->elms = ( sfx_void ** )sfx_malloc( sizeof( sfx_void * ) * ( *ring )->size );
		if ( sfx_null == ( *ring )->elms )
		{
			sfx_free( *ring );
			*ring = sfx_null;

			return ret;
		}
		
		memset( ( *ring )->elms, 0, sizeof( sfx_void * ) * ( *ring )->size ); 
		
		( *ring )->hdr = hdr;
		ret = SFX_RLT_OK;
	}

	return ret;
}


sfx_inline sfx_void sfxResetRing( SfxRing * ring )
{
	sfx_32 i = 0;
	if ( sfx_null == ring )
	{
		return;
	}

	for ( i = 0; i < ring->size; i++ )
	{
		if ( ring->elms[i] )
		{
			ring->hdr( ring->elms[i] );
			ring->elms[i] = sfx_null;
		}
	}
	ring->read = 0;
	ring->write = 0;

	return;
}


sfx_inline sfx_void sfxFreeRing( SfxRing * ring )
{
	sfx_32 ret = SFX_RLT_ERROR;

#ifdef SFX_DEBUG_PRINT_INFO
	printf( "_sfxFreeRing.\n" );
#endif

	if ( sfx_null == ring )
	{
		return;
	}

	sfxResetRing( ring );

	sfx_free( ring->elms );
	ring->elms = sfx_null;

	sfx_free( ring );
	ring = sfx_null;

	return;
}


sfx_32 sfx_inline sfxRingPush( SfxRing * ring, sfx_void * elm )
{
	sfx_32 ret = SFX_RLT_ERROR;

	if ( sfx_null == ring )
	{
		return SFX_RLT_ERROR;
	}
	
	if ( sfx_null == ring->elms[ring->write] )
	{
		ring->elms[ring->write] = elm;
		ring->write++;
		ret = SFX_RLT_OK;
	}
	if ( ring->size == ring->write )
	{
		ring->write = 0;
	}

	return ret;
}


sfx_inline sfx_32 sfxRingPop( SfxRing * ring, sfx_void ** elm )
{
	sfx_32 ret = SFX_RLT_ERROR;

	*elm = sfx_null;
	if ( sfx_null != ring->elms[ring->read] )
	{
		*elm = ring->elms[ring->read];
		ring->elms[ring->read] = sfx_null;
		ring->read++;
		ret = SFX_RLT_OK;
	}
	if ( ring->size == ring->read )
	{
		ring->read = 0;
	}

	return ret;
}

sfx_inline sfx_bool sfxRingIsEmpty( SfxRing * ring )
{
	if ( sfx_null == ring )
	{
		return sfx_true;
	}

	return sfx_null == ring->elms[ring->read];
}


typedef struct SfxSafeRing
{
	volatile sfx_32 read;
	volatile sfx_32 write;
	volatile sfx_32 size;
	volatile sfx_32 count;
	sfx_void ** elms;
	sfx_mutex * mtx;
	SFX_RING_ELMS_HANDLER hdr;
}SfxSafeRing;

sfx_inline sfx_32 sfxAllocSafeRing( sfx_u32 size, SfxSafeRing ** ring, SFX_RING_ELMS_HANDLER hdr );
sfx_inline sfx_void sfxResetSafeRing( SfxSafeRing * ring );
sfx_inline sfx_void sfxFreeSafeRing( SfxSafeRing * ring );
sfx_inline sfx_32 sfxSafeRingPush( SfxSafeRing * ring, sfx_void * elm );
sfx_inline sfx_32 sfxSafeRingPop( SfxSafeRing * ring, sfx_void ** elm );
sfx_inline sfx_bool sfxSafeRingIsEmpty( SfxSafeRing * ring );
sfx_inline sfx_32 sfxSafeRingSize( SfxSafeRing * ring );

sfx_inline sfx_32 sfxAllocSafeRing( sfx_u32 size, SfxSafeRing ** ring, SFX_RING_ELMS_HANDLER hdr )
{
	sfx_32 ret = SFX_RLT_ERROR;
	
	*ring = sfx_null;
	if ( sfx_null == hdr )
	{
		return ret;
	}

#ifdef SFX_DEBUG_PRINT_INFO 
	printf( "_sfxAllocSafeRing.\n" );
#endif

	*ring = ( SfxSafeRing * )sfx_malloc( sizeof( SfxSafeRing ) );
	if ( *ring )
	{
		memset( *ring, 0, sizeof( SfxSafeRing ) );
		( *ring )->size = size;
		( *ring )->elms = ( sfx_void ** )sfx_malloc( sizeof( sfx_void * ) * ( *ring )->size );
		if ( sfx_null == ( *ring )->elms )
		{
			sfx_free( *ring );
			*ring = sfx_null;

			return ret;
		}
		
		memset( ( *ring )->elms, 0, sizeof( sfx_void * ) * ( *ring )->size ); 
		sfxCreateMutex( &( *ring )->mtx );
		if ( sfx_null == ( *ring )->mtx )
		{
			sfx_free( ( *ring )->elms );
			( *ring )->elms = sfx_null;

			sfx_free( *ring );
			*ring = sfx_null;

			return ret;
		}
			
		ret = SFX_RLT_OK;
		( *ring )->hdr = hdr;
	}

	return ret;
}


sfx_inline sfx_void sfxResetSafeRing( SfxSafeRing * ring )
{
	sfx_32 i = 0;
	if ( sfx_null == ring )
	{
		return;
	}

	sfxLockMutex( ring->mtx );
	for ( i = 0; i < ring->size; i++ )
	{
		if ( ring->elms[i] )
		{
			ring->hdr( ring->elms[i] );
			ring->elms[i] = sfx_null;
		}
	}
	ring->read = 0;
	ring->write = 0;
	ring->count = 0;
	sfxUnlockMutex( ring->mtx );

	return;
}


sfx_inline sfx_32 sfxSafeRingSize( SfxSafeRing * ring )
{
	return ring->count;
}


sfx_inline sfx_void sfxFreeSafeRing( SfxSafeRing * ring )
{
	sfx_32 ret = SFX_RLT_ERROR;

#ifdef SFX_DEBUG_PRINT_INFO
	printf( "_sfxFreeSafeRing.\n" );
#endif

	if ( sfx_null == ring )
	{
		return;
	}

	sfxResetSafeRing( ring );

	sfxDestroyMutex( ring->mtx );
	ring->mtx = sfx_null;

	sfx_free( ring->elms );
	ring->elms = sfx_null;

	sfx_free( ring );
	ring = sfx_null;

	return;
}


sfx_32 sfx_inline sfxSafeRingPush( SfxSafeRing * ring, sfx_void * elm )
{
	sfx_32 ret = SFX_RLT_ERROR;

	if ( sfx_null == ring )
	{
		return SFX_RLT_ERROR;
	}
	
	sfxLockMutex( ring->mtx );
	if ( sfx_null == ring->elms[ring->write] )
	{
		ring->elms[ring->write] = elm;
		ring->write++;
		ring->count++;
		ret = SFX_RLT_OK;
	}
	if ( ring->size == ring->write )
	{
		ring->write = 0;
	}
	sfxUnlockMutex( ring->mtx );

	return ret;
}


sfx_inline sfx_32 sfxSafeRingPop( SfxSafeRing * ring, sfx_void ** elm )
{
	sfx_32 ret = SFX_RLT_ERROR;

	*elm = sfx_null;
	sfxLockMutex( ring->mtx );
	if ( sfx_null != ring->elms[ring->read] )
	{
		*elm = ring->elms[ring->read];
		ring->elms[ring->read] = sfx_null;
		ring->read++;
		ring->count--;
		ret = SFX_RLT_OK;
	}
	if ( ring->size == ring->read )
	{
		ring->read = 0;
	}
	sfxUnlockMutex( ring->mtx );

	return ret;
}

sfx_inline sfx_bool sfxSafeRingIsEmpty( SfxSafeRing * ring )
{
	if ( sfx_null == ring )
	{
		return sfx_true;
	}

	return sfx_null == ring->elms[ring->read];
}

#endif


#ifndef __SFX__SfxQueue__TYPE__
#define __SFX__SfxQueue__TYPE__
#define SFX_QUEUE_ELMS_HANDLER SFX_ELMS_HANDLER

typedef struct SfxQueElms
{
	sfx_void * elm;
	struct SfxQueElms * next;
}SfxQueElms;

typedef struct SfxSafeQueue
{
	sfx_mutex * mtx;
	SfxQueElms * head;
	SfxQueElms * tail;
	volatile sfx_u32 size;
	SFX_QUEUE_ELMS_HANDLER hdr;
}SfxSafeQueue;

sfx_inline sfx_32 sfxAllocSafeQueue( SfxSafeQueue ** queue, SFX_QUEUE_ELMS_HANDLER hdr );
sfx_inline sfx_void sfxResetSafeQueue( SfxSafeQueue * queue );
sfx_inline sfx_void sfxFreeSafeQueue( SfxSafeQueue * queue );
sfx_inline sfx_32 sfxSafeQueuePush( SfxSafeQueue * queue, sfx_void * elm );
sfx_inline sfx_32 sfxSafeQueuePop( SfxSafeQueue * queue, sfx_void ** elm );
sfx_inline sfx_bool sfxSafeQueueIsEmpty( SfxSafeQueue * queue );
sfx_inline sfx_u32 sfxSafeQueueSize( SfxSafeQueue * queue );

sfx_inline sfx_32 sfxAllocSafeQueue( SfxSafeQueue ** queue, SFX_QUEUE_ELMS_HANDLER hdr )
{
	sfx_32 ret = SFX_RLT_ERROR;
	
	*queue = sfx_null;
	if ( sfx_null == hdr )
	{
		return ret;
	}

	*queue = ( SfxSafeQueue * )sfx_malloc( sizeof( SfxSafeQueue ) );
	if ( *queue )
	{
		memset( *queue, 0, sizeof( SfxSafeQueue ) );
		sfxCreateMutex( &( *queue )->mtx );
		if ( sfx_null == ( *queue )->mtx )
		{
			sfx_free( *queue );
			*queue = sfx_null;

			return ret;
		}
			
		ret = SFX_RLT_OK;
		( *queue )->hdr = hdr;
	}

	return ret;
}


sfx_inline sfx_void sfxResetSafeQueue( SfxSafeQueue * queue )
{
	sfx_void * elm = sfx_null;

	if ( sfx_null == queue )
	{
		return;
	}

	sfxLockMutex( queue->mtx );
	while ( !sfxSafeQueueIsEmpty( queue ) )
	{
		sfxSafeQueuePop( queue, &elm );
		if ( elm )
		{
			queue->hdr( elm );
			elm = sfx_null;
		}
	}
	queue->size = 0;
	sfxUnlockMutex( queue->mtx );

	return;
}


sfx_inline sfx_void sfxFreeSafeQueue( SfxSafeQueue * queue )
{
	sfx_32 ret = SFX_RLT_ERROR;

	if ( sfx_null == queue )
	{
		return;
	}

	sfxResetSafeQueue( queue );
	
	sfxDestroyMutex( queue->mtx );
	queue->mtx = sfx_null;

	sfx_free( queue );
	queue = sfx_null;

	return;
}


sfx_32 sfx_inline sfxSafeQueuePush( SfxSafeQueue * queue, sfx_void * elm )
{
	sfx_32 ret = SFX_RLT_ERROR;
	SfxQueElms * elms = sfx_null;
	
	elms = ( SfxQueElms * )sfx_malloc( sizeof( SfxQueElms ) );
	if ( sfx_null == elms )
	{
		return ret;
	}
	memset( elms, 0, sizeof( SfxQueElms ) );
	elms->elm = elm;
	sfxLockMutex( queue->mtx );
	if ( sfx_null == queue->tail )
	{
		queue->tail = elms;
		queue->head = elms;
	}
	else
	{
		queue->tail->next = elms;
		queue->tail = elms;
	}
	queue->size++;
	sfxUnlockMutex( queue->mtx );

	return SFX_RLT_OK;
}


sfx_inline sfx_32 sfxSafeQueuePop( SfxSafeQueue * queue, sfx_void ** elm )
{
	sfx_32 ret = SFX_RLT_ERROR;
	SfxQueElms * elms = sfx_null;

	*elm = sfx_null;
	sfxLockMutex( queue->mtx );
	if ( queue->head )
	{
		elms = queue->head;
		queue->head = elms->next;
		elms->next = sfx_null;
		if ( sfx_null == queue->head )
		{
			queue->tail = sfx_null;
		}
		*elm = elms->elm;
		sfx_free( elms );
		elms = sfx_null;
		queue->size--;
	}
	sfxUnlockMutex( queue->mtx );

	return SFX_RLT_OK;
}


sfx_inline sfx_u32 sfxSafeQueueSize( SfxSafeQueue * queue )
{
	if ( sfx_null == queue )
	{
		return 0;
	}

	return queue->size;
}


sfx_inline sfx_bool sfxSafeQueueIsEmpty( SfxSafeQueue * queue )
{
	if ( sfx_null == queue )
	{
		return sfx_true;
	}

	return sfx_null == queue->tail;
}

#endif


sfx_inline sfx_32 sfxAsciiToHex( const sfx_8 * ascii, sfx_u32 len, sfx_u8 * hex, sfx_u32 size )
{
	sfx_u8 * v = sfx_null;
	sfx_u8 * s = sfx_null;

	if ( 0 == len || 0 == size )
	{
		return size;
	}

	v = hex;
	s = ( sfx_u8 * )ascii;
	ascii += len;
	while ( s < ( sfx_u8 * )ascii && size )
	{
		v[0] = ( ( ( s[0] - ( s[0] > 0x39 ? 0x37 : 0x30 ) ) & 0x0F ) << 4 ) | ( ( s[1] - ( s[1] > 0x39 ? 0x37 : 0x30 ) ) & 0x0F );
		v++;
		size--;
		s += 2;
	}

	return ( sfx_32 )( v - hex );
} 


sfx_inline sfx_32 sfxHexToAscii( sfx_u8 * hex, sfx_u32 len, sfx_8 * ascii, sfx_u32 size, sfx_u8 flags )
{
	sfx_u8 offset = 0;

	if ( 0 == len || 0 == size )
	{
		return 0;
	}

	offset = ( flags & 0x80 ) ? 0x27 : 0x07;
	flags = flags & 0x7F;
	if ( flags )
	{
		if ( len * 3 >= size )
		{
			len = ( size - 1 ) / 3;
		}
	}
	else
	{
		if ( len * 2 >= size )
		{
			len = ( size - 1 ) / 2;
		}
	}
	size = len;

	do
	{
		ascii[0] = ( ( hex[0] & 0xF0 ) >> 4 ) + 0x30;  /*0~9: +0x30*/
		if ( ascii[0] > 0x39 )
		{
			ascii[0] += offset;                           /*A~F: +0x37*/
		}

		ascii++;
		ascii[0] = ( hex[0] & 0x0F ) + 0x30;          
		if ( ascii[0] > 0x39 )
		{
			ascii[0] += offset;
		}		
		ascii++;
		hex++;
		len--;
		if ( 0 == len )
		{
			ascii[0] = 0;
			flags = 0x00;
		}

		if ( 0x00 != flags )
		{
			ascii[0] = flags;               /*add separator*/ 
			ascii++;
		}
	} while ( 0 != len );

	return size;
}

#ifndef SFX_LOG_HANDLER
#define SFX_LOG_HANDLER
typedef enum SfxLogLevel
{
    SFX_LOG_LEVEL_UNKNOW = 0,               // 未知日志等级
    SFX_LOG_LEVEL_INFO = 0x01,              // 普通日志信息 (如:无关紧要的信息输出)
    SFX_LOG_LEVEL_IMPORTANT = 0x02,         // 重要的日志通知,模块一切正常(如:重要流程通知)
    SFX_LOG_LEVEL_WARNING = 0x04,           // 警告日志,即将可能发生致命错误(如:资源不足)
    SFX_LOG_LEVEL_FATAL = 0x08,             // 致命错误,模块将停止工作(如:网络断开)
	SFX_LOG_LEVEL_CLEANUP = 0x10,           // 此模块已退出所有线程、流程,不再执行任何代码,
	                                        // 通常通过该日志等级回调,实现在回调内释放模块自身
									        // 本日志等级在模块生命周期内只会发生一次调用
    SFX_LOG_LEVEL_DEBUG = 0x20,             // 调试模式的信息
	SFX_LOG_LEVEL_USERDEFINE = 0x40,        // 扩展日志类型
	SFX_LOG_LEVEL_ALL = 0xFF                // 
											
}SfxLogLevel;


typedef struct SfxLogUserDefine
{
	sfx_u32 id;
	sfx_u32 userSize;
	sfx_void * userData;
}SfxLogUserDefine;


typedef struct SfxLogConfig
{
	sfx_u32 id;
	sfx_u8 logMask;
	sfx_u8 limitDay;
	sfx_bool output;
	sfx_u32 limitSize;
	sfx_8 path[SFX_DEFAULT_PATH_SIZE];
	sfx_8 module[SFX_DEFAULT_MIN_SIZE];
}SfxLogConfig;
sfx_32 sfxAppendLog( SfxLogLevel level, const sfx_8 * log, ... );

#define SFX_STD_LOG_MODULE_EXIT "module exited"
#define SFX_STD_LOG_RECONNECT   "module reconnecting"
#define SFX_STD_LOG_PROGRESS    "progress"
#define SFX_STD_LOG_TIMEOUT     "timeout"
#define SFX_STD_LOG_NORMAL      "normal"
#define SFX_STD_LOG_READY       "ready"
#define SFX_STD_LOG_DONE        "done"

/**************************************************
* 接口:SfxLogHandler
* 功能:日志回调接口
* 参数:const sfx_void * userPtr 用户自定义数据
*      SfxLogLevel level 日志等级
*      const sfx_8 * log 日志内容
*      sfx_u32 logLen 日志信息长度,如果log指向字符串,
*      可忽略本参数
* 返回:已处理返回 SFX_RLT_OK,未处理返回 SFX_RLT_ERROR。
* 备注:本接口内不可进行阻塞/耗时操作。
* 作者:Inpilen@foxmail.com
**************************************************/
typedef sfx_32 ( sfx_stdcall * SfxLogHandler )(
											   const sfx_void * userPtr,
											   SfxLogLevel level,
											   const sfx_8 * log,
											   sfx_u32 logLen
											   );

sfx_inline sfx_32 sfx_stdcall SfxLogDefaultHandler(
												   const sfx_void * userPtr,
												   SfxLogLevel level,
												   const sfx_8 * log,
												   sfx_u32 logLen
												   )
{

	return 0;
}

#endif


//#define SFX_MEMORY_DEBUG_MONITOR
sfx_inline sfx_void * sfx_malloc( sfx_u32 size )
{

#ifdef SFX_MEMORY_DEBUG_MONITOR
	static sfx_u32 dbg = 0;
	sfx_void * ret = sfx_null;

	ret = malloc( size + 8 + 4 );
	if ( ret )
	{
		*( sfx_u32 * )ret = 0xF1F1F1F1;
		*( sfx_u32 * )( ( sfx_u8 * )ret + 4 ) = size;
		ret = ( ( ( sfx_u8 * )ret ) + 8 );
		*( sfx_u32 * )( ( sfx_u8 * )ret + size ) = 0xF1F1F1F1;
	}
	//sfxAppendLog( SFX_LOG_LEVEL_DEBUG, "_sfx_malloc.%d %p\n", dbg++, ret );

	return ret;
#else
	return malloc( size );
#endif
}

sfx_inline sfx_void sfx_free( sfx_void * buf )
{
	sfx_u32 ret = 0;

	if ( sfx_null == buf )
	{
		return;
	}

#ifdef SFX_MEMORY_DEBUG_MONITOR
	//sfxAppendLog( SFX_LOG_LEVEL_DEBUG, "_sfx_free.%d %p\n", *( sfx_u32 * )( ( ( sfx_u8 * )buf ) - 4 ), buf );
	buf = ( ( ( sfx_u8 * )buf ) - 8 );
	if ( 0xF1F1F1F1 != *( sfx_u32 * )( ( sfx_u8 * )buf + 8 + *( sfx_u32 * )( ( sfx_u8 * )buf + 4 ) ) )
	{
		ret = *( sfx_u32 * )( ( sfx_u8 * )buf + 4 );
	}
	free( buf );
#else
	free( buf );
#endif

	return; 
}


#ifndef __SFX__SfxBufs__TYPE__
#define __SFX__SfxBufs__TYPE__
typedef struct SfxBufs SfxBufs;
typedef struct SfxBufsHdr SfxBufsHdr;
typedef sfx_void ( * SFX_FREE_BUF )( SfxBufs * bufs );
typedef SfxBufs * ( * SFX_ALLOC_BUF )( const SfxBufsHdr * hdr );

typedef struct SfxBufsHdr   
{
	sfx_32 group;
	sfx_void * userPtr;
	SFX_FREE_BUF free;
	SFX_ALLOC_BUF alloc;
}SfxBufsHdr;


typedef struct SfxBufs
{
	sfx_u8 * buf;
	sfx_u32 size;
	sfx_u32 read;
	sfx_u32 write;
	struct SfxBufs * next;
	const SfxBufsHdr * hdr;
}SfxBufs;


sfx_inline sfx_32 sfxWriteBufs( SfxBufs ** bufs, sfx_u8 * data, sfx_u32 len )
{
    sfx_u32 cpyLen = 0;
    SfxBufs * tmp = sfx_null;

    tmp = *bufs;
_CONTINUE_WRITE:
    cpyLen = tmp->size - tmp->write;
    cpyLen = cpyLen > len ? len : cpyLen;
    memcpy( tmp->buf + tmp->write, data, cpyLen );
    tmp->write += cpyLen;
    data += cpyLen;
    len -= cpyLen;
    if ( len > 0 )
    {
        tmp->next = tmp->hdr->alloc( tmp->hdr );
        if ( sfx_null == tmp->next )
        {
            return SFX_RLT_ERROR;
        }
        tmp = tmp->next;
        *bufs = tmp;
        goto _CONTINUE_WRITE;
    }

    return SFX_RLT_OK;
}

sfx_inline sfx_void SfxBufsDefaultFree( SfxBufs * bufs )
{
	SfxBufs * tmp = sfx_null;

	if ( sfx_null == bufs )
	{
		return;
	}

	while ( bufs )
	{
		tmp = bufs->next;
		sfx_free( bufs );
		bufs = sfx_null;
		bufs = tmp;
	}

	return;
}


sfx_inline SfxBufs * SfxBufsDefaultAlloc( const SfxBufsHdr * hdr )
{
	SfxBufs * ret = sfx_null;

	ret = ( SfxBufs * )sfx_malloc( SFX_DEFAULT_PAGE_SIZE );
	if ( sfx_null == ret )
	{
		return ret;
	}

	memset( ret, 0, SFX_DEFAULT_PAGE_SIZE );
	ret->hdr = hdr;
	ret->buf = ( sfx_u8 * )ret + sizeof( SfxBufs );
	ret->size = SFX_DEFAULT_PAGE_SIZE - sizeof( SfxBufs );

	return ret;
}

sfx_inline sfx_void SfxBufsDefaultHandler( sfx_void * elms )
{
	SfxBufs * buf = sfx_null;

	if ( sfx_null == elms )
	{
		return;
	}

	buf = ( SfxBufs * )elms;
	buf->hdr->free( buf );
	buf = sfx_null;

	return;
}

/*
sfx_inline sfx_void SfxBufsDefaultFree( SfxBufs ** bufs )
{
	SfxBufs * tmp = sfx_null;

	while ( *bufs )
	{
		tmp = (*bufs)->next;
		if ( (*bufs)->buf )
		{
			sfx_free( ( *bufs )->buf );
			( *bufs )->buf = sfx_null;
		}

		sfx_free( *bufs );
		*bufs = sfx_null;
		*bufs = tmp;
	}

	return;
}

sfx_inline SfxBufs * SfxBufsDefaultAlloc( SfxPtlBufsHdr * ptl )
{
	SfxBufs * ret = sfx_null;

	ret = ( SfxBufs * )sfx_malloc( sizeof( SfxBufs ) );
	if ( sfx_null == ret )
	{
		return ret;
	}

	memset( ret, sfx_null, sizeof( SfxBufs ) );
	ret->size = 4096;
	ret->ptl = ptl;
	ret->buf = ( sfx_u8 * )sfx_malloc( sizeof( sfx_u8 ) * ret->size );
	if ( sfx_null == ret->buf )
	{
		SfxBufsDefaultFree( &ret );
		ret = sfx_null;
	}

	return ret;
}*/
#endif

sfx_inline sfx_bool sfx_isValidSocket( sfx_socket skt );
sfx_inline sfx_32 sfx_safeSend( sfx_socket skt, const sfx_u8 * data, sfx_u32 len );
sfx_inline sfx_32 sfx_recv( sfx_socket skt, sfx_u8 * data, sfx_u32 size );
sfx_inline sfx_32 sfx_sendto( sfx_socket skt, sfx_u8 * data, sfx_u32 size, struct sockaddr_in * addr );
sfx_inline sfx_32 sfx_recvfrom( sfx_socket skt, sfx_u8 * data, sfx_u32 size, struct sockaddr_in * addr, sfx_32 * addrLen );
sfx_u32 sfxGetTimeMs();
sfx_u32 sfxGetHTimeMs();
sfx_u64 sfxGetTimeMs64();
sfx_u64 sfxGetHTimeMs64();
sfx_u64 sfxGetHTimeUs();
sfx_u64 sfxGetTickCount();
sfx_void sfxSleepMs( sfx_u32 ms );



typedef struct sfx_date
{
	sfx_u32 days;
	sfx_u16 hour;
	sfx_u16 min;
	sfx_u16 sec;
	sfx_u16 mil; 
}sfx_date;
sfx_u64 sfxGetDate();
// std[0:YMD 1:YMDHMS 2:Y-M-D H:M:S 3:Y-M-DTH:M:S]
sfx_32 sfxGetTime( sfx_8 * date, sfx_u32 len, sfx_u8 std );
sfx_32 sfxTimeToString( sfx_u64 sec, sfx_8 * date, sfx_u32 len, sfx_u8 std );
sfx_u64 sfxStringToTime( const sfx_8 * date, sfx_u8 std );
sfx_void sfxTimeToDate( sfx_u64 ms, sfx_date * date );
sfx_u32 sfxTimeToDays( sfx_u64 sec );

sfx_32 sfx_getguid( sfx_8 * id, sfx_32 size );
/************* socket  ***************/

#ifdef SFX_WIN 
#define sfx_uninitSocketModule() WSACleanup()
#define sfx_getSocketError() WSAGetLastError()
#define sfx_asyncSocketError() \
	( WSAEWOULDBLOCK != WSAGetLastError() && 0 != WSAGetLastError() )
#define sfx_setupAddr( addr, ip ) addr.sin_addr.S_un.S_addr = ip
#define sfx_getAddr( addr ) addr.sin_addr.S_un.S_addr
	

sfx_void sfxInitPortMgr();
sfx_void sfxUninitPortMgr();
sfx_void sfxAppendPortRule( sfx_u16 minPort, sfx_u16 maxPort, sfx_u8 falg );
sfx_u16 sfxAcquirePort( sfx_bool udp );
sfx_void sfxDiscardPort( sfx_u16 port, sfx_bool udp );

sfx_inline sfx_32 sfx_initSocketModule()
{
	sfx_u16 ver = 0;
	WSADATA wsaData = {0};

	ver = MAKEWORD( 2, 2 );
	if ( !( 0 == WSAStartup( ver, &wsaData ) 
		&& ( 2 == LOBYTE( wsaData.wVersion ) && 2 == HIBYTE( wsaData.wVersion ) ) )  )
	{
		return SFX_RLT_ERROR;
	}

	return SFX_RLT_OK;
}

sfx_inline sfx_void sfxAtomIncrement( sfx_atom * atom )
{
	InterlockedIncrement( atom );

	return;
}


sfx_inline sfx_void sfxAtomDecrement( sfx_atom * atom )
{
	InterlockedDecrement( atom );

	return;
}

#endif

#ifdef SFX_LINUX
#define sfx_uninitSocketModule()
#define sfx_initSocketModule()
#define sfx_getSocketError() errno
#define sfx_asyncSocketError() \
	( EAGAIN != errno )
#define sfx_setupAddr( addr, ip ) addr.sin_addr.s_addr = ip
#endif

#define SFX_SOCKET_REPEAT_COUNT   0x00000003
#define SFX_SOCKET_REPEAT_DELAY   0x00000003
#define SFX_SOCKET_SBUF_SIZE      0x00002000


sfx_inline sfx_32 sfx_send( sfx_socket skt, const sfx_u8 * data, sfx_u32 len )
{
	sfx_32 flags = 0;

#ifdef SFX_LINUX
	flags = MSG_NOSIGNAL; 
#endif

	return send( skt, ( const sfx_8 * )data, len, flags );
}


sfx_inline sfx_bool sfx_isValidSocket( sfx_socket skt )
{
	sfx_32 ret = 0;
#ifdef SFX_WIN
	sfx_32 size = 0;
	DWORD rcv_size = 0;
#else
	socklen_t size = 0;
	unsigned long long rcv_size = 0;
#endif
	if ( skt <= 0 )
	{
		return sfx_false;
	}
	
	size = sizeof( rcv_size );
	ret = getsockopt( skt, SOL_SOCKET, SO_RCVBUF, ( char * )&rcv_size, &size );
	if ( 0 == ret && rcv_size > 0 && rcv_size < 0xFFFFF )
	{
		return sfx_true;
	}
	
	ret = sfx_getSocketError();
	//sfx_assert( 10038 == ret );

	return sfx_false;
}

#if 1
sfx_inline sfx_32 sfx_safeSend( sfx_socket skt, const sfx_u8 * data, sfx_u32 len )
{
	sfx_32 err = 0;
	sfx_32 flags = 0;
	sfx_32 ret = 0;
	sfx_32 repeat = SFX_SOCKET_REPEAT_COUNT;

#ifdef SFX_LINUX
	flags = MSG_NOSIGNAL; 
#endif

	while ( len > 0 )
	{
		ret = send( skt, ( const sfx_8 * )data, len, flags );
		if ( ret <= 0 )
		{
			err = sfx_getSocketError();
#ifdef SFX_LINUX
			if ( EPIPE == err )
			{
				return -1;
			}
#endif
			if ( 10035 == err )
			{
				if ( repeat-- > 0 )
				{
					sfxSleepMs( SFX_SOCKET_REPEAT_DELAY );
					continue;
				}
			}
			len = ret;
			break;
		}
		/*
		err = sfx_getSocketError();
#ifdef SFX_LINUX
		if ( EPIPE == err )
		{
			return -1;
		}
#endif
		*/

		len -= ret;
		data += ret;
		repeat = SFX_SOCKET_REPEAT_COUNT;
	}

	return len;
}
#else
sfx_inline sfx_32 sfx_safeSend( sfx_socket skt, const sfx_u8 * data, sfx_u32 len )
{	
	sfx_32 flags = 0;
	sfx_32 ret = 0;
#ifdef SFX_LINUX
	flags = MSG_NOSIGNAL; 
#endif

	while ( len > 0 )
	{
		ret = send( skt, ( const sfx_8 * )data, len, flags );
		if ( ret <= 0 )
		{
			len = ret;
			break;
		}
		len -= ret;
		data += ret;
	}

	return len;
}
#endif


sfx_inline sfx_void sfx_closeSocket( sfx_socket skt )
{
#ifdef SFX_WIN
	closesocket( skt );
#endif
#ifdef SFX_LINUX
	close( skt );
#endif

	return;
}


sfx_inline sfx_socket sfx_accept( sfx_socket skt, struct sockaddr_in * addr )
{
	sfx_32 ret = 0;
	struct timeval timeout = {0};
	sfx_socket sock = sfx_invalid_socket;

#ifdef SFX_WIN
	sfx_32 addrLen = sizeof( struct sockaddr_in );
#endif
#ifdef SFX_LINUX
	sfx_u32 addrLen = sizeof( struct sockaddr_in );
#endif
	memset( addr, 0, addrLen );
	sock = accept( skt, ( struct sockaddr * )addr, &addrLen );
	if ( sfx_invalid_socket != sock )
	{
		timeout.tv_sec = 1;
		ret = setsockopt( sock, SOL_SOCKET, SO_RCVTIMEO, ( const char * )&timeout, sizeof( struct timeval ) );
		//if ( 0 != ret )
		//{
		//	sfx_closeSocket( sock );
		//	sock = sfx_invalid_socket;
		//}
		ret = setsockopt( sock, SOL_SOCKET, SO_SNDTIMEO, ( const char * )&timeout, sizeof( struct timeval ) );
		//if ( 0 != ret )
		//{
		//	sfx_closeSocket( sock );
		//	sock = sfx_invalid_socket;
		//}
	}
	
	return sock;
}

sfx_inline sfx_32 sfx_recv( sfx_socket skt, sfx_u8 * data, sfx_u32 size )
{
	sfx_32 flags = 0;
#ifdef SFX_LINUX
	flags = MSG_NOSIGNAL; 
#endif
	return recv( skt, ( sfx_8 * )data, size, flags );
}


sfx_inline sfx_32 sfx_recvfrom( sfx_socket skt, sfx_u8 * data, sfx_u32 size, struct sockaddr_in * addr, sfx_32 * addrLen )
{

#ifdef SFX_WIN
	return recvfrom( skt, ( sfx_8 * )data, size, 0, ( struct sockaddr * )addr, addrLen );
#endif

#ifdef SFX_LINUX
	return recvfrom( skt, ( sfx_8 * )data, size, MSG_NOSIGNAL, ( struct sockaddr * )addr, ( socklen_t * )addrLen );
#endif
}


sfx_inline sfx_32 sfx_sendto( sfx_socket skt, sfx_u8 * data, sfx_u32 size, struct sockaddr_in * addr )
{

#ifdef SFX_WIN
	return sendto( skt, ( sfx_8 * )data, size, 0, ( struct sockaddr * )addr, sizeof( struct sockaddr_in ) );
#endif

#ifdef SFX_LINUX
	return sendto( skt, ( sfx_8 * )data, size, MSG_NOSIGNAL, ( struct sockaddr * )addr, sizeof( struct sockaddr_in ) );
#endif
}


sfx_inline sfx_32 sfx_setSocketCache( sfx_socket skt, sfx_32 recv, sfx_32 send )
{
	if ( recv > 0 )
	{
		setsockopt( skt, SOL_SOCKET, SO_RCVBUF, ( sfx_8 * )&recv, sizeof( sfx_32 ) );
	}

	if ( send > 0 )
	{
		setsockopt( skt, SOL_SOCKET, SO_SNDBUF, ( sfx_8 * )&send, sizeof( sfx_32 ) );
	}

	return SFX_RLT_OK;
}


sfx_inline sfx_32 sfx_setSocketTimeout( sfx_socket skt, sfx_32 recv, sfx_32 send )
{
	if ( recv > 0 )
	{
		setsockopt( skt, SOL_SOCKET, SO_RCVTIMEO, ( sfx_8 * )&recv, sizeof( sfx_32 ) );
	}

	if ( send > 0 )
	{
		setsockopt( skt, SOL_SOCKET, SO_SNDTIMEO, ( sfx_8 * )&send, sizeof( sfx_32 ) );
	}

	return SFX_RLT_OK;
}

sfx_inline sfx_32 sfx_setSocketMode( sfx_socket skt, sfx_bool nonBlock )
{
	sfx_32 flags = 0;
	
#ifdef SFX_WIN
	flags = nonBlock ? 1 : 0;
	ioctlsocket( skt, FIONBIO, ( sfx_ulong * )&flags );
#endif
#ifdef SFX_LINUX
	flags = fcntl( skt, F_GETFL, 0 );
	if ( nonBlock )
	{
		flags |= O_NONBLOCK;
	}
	else
	{
		flags &= ~O_NONBLOCK;
	}
	fcntl( skt, F_SETFL, flags );
#endif
	
	return 0;
}


typedef enum SfxFileMode
{
	SFX_FILE_MODE_UNKNOW = 0x0,
	SFX_FILE_MODE_R_EXISTING = 0x01,
	SFX_FILE_MODE_WR_EXISTING = 0x02,       
	SFX_FILE_MODE_WR_AUTO_CREATE = 0x03,    // append EOF
	SFX_FILE_MODE_WR_ALWAYS_CREATE = 0x04   // clear old data
}SfxFileMode;

typedef enum SfxFileSeekMode
{
	SFX_FILE_SEEK_MODE_HOF = 0x0,
	SFX_FILE_SEEK_MODE_POS = 0x01,
	SFX_FILE_SEEK_MODE_EOF = 0x02
}SfxFileSeekMode;
/* SEEK_SET FILE_BEGIN */
#define SFX_USE_STD_FILE_IO
//#define SFX_USE_WIN_FILE_API

sfx_32 sfx_mkdir( const sfx_8 * dir );
sfx_32 sfx_rmdir( const sfx_8 * dir );
sfx_bool sfx_exist( const sfx_8 * dir );
sfx_bool sfx_fexist( const sfx_8 * file );
sfx_bool sfx_fdel( const sfx_8 * file );
sfx_bool sfx_frename( const sfx_8 * src, const sfx_8 * dst );
sfx_bool sfx_fcopy( const sfx_8 * src, const sfx_8 * dst, sfx_bool existError );
sfx_32 sfx_upstd( sfx_8 * path );
sfx_32 sfx_getapp( sfx_8 * app, sfx_u32 size );
sfx_32 sfx_getpath( sfx_8 * path, sfx_u32 size );

sfx_32 sfx_dbg_write( const sfx_8 * file, sfx_u8 * data, sfx_32 len, sfx_32 maxSize );

#define SFX_PATH_TAIL_STD(path) {*(sfx_8 * )( path+strlen(path)+1)=0x00;if('/'!=path[strlen(path)-1]){*(sfx_8 * )(path+strlen(path))='/';}}

#ifdef SFX_WIN
#define sfx_frm( f ) DeleteFileA( f )
#define sfx_fmv( src, dst ) MoveFileA( src, dst )
#define sfx_fseek( f, low, high, mode ) _fseeki64( f, ( ( sfx_u64 )high << 32 ) | low, mode )
#endif
#ifdef SFX_LINUX
#define sfx_frm( f ) remove( f )
#define sfx_fmv( src, dst ) rename( src, dst )
#define sfx_fseek( f, low, high, mode ) fseeko64( f, ( ( sfx_u64 )high << 32 ) | low, mode )
//#define sfx_fseek( f, low, high, mode ) fseek( f, low, mode )
#endif

#ifdef SFX_USE_STD_FILE_IO
typedef FILE * sfx_file;
sfx_inline sfx_file sfx_fopen( const sfx_8 * path, SfxFileMode mode )
{
	sfx_file f = sfx_null;
	sfx_8 * p = sfx_null;

	if ( sfx_null == path )
	{
		return sfx_invalid_file;
	}
	
	switch ( mode )
	{
		case SFX_FILE_MODE_R_EXISTING:
			p = "rb";
			break;

		case SFX_FILE_MODE_WR_EXISTING:
			p = "rb+";
			break;

		case SFX_FILE_MODE_WR_AUTO_CREATE:
			p = "ab+";
			break;

		case SFX_FILE_MODE_WR_ALWAYS_CREATE:
			p = "wb+";
			break;

		default:
			return sfx_null;
	}
#ifdef SFX_WIN
	fopen_s( &f, path, p );
	//f = fopen( path, p );
#endif

#ifdef SFX_LINUX
	f = fopen( path, p );
#endif
	
	return f;
}

#define sfx_fread( f, buf, len )  fread( buf, 1, len, f )
#define sfx_fwrite( f, buf, len ) fwrite( buf, 1, len, f )
#define sfx_fflush( f ) fflush( f )
#define sfx_ftell( f ) ftell( f )
sfx_inline sfx_u64 sfx_fsize( sfx_file f )
{
	sfx_u32 pos = 0;
	sfx_u32 size = 0;

	pos = sfx_ftell( f );
	sfx_fseek( f, 0, 0, SFX_FILE_SEEK_MODE_EOF );
	size = sfx_ftell( f );
	sfx_fseek( f, pos, 0, SFX_FILE_SEEK_MODE_HOF ); 

	return size;
}
#define sfx_fclose( f ) fclose( f )
#endif

#ifdef SFX_LINUX
#undef SFX_USE_WIN_FILE_API
#endif

#ifdef SFX_USE_WIN_FILE_API
typedef HANDLE sfx_file;

sfx_inline sfx_file sfx_fopen( const sfx_8 * path, SfxFileMode mode )
{
	sfx_u32 c = 0;
	sfx_u32 rw = 0;
	sfx_file f = sfx_null;
	
	switch ( mode )
	{
		case SFX_FILE_MODE_R_EXISTING:
			c = OPEN_EXISTING;
			rw = GENERIC_READ;
			break;

		case SFX_FILE_MODE_WR_EXISTING:
			c = OPEN_EXISTING;
			rw = GENERIC_READ | GENERIC_WRITE;
			break;

		case SFX_FILE_MODE_WR_AUTO_CREATE:
			c = OPEN_ALWAYS;
			rw = GENERIC_READ | GENERIC_WRITE;
			break;

		case SFX_FILE_MODE_WR_ALWAYS_CREATE:
			c = CREATE_ALWAYS;
			rw = GENERIC_READ | GENERIC_WRITE;
			break;

		default:
			return sfx_null;
	}

	f = CreateFileA( 
					path, rw, 
					SFX_FILE_MODE_R_EXISTING == mode 
					? FILE_SHARE_READ | FILE_SHARE_WRITE : FILE_SHARE_READ, 
					sfx_null, c, 0, sfx_null ) ;
	if ( INVALID_HANDLE_VALUE == f )
	{
		return sfx_null;
	}

	return f;
}

sfx_inline sfx_u32 sfx_fread( sfx_file f, sfx_u8 * buf, sfx_u32 len )
{
	sfx_u32 ret = 0;

	if ( ReadFile( f, buf, len, ( LPDWORD )&ret, sfx_null ) )
	{
		return ret;
	}

	return 0;
}


sfx_inline sfx_32 sfx_fwrite( sfx_file f, const sfx_u8 * buf, sfx_u32 len )
{
	sfx_u32 ret = 0;

	if ( WriteFile( f, buf, len, ( LPDWORD )&ret, sfx_null ) )
	{
		return ret;
	}

	return ret;
}

sfx_inline sfx_32 sfx_fseek( sfx_file f, sfx_u32 low, sfx_u32 high, SfxFileSeekMode mode )
{
	int m = 0;

	if ( SFX_FILE_SEEK_MODE_HOF == mode )
	{
		m = FILE_BEGIN;
	}
	else if ( SFX_FILE_SEEK_MODE_EOF == mode )
	{
		m = FILE_END;
	}
	else
	{
		m = FILE_CURRENT;
	}
	
	return SetFilePointer( f, low, ( PLONG )&high, m );
}


sfx_inline sfx_u64 sfx_ftell( sfx_file f )
{
	LONG high = 0;
	sfx_u32 low = 0;
	sfx_u64 count = 0;
	
	low = SetFilePointer( f, 0, ( PLONG )&high, FILE_CURRENT );
	count = ( sfx_u32 )high;
	count = count << 32;

	return count | low;
}


sfx_inline sfx_u64 sfx_fsize( sfx_file f )
{
	sfx_u64 old = 0;
	sfx_u64 size = 0;

	old = sfx_ftell( f );
	sfx_fseek( f, 0, 0, SFX_FILE_SEEK_MODE_EOF );
	size = sfx_ftell( f );
	sfx_fseek( f, ( sfx_u32 )old, old >> 32, SFX_FILE_SEEK_MODE_HOF );

	return size;
}
#define sfx_fflush( f ) 
#define sfx_fclose( f ) CloseHandle( f ) 
#endif

sfx_32 sfxGetCPUBrand( sfx_8 * brand, sfx_u32 size );
// unit: MB
sfx_32 sfxGetMemoryStatus( sfx_u32 * total, sfx_u32 * used );
// idle rate: ( idle_new - idle_old ) / ( busy_new - busy_old )
sfx_32 sfxGetCPUTime( sfx_u64 * idle, sfx_u64 * busy );
sfx_u64 sfxGetProcessTime( sfx_u32 pid );

typedef sfx_void sfx_http_parser;
typedef sfx_void sfx_http_packet;

#ifdef __cplusplus
}
#endif
#endif