18
18
#include <stdlib.h>
19
19
#include <sys/wait.h>
20
20
#include <dirent.h>
21
+ #include <zlib.h>
21
22
22
23
#define LISTEN_PORT 7345
23
24
#define THERMAL_DIR "/mnt/DCIM/102SIYI_TEM"
24
- #define EXPECTED_SIZE 655360
25
+ //#define THERMAL_DIR "/mnt/Photo/B"
26
+ //#define THERMAL_DIR "test_images"
25
27
26
- struct header {
28
+ #define EXPECTED_SIZE (640 * 512 * 2)
29
+
30
+ #define FORK_PER_CONNECTION 1
31
+
32
+ #define PACKED __attribute__((__packed__))
33
+
34
+ struct PACKED header {
27
35
char fname [128 ];
36
+ uint32_t compressed_size ;
28
37
double timestamp ;
29
38
};
30
39
31
- static int open_socket_in (int type , int port )
40
+ static int open_socket_in (int port )
32
41
{
33
42
struct sockaddr_in sock ;
34
43
int res ;
@@ -99,6 +108,46 @@ char *find_latest_file(int fd, struct stat *st_latest)
99
108
return newest_fname ;
100
109
}
101
110
111
+ // Function to handle errors
112
+ static void zlib_error (const char * msg ) {
113
+ perror (msg );
114
+ exit (1 );
115
+ }
116
+
117
+ /*
118
+ compress a buffer, returning the compressed size
119
+ exit via zlib_error() on any error
120
+ */
121
+ static uint32_t compress_buffer (uint8_t * in_buffer , uint32_t in_size , uint8_t * out_buffer , uint32_t out_buf_size , uint8_t compression_level )
122
+ {
123
+ z_stream strm ;
124
+ memset (& strm , 0 , sizeof (strm )); // Initialize the z_stream structure
125
+
126
+ // Initialize the zlib stream for compression
127
+ if (deflateInit (& strm , compression_level ) != Z_OK ) {
128
+ zlib_error ("deflateInit failed" );
129
+ }
130
+
131
+ // Set up the zlib stream for compression
132
+ strm .avail_in = in_size ;
133
+ strm .next_in = in_buffer ;
134
+
135
+ strm .avail_out = out_buf_size ;
136
+ strm .next_out = out_buffer ;
137
+
138
+ // Compress with zlib
139
+ int ret = deflate (& strm , Z_FINISH );
140
+ if (ret == Z_STREAM_ERROR ) {
141
+ deflateEnd (& strm );
142
+ zlib_error ("deflate failed" );
143
+ }
144
+
145
+ // Clean up the zlib stream
146
+ deflateEnd (& strm );
147
+
148
+ // Calculate how many bytes were compressed into out_buffer
149
+ return out_buf_size - strm .avail_out ;
150
+ }
102
151
103
152
104
153
static void serve_connection (int fd )
@@ -129,25 +178,31 @@ static void serve_connection(int fd)
129
178
if (read (dfd , buf , sizeof (buf )) != sizeof (buf )) {
130
179
return ;
131
180
}
181
+ uint8_t compressed_buf [EXPECTED_SIZE * 2 ];
182
+ h .compressed_size = compress_buffer (buf , sizeof (buf ), compressed_buf , sizeof (compressed_buf ), 1 );
183
+ if (h .compressed_size == 0 ) {
184
+ printf ("compression failed %s\n" , h .fname );
185
+ exit (1 );
186
+ }
132
187
133
188
write (fd , & h , sizeof (h ));
134
- write (fd , buf , sizeof ( buf ) );
189
+ write (fd , compressed_buf , h . compressed_size );
135
190
136
- printf ("Sent %s\n" , h .fname );
191
+ printf ("Sent %s compression %.1f%% compressed_size=%u \n" , h .fname , ( 100.0 * h . compressed_size ) / sizeof ( buf ), ( unsigned ) h . compressed_size );
137
192
}
138
193
139
194
static void listener (void )
140
195
{
141
196
int sock ;
142
197
143
- sock = open_socket_in (SOCK_STREAM , LISTEN_PORT );
198
+ sock = open_socket_in (LISTEN_PORT );
144
199
145
- if (listen (sock , 20 ) == -1 ) {
200
+ if (listen (sock , 200 ) == -1 ) {
146
201
fprintf (stderr ,"listen failed\n" );
147
202
exit (1 );
148
203
}
149
204
150
- printf ("waiting for connections\n" );
205
+ printf ("waiting for connections on port %u \n" , ( unsigned ) LISTEN_PORT );
151
206
152
207
while (1 ) {
153
208
struct sockaddr addr ;
@@ -159,16 +214,20 @@ static void listener(void)
159
214
fd = accept (sock , & addr , & in_addrlen );
160
215
161
216
if (fd != -1 ) {
217
+ #if FORK_PER_CONNECTION
162
218
if (fork () == 0 ) {
163
219
serve_connection (fd );
164
220
exit (0 );
165
221
}
222
+ #else
223
+ serve_connection (fd );
224
+ #endif
166
225
close (fd );
167
226
}
168
227
}
169
228
}
170
229
171
- int main (int argc , const char * argv [] )
230
+ int main (void )
172
231
{
173
232
listener ();
174
233
return 0 ;
0 commit comments