Revision be033378 net_helper-ml.c

View differences:

net_helper-ml.c
40 40
	int bIdx;	// index of the message in the proper buffer
41 41
	unsigned char msgType; // message type
42 42
	int mSize;	// message size
43
	bool conn_cb_called;
44
	bool cancelled;
43 45
} msgData_cb;
44 46

  
45 47
static nodeID *me; //TODO: is it possible to get rid of this (notwithstanding ml callback)??
......
170 172

  
171 173
}
172 174

  
175
void free_sending_buffer(int i)
176
{
177
	free(sendingBuffer[i]);
178
	sendingBuffer[i] = NULL;
179
}
180

  
173 181
/**
174 182
 * Callback called by the ml when a connection is ready to be used to send data to a remote peer
175 183
 * @param connectionID
......
180 188
	msgData_cb *p;
181 189
	p = (msgData_cb *)arg;
182 190
	if (p == NULL) return;
191
	if (p->cancelled) {
192
	    free(p);
193
	    return;
194
	}
183 195
	mlSendData(connectionID,(char *)(sendingBuffer[p->bIdx]),p->mSize,p->msgType,NULL);
184 196
/**/char mt = ((char*)sendingBuffer[p->bIdx])[0]; ++snd_counter;
185 197
	if (mt!=MSG_TYPE_TOPOLOGY &&
186 198
		mt!=MSG_TYPE_CHUNK && mt!=MSG_TYPE_SIGNALLING) {
187 199
			fprintf(stderr,"Net-helper ERROR! Sent message # %d of type %c and size %d\n",
188 200
				snd_counter,mt+'0', p->mSize);}
189
	free(sendingBuffer[p->bIdx]);
190
	sendingBuffer[p->bIdx] = NULL;
201
	free_sending_buffer(p->bIdx);
191 202
//	fprintf(stderr,"Net-helper: Message # %d for connection %d sent!\n ", p->bIdx,connectionID);
192 203
	//	event_base_loopbreak(base);
193
	free(p);
204
	p->conn_cb_called = true;
194 205
}
195 206

  
196 207
/**
......
204 215
	p = (msgData_cb *)arg;
205 216
	if (p != NULL) {
206 217
		fprintf(stderr,"Net-helper: Connection %d could not be established to send msg %d.\n ", connectionID,p->bIdx);
207
		free(sendingBuffer[p->bIdx]);
208
		sendingBuffer[p->bIdx] = NULL;
209
		free(p);//p->mSize = -1;
218
		if (p->cancelled) {
219
			free(p);//p->mSize = -1;
220
		} else {
221
			p->conn_cb_called = true;
222
		}
210 223
	}
211 224
	//	event_base_loopbreak(base);
212 225
}
......
326 339
}
327 340

  
328 341

  
342
void send_to_peer_cb(int fd, short event, void *arg)
343
{
344
	msgData_cb *p = (msgData_cb *) arg;
345
	if (p->conn_cb_called) {
346
		free(p);
347
	}
348
	else { //don't send it anymore
349
		free_sending_buffer(p->bIdx);
350
		p->cancelled = true;
351
		// don't free p, the other timeout will do it
352
	}
353
}
354

  
329 355
/**
330 356
 * Called by the application to send data to a remote peer
331 357
 * @param from
......
340 366
	int index = next_S();
341 367
	if (index<0) {
342 368
		// free(buffer_ptr);
369
		fprintf(stderr,"Net-helper: buffer full\n ");
343 370
		return -1;
344 371
	}
345 372
	sendingBuffer[index] = realloc(sendingBuffer[index],buffer_size);
......
347 374
	memcpy(sendingBuffer[index],buffer_ptr,buffer_size);
348 375
	// free(buffer_ptr);
349 376
	msgData_cb *p = malloc(sizeof(msgData_cb));
350
	p->bIdx = index; p->mSize = buffer_size; p->msgType = (unsigned char)buffer_ptr[0];
377
	p->bIdx = index; p->mSize = buffer_size; p->msgType = (unsigned char)buffer_ptr[0]; p->conn_cb_called = false; p->cancelled = false;
351 378
	int current = p->bIdx;
352 379
	send_params params = {0,0,0,0};
353 380
	to->connID = mlOpenConnection(to->addr,&connReady_cb,p, params);
354 381
	if (to->connID<0) {
355
		free(sendingBuffer[current]);
356
		sendingBuffer[current] = NULL;
382
		free_sending_buffer(current);
357 383
		fprintf(stderr,"Net-helper: Couldn't get a connection ID to send msg %d.\n ", p->bIdx);
358 384
		free(p);
359 385
		return -1;
360 386
	}
361 387
	else {
388
		struct timeval timeout = {0, 100*1000};
389
		event_base_once(base, -1, EV_TIMEOUT, send_to_peer_cb, (void *) p, &timeout);
362 390
		return buffer_size; //p->mSize;
363 391
	}
364 392

  

Also available in: Unified diff