-
Notifications
You must be signed in to change notification settings - Fork 56
/
BaseAPI.c
219 lines (196 loc) · 5.69 KB
/
BaseAPI.c
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
#include "BaseAPI.h"
int usec_for_EW = 100000;
int API_env_init(){
#ifdef WIN32
// 初始化套接字库 win32 下需要
WSADATA wsd;
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
perror ("WSAStartup failed!");
return 1;
}
#else
signal(SIGPIPE, SIG_IGN);
#endif
return ENV_INIT_OK;
}
#ifdef WIN32
struct hostent *API_socket_gethostbyname(char * ser_addr){
return gethostbyname(ser_addr);
}
#else
struct in_addr *API_socket_getaddrinfo(char *url){
struct addrinfo *result;
int error = getaddrinfo(url,NULL,NULL,&result);
return &(((struct sockaddr_in *)(result->ai_addr))->sin_addr);
}
#endif
int API_socket_connect(char *ser_addr,int port){
struct sockaddr_in server_addr,client_addr;
struct hostent * des_addr;
memset(&client_addr, 0 ,sizeof(client_addr));
//bzero(&client_addr,sizeof(client_addr));
client_addr.sin_family = AF_INET; // internet
client_addr.sin_addr.s_addr = htons( INADDR_ANY ); // local address
client_addr.sin_port = htons(0); // any port
int client_socket = socket(AF_INET,SOCK_STREAM,0);
if( client_socket < 0){
printf("Create Socket Failed ! \n");
return SOCKET_CONNECT_ERROR;
}
if( bind(client_socket,(struct sockaddr*)&client_addr,sizeof(client_addr))){
printf("Client Bind Port Failed ! \n");
return SOCKET_CONNECT_ERROR;
}
memset(&server_addr,0,sizeof(server_addr));
//bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
// dns url -> ip
#ifdef WIN32
des_addr = API_socket_gethostbyname(ser_addr);
if ( des_addr == NULL ){
return SOCKET_CONNECT_ERROR;
}
server_addr.sin_addr = *(struct in_addr*)des_addr->h_addr;
#else
server_addr.sin_addr = *(API_socket_getaddrinfo(ser_addr));
#endif
if( server_addr.sin_addr.s_addr == 0 )
{
printf("Server IP Address Error!\n");
return SOCKET_CONNECT_ERROR;
}
server_addr.sin_port = htons(port);
//向服务器发起连接,连接成功后client_socket代表了客户机和服务器的一个socket连接
if(connect(client_socket,(struct sockaddr*)&server_addr, sizeof(server_addr)) < 0)
{
printf("Can Not Connect To %s!\n", ser_addr );
return SOCKET_CONNECT_ERROR;
}
//puts("got the client_socket");
return client_socket;
}
int API_socket_init_server(int port,int maxlisten){
int socket_desc;
struct sockaddr_in server;
int c;
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Error : Could not create socket [ port = %d ].\n",port);
return SOCKET_SERVER_INIT_ERROR;
}
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( port );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
printf("Error : bind port %d .\n",port);
return SOCKET_SERVER_INIT_ERROR;
}
// puts("bind done");
//Listen
listen(socket_desc , maxlisten);
return socket_desc;
}
int API_socket_read_state(int sock,int sec,int usec){
fd_set fds ;
int n = 1;
struct timeval tv ;
// Set up the file descriptor set.
FD_ZERO(&fds) ;
FD_SET(sock, &fds) ;
// Set up the struct timeval for the timeout.
tv.tv_sec = sec ;
tv.tv_usec = usec ;
// Wait until timeout or data received.
n = select ( sock + 1, &fds, NULL, NULL, &tv ) ;
if ( n >0 ){
if ( FD_ISSET(sock,&fds) ) {
return SOCKET_CAN_READ_STATE; // socket 可以读
}
return SOCKET_MAY_CLOSE_NOW;
}
else if ( n == 0 ) return SOCKET_OVER_TIME; // 超时
return SOCKET_INIT_ERROR; // 出错了
}
int API_socket_write_state(int sock,int sec,int usec){
fd_set fds ;
int n = 1;
struct timeval tv ;
// Set up the file descriptor set.
FD_ZERO(&fds) ;
FD_SET(sock, &fds) ;
// Set up the struct timeval for the timeout.
tv.tv_sec = sec ;
tv.tv_usec = usec ;
// Wait until timeout or data received.
n = select ( sock + 1, NULL, &fds, NULL, &tv ) ;
if ( n >0 ){
if ( FD_ISSET(sock,&fds) ) {
return SOCKET_CAN_WRITE_STATE; // socket 可以读
}
return SOCKET_MAY_CLOSE_NOW;
}
else if ( n == 0 ) return SOCKET_OVER_TIME; // 超时
return SOCKET_INIT_ERROR; // 出错了
}
int API_socket_send(int sock,char *buf,int buflen){
return send(sock,buf,buflen,0);
}
int API_socket_recv(int sock,char *buf,int buflen){
return recv(sock,buf,buflen,0);
}
int API_socket_close(int sock){
if (sock>0){
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
}
return 1;
}
//int API_thread_create(pthread_t *thread, const pthread_attr_t *attr,
// void *(*start_routine) (void *), void *arg){
// return pthread_create(thread,attr,start_routine,arg);
//}
//
//int API_thread_detach(){
// return pthread_detach(pthread_self());
//}
int API_m_itochar(int a,char *b,int len){
int i;
if(len!=4) return I_TO_CHAR_ERROR;
int buf = a;
for (i = 0;i< len ;i++){
b[i] = buf % 128;
if(b[i] < 0) return I_TO_CHAR_ERROR;
buf = buf / 128;
}
return I_TO_CHAR_OK;
}
int API_m_chartoi(char *b,int len){
int i;
int result = 0;
if (len!=4)return CHAR_TO_I_ERROR;
for (i = len - 1 ;i >= 0 ; i--){
if (b[i] < 0) return CHAR_TO_I_ERROR;
result = result * 128 + b[i];
}
return result;
}
int API_m_sleep(int usec){
usleep(usec);
return 1;
}
int API_set_usec_time(int usec){
usec_for_EW = usec;
return 1;
}
int API_get_usec_time(){
return usec_for_EW;
}