-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipmsg_filerecv.c
More file actions
173 lines (144 loc) · 3.63 KB
/
ipmsg_filerecv.c
File metadata and controls
173 lines (144 loc) · 3.63 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
/*
* Copyright (c) 2018, Real-Thread Information Technology Ltd
* All rights reserved
*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-04-28 heyuanjie87 the first version
*/
#include <rtthread.h>
#include "ipmsg.h"
#include "ipmsgdef.h"
#include <stdio.h>
#include <sys/socket.h>
#include "netdb.h"
#define DBG_TAG "ipmsgfr"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#ifndef IPMSG_FILERECV_BUFSZ
#define IPMSG_FILERECV_BUFSZ 1024
#endif
#ifdef IPMSG_FILERECV_ENABLE
static int ipmsg_getdata_req(int sk, ipmsg_filehandler_t *fh, ipmsg_fileinfo_t *fi, char *buf, int bufsz)
{
char *msg;
char ext[24];
int len;
sprintf(ext, "%x:%x:0", fh->packetid, fi->id);
msg = ipmsg_msg_make(fh->im, IPMSG_GETFILEDATA, ext, NULL, buf, &bufsz);
len = send(sk, msg, bufsz, 0);
return (len == bufsz);
}
static int ipmsg_data_recv(int sk, ipmsg_filehandler_t *fh, ipmsg_fileinfo_t *fi, char *buf, int bufsz)
{
int len;
int ret = -1;
if (ipmsg_sock_wait(sk, 2000) <= 0)
return 0;
len = recv(sk, buf, bufsz, 0);
if (len > 0)
{
ret = fh->data(fh, buf, len);
if (ret <= 0)
ret = 0;
else if (ret > 0)
fi->pos += ret;
}
if (fi->size == fi->pos)
{
fh->notify(fh, IPMSG_FE_COMPLETE, fi);
ret = 0;
}
return (ret == len);
}
static int sock_init(ipmsg_filehandler_t *fh)
{
struct sockaddr_in server_addr = {0};
int sock;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
LOG_E("Create socket error");
return -1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(fh->im->port);
server_addr.sin_addr.s_addr = fh->ip;
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
LOG_E("Connect fail!");
closesocket(sock);
return -1;
}
return sock;
}
static void msg_server(void *p)
{
int sock;
char *buf;
ipmsg_filehandler_t *fh = p;
int stage = 0;
fh->notify(fh, IPMSG_FE_ENTER, NULL);
if ((sock = sock_init(fh)) < 0)
goto __exit;
if ((buf = rt_malloc(IPMSG_FILERECV_BUFSZ)) == NULL)
goto _out;
while (1)
{
switch (stage)
{
case 0:
{
if (fh->notify(fh, IPMSG_FE_OPEN, fh->fi) != 0)
goto _out;
if (ipmsg_getdata_req(sock, fh, fh->fi, buf, IPMSG_FILERECV_BUFSZ))
stage++;
else
{
//error
stage = 2;
}
}
break;
case 1:
{
if (!ipmsg_data_recv(sock, fh, fh->fi, buf, IPMSG_FILERECV_BUFSZ))
{
stage++;
}
}
break;
case 2:
{
fh->notify(fh, IPMSG_FE_CLOSE, fh->fi);
}
goto _out;
}
}
_out:
closesocket(sock);
rt_free(buf);
__exit:
fh->notify(fh, IPMSG_FE_EXIT, NULL);
ipmsg_filehandler_free(fh);
}
int ipmsg_filerecv_start(ipmsg_filehandler_t *h)
{
rt_thread_t tid;
int ret = -1;
tid = rt_thread_create("ipmsg-r",
msg_server,
h,
2048,
22,
20);
if (tid)
{
ret = rt_thread_startup(tid);
}
return ret;
}
#endif