Zero-copy FTP/HTTP Daemon compatible with all POSIX systems
| 1 | #include "pal_ffi.h" |
| 2 | #include <jni.h> |
| 3 | |
| 4 | #ifdef __cplusplus |
| 5 | extern "C" { |
| 6 | #endif |
| 7 | |
| 8 | /* |
| 9 | * Class: org_zftpd_ffi_PalAlloc |
| 10 | * Method: initDefault |
| 11 | * Signature: ()I |
| 12 | */ |
| 13 | JNIEXPORT jint JNICALL Java_org_zftpd_ffi_PalAlloc_initDefault(JNIEnv *env, |
| 14 | jclass clazz) { |
| 15 | (void)env; |
| 16 | (void)clazz; |
| 17 | return (jint)pal_ffi_alloc_init_default(); |
| 18 | } |
| 19 | |
| 20 | /* |
| 21 | * Class: org_zftpd_ffi_PalAlloc |
| 22 | * Method: malloc |
| 23 | * Signature: (J)J |
| 24 | */ |
| 25 | JNIEXPORT jlong JNICALL Java_org_zftpd_ffi_PalAlloc_malloc(JNIEnv *env, |
| 26 | jclass clazz, |
| 27 | jlong size) { |
| 28 | (void)env; |
| 29 | (void)clazz; |
| 30 | return (jlong)(intptr_t)pal_ffi_malloc((size_t)size); |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | * Class: org_zftpd_ffi_PalAlloc |
| 35 | * Method: free |
| 36 | * Signature: (J)V |
| 37 | */ |
| 38 | JNIEXPORT void JNICALL Java_org_zftpd_ffi_PalAlloc_free(JNIEnv *env, |
| 39 | jclass clazz, |
| 40 | jlong ptr) { |
| 41 | (void)env; |
| 42 | (void)clazz; |
| 43 | if (ptr != 0) { |
| 44 | pal_ffi_free((void *)(intptr_t)ptr); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | * Class: org_zftpd_ffi_PalAlloc |
| 50 | * Method: calloc |
| 51 | * Signature: (JJ)J |
| 52 | */ |
| 53 | JNIEXPORT jlong JNICALL Java_org_zftpd_ffi_PalAlloc_calloc(JNIEnv *env, |
| 54 | jclass clazz, |
| 55 | jlong nmemb, |
| 56 | jlong size) { |
| 57 | (void)env; |
| 58 | (void)clazz; |
| 59 | return (jlong)(intptr_t)pal_ffi_calloc((size_t)nmemb, (size_t)size); |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * Class: org_zftpd_ffi_PalAlloc |
| 64 | * Method: realloc |
| 65 | * Signature: (JJ)J |
| 66 | */ |
| 67 | JNIEXPORT jlong JNICALL Java_org_zftpd_ffi_PalAlloc_realloc(JNIEnv *env, |
| 68 | jclass clazz, |
| 69 | jlong ptr, |
| 70 | jlong size) { |
| 71 | (void)env; |
| 72 | (void)clazz; |
| 73 | return (jlong)(intptr_t)pal_ffi_realloc((void *)(intptr_t)ptr, (size_t)size); |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Class: org_zftpd_ffi_PalAlloc |
| 78 | * Method: alignedAlloc |
| 79 | * Signature: (JJ)J |
| 80 | */ |
| 81 | JNIEXPORT jlong JNICALL Java_org_zftpd_ffi_PalAlloc_alignedAlloc( |
| 82 | JNIEnv *env, jclass clazz, jlong alignment, jlong size) { |
| 83 | (void)env; |
| 84 | (void)clazz; |
| 85 | return (jlong)(intptr_t)pal_ffi_aligned_alloc((size_t)alignment, |
| 86 | (size_t)size); |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Class: org_zftpd_ffi_PalAlloc |
| 91 | * Method: getArenaFreeApprox |
| 92 | * Signature: ()J |
| 93 | */ |
| 94 | JNIEXPORT jlong JNICALL |
| 95 | Java_org_zftpd_ffi_PalAlloc_getArenaFreeApprox(JNIEnv *env, jclass clazz) { |
| 96 | (void)env; |
| 97 | (void)clazz; |
| 98 | return (jlong)pal_ffi_alloc_arena_free_approx(); |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Class: org_zftpd_ffi_EventLoop |
| 103 | * Method: create |
| 104 | * Signature: ()J |
| 105 | */ |
| 106 | JNIEXPORT jlong JNICALL Java_org_zftpd_ffi_EventLoop_create(JNIEnv *env, |
| 107 | jclass clazz) { |
| 108 | (void)env; |
| 109 | (void)clazz; |
| 110 | return (jlong)(intptr_t)pal_ffi_event_loop_create(); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Class: org_zftpd_ffi_EventLoop |
| 115 | * Method: runLoop |
| 116 | * Signature: (J)I |
| 117 | */ |
| 118 | JNIEXPORT jint JNICALL Java_org_zftpd_ffi_EventLoop_runLoop(JNIEnv *env, |
| 119 | jclass clazz, |
| 120 | jlong loopHandle) { |
| 121 | (void)env; |
| 122 | (void)clazz; |
| 123 | return (jint)pal_ffi_event_loop_run((void *)(intptr_t)loopHandle); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Class: org_zftpd_ffi_EventLoop |
| 128 | * Method: stopLoop |
| 129 | * Signature: (J)V |
| 130 | */ |
| 131 | JNIEXPORT void JNICALL Java_org_zftpd_ffi_EventLoop_stopLoop(JNIEnv *env, |
| 132 | jclass clazz, |
| 133 | jlong loopHandle) { |
| 134 | (void)env; |
| 135 | (void)clazz; |
| 136 | if (loopHandle != 0) { |
| 137 | pal_ffi_event_loop_stop((void *)(intptr_t)loopHandle); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Class: org_zftpd_ffi_EventLoop |
| 143 | * Method: destroyLoop |
| 144 | * Signature: (J)V |
| 145 | */ |
| 146 | JNIEXPORT void JNICALL Java_org_zftpd_ffi_EventLoop_destroyLoop( |
| 147 | JNIEnv *env, jclass clazz, jlong loopHandle) { |
| 148 | (void)env; |
| 149 | (void)clazz; |
| 150 | if (loopHandle != 0) { |
| 151 | pal_ffi_event_loop_destroy((void *)(intptr_t)loopHandle); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Class: org_zftpd_ffi_FtpServer |
| 157 | * Method: create |
| 158 | * Signature: (Ljava/lang/String;ILjava/lang/String;)J |
| 159 | */ |
| 160 | JNIEXPORT jlong JNICALL Java_org_zftpd_ffi_FtpServer_create( |
| 161 | JNIEnv *env, jclass clazz, jstring bindIp, jint port, jstring rootPath) { |
| 162 | (void)clazz; |
| 163 | const char *c_bindIp = (*env)->GetStringUTFChars(env, bindIp, 0); |
| 164 | const char *c_rootPath = (*env)->GetStringUTFChars(env, rootPath, 0); |
| 165 | |
| 166 | void *server = |
| 167 | pal_ffi_ftp_server_create(c_bindIp, (uint16_t)port, c_rootPath); |
| 168 | |
| 169 | (*env)->ReleaseStringUTFChars(env, bindIp, c_bindIp); |
| 170 | (*env)->ReleaseStringUTFChars(env, rootPath, c_rootPath); |
| 171 | |
| 172 | return (jlong)(intptr_t)server; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Class: org_zftpd_ffi_FtpServer |
| 177 | * Method: startServer |
| 178 | * Signature: (J)I |
| 179 | */ |
| 180 | JNIEXPORT jint JNICALL Java_org_zftpd_ffi_FtpServer_startServer( |
| 181 | JNIEnv *env, jclass clazz, jlong serverHandle) { |
| 182 | (void)env; |
| 183 | (void)clazz; |
| 184 | return (jint)pal_ffi_ftp_server_start((void *)(intptr_t)serverHandle); |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Class: org_zftpd_ffi_FtpServer |
| 189 | * Method: isServerRunning |
| 190 | * Signature: (J)I |
| 191 | */ |
| 192 | JNIEXPORT jint JNICALL Java_org_zftpd_ffi_FtpServer_isServerRunning( |
| 193 | JNIEnv *env, jclass clazz, jlong serverHandle) { |
| 194 | (void)env; |
| 195 | (void)clazz; |
| 196 | return (jint)pal_ffi_ftp_server_is_running( |
| 197 | (const void *)(intptr_t)serverHandle); |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Class: org_zftpd_ffi_FtpServer |
| 202 | * Method: getSessions |
| 203 | * Signature: (J)J |
| 204 | */ |
| 205 | JNIEXPORT jlong JNICALL Java_org_zftpd_ffi_FtpServer_getSessions( |
| 206 | JNIEnv *env, jclass clazz, jlong serverHandle) { |
| 207 | (void)env; |
| 208 | (void)clazz; |
| 209 | return (jlong)pal_ffi_ftp_server_get_active_sessions( |
| 210 | (const void *)(intptr_t)serverHandle); |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Class: org_zftpd_ffi_FtpServer |
| 215 | * Method: stopServer |
| 216 | * Signature: (J)V |
| 217 | */ |
| 218 | JNIEXPORT void JNICALL Java_org_zftpd_ffi_FtpServer_stopServer( |
| 219 | JNIEnv *env, jclass clazz, jlong serverHandle) { |
| 220 | (void)env; |
| 221 | (void)clazz; |
| 222 | if (serverHandle != 0) { |
| 223 | pal_ffi_ftp_server_stop((void *)(intptr_t)serverHandle); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Class: org_zftpd_ffi_FtpServer |
| 229 | * Method: destroyServer |
| 230 | * Signature: (J)V |
| 231 | */ |
| 232 | JNIEXPORT void JNICALL Java_org_zftpd_ffi_FtpServer_destroyServer( |
| 233 | JNIEnv *env, jclass clazz, jlong serverHandle) { |
| 234 | (void)env; |
| 235 | (void)clazz; |
| 236 | if (serverHandle != 0) { |
| 237 | pal_ffi_ftp_server_destroy((void *)(intptr_t)serverHandle); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Class: org_zftpd_ffi_HttpServer |
| 243 | * Method: create |
| 244 | * Signature: (JI)J |
| 245 | */ |
| 246 | JNIEXPORT jlong JNICALL Java_org_zftpd_ffi_HttpServer_create(JNIEnv *env, |
| 247 | jclass clazz, |
| 248 | jlong loopHandle, |
| 249 | jint port) { |
| 250 | (void)env; |
| 251 | (void)clazz; |
| 252 | return (jlong)(intptr_t)pal_ffi_http_server_create( |
| 253 | (void *)(intptr_t)loopHandle, (uint16_t)port); |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Class: org_zftpd_ffi_HttpServer |
| 258 | * Method: destroyServer |
| 259 | * Signature: (J)V |
| 260 | */ |
| 261 | JNIEXPORT void JNICALL Java_org_zftpd_ffi_HttpServer_destroyServer( |
| 262 | JNIEnv *env, jclass clazz, jlong serverHandle) { |
| 263 | (void)env; |
| 264 | (void)clazz; |
| 265 | if (serverHandle != 0) { |
| 266 | pal_ffi_http_server_destroy((void *)(intptr_t)serverHandle); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | #ifdef __cplusplus |
| 271 | } |
| 272 | #endif |
| 273 |