/
output-chunkstream.c
175 lines (160 loc) · 4.62 KB
/
output-chunkstream.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
/*
* Copyright (c) 2010-2011 Csaba Kiraly
*
* This file is part of PeerStreamer.
*
* PeerStreamer is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* PeerStreamer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with PeerStreamer. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#ifndef _WIN32
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#else
#include <winsock2.h>
#endif
#include <chunk.h>
#include <chunkiser.h>
#include <trade_msg_la.h>
#include "measures.h"
#include "dbg.h"
#define BUFSIZE 65536*8
static int fd = -1;
static char *fname = NULL;
static enum MODE {FILE_MODE, TCP_MODE} mode;
#ifdef _WIN32
static int inet_aton(const char *cp, struct in_addr *addr)
{
if( cp==NULL || addr==NULL )
{
return(0);
}
addr->s_addr = inet_addr(cp);
return (addr->s_addr == INADDR_NONE) ? 0 : 1;
}
#endif
static void output_connect(void)
{
if (!fname){
mode = FILE_MODE;
fd = STDOUT_FILENO;
} else {
char *c;
int port;
char ip[32];
c = strchr(fname,',');
if (c) {
*(c++) = 0;
}
if (sscanf(fname,"tcp://%[0-9.]:%d", ip, &port) == 2) {
mode = TCP_MODE;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
fprintf(stderr,"output-chunkstream: Error creating socket\n");
} else {
struct sockaddr_in servaddr;
fprintf(stderr,"output-chunkstream: tcp socket opened fd=%d\n", fd);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
if (inet_aton(ip, &servaddr.sin_addr) < 0) {
fprintf(stderr,"output-chunkstream: Error converting IP address: %s\n", ip);
return;
}
if (connect(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
fprintf(stderr,"output-chunkstream: Error connecting to %s:%d\n", ip, port);
} else {
fprintf(stderr,"output-chunkstream: Connected to %s:%d\n", ip, port);
}
}
} else {
mode = FILE_MODE;
#ifndef _WIN32
fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC | O_NONBLOCK, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
#else
fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC);
if (fd >= 0) {
unsigned long nonblocking = 1;
ioctlsocket(fd, FIONBIO, (unsigned long*) &nonblocking);
}
#endif
if (fd < 0) {
fprintf(stderr,"output-chunkstream: Error opening output file %s", fname);
perror(NULL);
} else {
fprintf(stderr,"output-chunkstream: opened output file %s\n", fname);
}
}
}
}
struct output_stream *out_stream_init(const char *fn, const char *config)
{
if (fn) fname = strdup(fn);
output_connect();
return 1; //TODO: create real output_stream structure
}
void chunk_write(struct output_stream *out, const struct chunk *c)
{
static char sendbuf[BUFSIZE];
static int pos = 0;
int ret;
uint32_t size;
size = encodeChunk(c, sendbuf + pos + sizeof(size), BUFSIZE - pos);
if (size <= 0) {
fprintf(stderr,"output-chunkstream: Error encoding chunk or no space in output buffer, skipping\n");
} else {
*((uint32_t*)(sendbuf + pos)) = htonl(size);
pos += sizeof(size) + size;
}
if (mode == TCP_MODE && fd < 0) {
fprintf(stderr,"output-chunkstream: reconnecting ...\n");
output_connect();
}
if (fd < 0) {
fprintf(stderr,"output-chunkstream: not conected\n");
return;
}
if (mode == TCP_MODE) { //distiction needed by Win32
ret = send(fd, sendbuf, pos, 0);
} else {
ret = write(fd, sendbuf, pos);
}
if (ret < 0) {
#ifndef _WIN32
if (ret == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
#else
if (ret == -1 && WSAGetLastError() == WSAEWOULDBLOCK) {
#endif
fprintf(stderr,"output-chunkstream: Output stalled ...\n");
} else {
perror("output-chunkstream: Error writing to output");
close(fd);
pos = 0;
fd = -1;
}
} else {
dprintf("output-chunkstream: written %d bytes\n", ret);
pos -= ret;
memmove(sendbuf, sendbuf + ret, pos);
}
}