aboutsummaryrefslogtreecommitdiff
path: root/iemnet_receiver.c
blob: 5fb41bdd7c85c95bbb3b402a544a647f9ef9a7f1 (plain)
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
/* iemnet
 *
 * receiver
 *   receives data "chunks" from a socket
 *
 *  copyright © 2010-2015 IOhannes m zmölnig, IEM
 */

/* This program is free software; you can redistribute it and/or                */
/* modify it under the terms of the GNU General Public License                  */
/* as published by the Free Software Foundation; either version 2               */
/* of the License, or (at your option) any later version.                       */
/*                                                                              */
/* This program 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 General Public License for more details.                                 */
/*                                                                              */
/* You should have received a copy of the GNU General Public License            */
/* along with this program; if not, see                                         */
/*     http://www.gnu.org/licenses/                                             */
/*                                                                              */

#define DEBUGLEVEL 4

#include "iemnet.h"
#include "iemnet_data.h"

#include <stdlib.h>
#include <errno.h>

#define INBUFSIZE 65536L /* was 4096: size of receiving data buffer */

struct _iemnet_receiver {
  int sockfd; /* owned outside; you must call iemnet__receiver_destroy() before freeing socket yourself */
  void*userdata;
  t_iemnet_receivecallback callback;
};


static void pollfun(void*z, int fd)
{
  // read data from socket and call callback
  t_iemnet_receiver*rec=(t_iemnet_receiver*)z;

  unsigned char data[INBUFSIZE];
  unsigned int size=INBUFSIZE;
  t_iemnet_chunk*chunk=NULL;
  int result = 0;
  int local_errno = 0;

  struct sockaddr_in  from;
  socklen_t           fromlen = sizeof(from);

  int recv_flags=0;
#ifdef MSG_DONTWAIT
  recv_flags|=MSG_DONTWAIT;
#endif
  errno=0;
  result = recvfrom(rec->sockfd, data, size, recv_flags,
                    (struct sockaddr *)&from, &fromlen);
  local_errno=errno;
  //fprintf(stderr, "read %d bytes...\n", result);
  DEBUG("recvfrom %d bytes: %d %p %d", result, rec->sockfd, data, size);
  DEBUG("errno=%d", local_errno);
  chunk = iemnet__chunk_create_dataaddr(result, (result>0)?data:NULL, &from);

  // call the callback with a NULL-chunk to signal a disconnect event.
  (rec->callback)(rec->userdata, chunk);

  iemnet__chunk_destroy(chunk);
}

t_iemnet_receiver*iemnet__receiver_create(int sock, void*userdata,
    t_iemnet_receivecallback callback, int subthread)
{
  t_iemnet_receiver*rec=(t_iemnet_receiver*)malloc(sizeof(
                          t_iemnet_receiver));

  DEBUG("create new receiver for 0x%X:%d", userdata, sock);
  //fprintf(stderr, "new receiver for %d\t%x\t%x\n", sock, userdata, callback);
  if(rec) {
    rec->sockfd=sock;
    rec->userdata=userdata;
    rec->callback=callback;

    if(subthread) {
      sys_lock();
    }
    sys_addpollfn(sock, pollfun, rec);
    if(subthread) {
      sys_unlock();
    }

  }
  //fprintf(stderr, "new receiver created\n");

  return rec;
}
void iemnet__receiver_destroy(t_iemnet_receiver*rec, int subthread)
{
  int sockfd;
  if(NULL==rec) {
    return;
  }

  sockfd=rec->sockfd;

  if(subthread) {
    sys_lock();
  }
  sys_rmpollfn(rec->sockfd);

  // FIXXME: read any remaining bytes from the socket

  if(subthread) {
    sys_unlock();
  }

  DEBUG("[%p] really destroying receiver %d", sockfd);
  DEBUG("[%p] closed socket %d", rec, sockfd);

  rec->sockfd=-1;
  rec->userdata=NULL;
  rec->callback=NULL;

  free(rec);
  rec=NULL;
}


/* just dummy, since we don't maintain a queue any more */
int iemnet__receiver_getsize(t_iemnet_receiver*x)
{
  if(x) {
    return 0;
  }
  return -1;
}