-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_java_io_Net.cpp
58 lines (41 loc) · 1.27 KB
/
io_java_io_Net.cpp
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
//
// io_java_io_Net.cpp
// io.java
//
// Created by kenvi.zhu on 16/5/16.
//
//
#include <jni.h>
#include <stdio.h>
#include "io_java_io_Net.h"
#include <fcntl.h>
#include "uv.h"
#include "type.h"
#include <stdlib.h>
#include "Env.h"
#include <assert.h>
using namespace io_java;
void on_connect(uv_connect_t* req, int status);
JNIEXPORT void JNICALL Java_io_java_io_Net_connect
(JNIEnv * env, jclass clazz, jstring addr, jint port, jobject callback) {
printf("connect");
struct callbak_args* callbackArg = getCallbackArgs(env, callback);
uv_tcp_t* socket = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
uv_tcp_init(uv_default_loop(), socket);
uv_connect_t* connect = (uv_connect_t*)malloc(sizeof(uv_connect_t));
struct sockaddr_in dest;
const char* addrStr = env->GetStringUTFChars(addr, NULL);
uv_ip4_addr(addrStr, port, &dest);
printf("before connect");
uv_tcp_connect(connect, socket, (const struct sockaddr*)&dest, on_connect);
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
printf("after connet");
}
void on_connect(uv_connect_t* req, int status) {
printf("on_connect");
if (status < 0) {
printf("connect error: %s", uv_err_name(status));
}else {
printf("connect success");
}
}