core/out_queue: More work on refactoring/dissecting the socket output queue
[lumina.git] / core / src / out_strategy / memory_socket.c
blob50681f9d752c8a3665ed6bc3cc6b4f67d8974a43
1 #include "out_strategy.h"
2 #include "out_strategy/memory_socket.h"
3 #include "output_queue_socket_internal.h"
5 #include <errno.h> /* errno, EAGAIN */
6 #include <sys/types.h>
7 #include <sys/socket.h> /* send */
9 #include <stdio.h> /* printf - in temporary area */
11 static DataItemType handledData[] = { DATA_ITEM_MEMORY, 0 };
12 static TargetType handledTargets[] = { TARGET_SOCKET, 0 };
14 static OutStrategy_Result OutStrategy_Memory_Socket_put(OutStrategy *strategy, DataItem *item, Target *target) {
15 MemoryItem *mem;
16 Target_Socket *socket;
17 int fd;
18 assert(strategy && item && target);
19 if(item->typeID != DATA_ITEM_MEMORY
20 || target->typeID != TARGET_SOCKET)
21 return OS_RESULT_SKIP;
22 mem = (MemoryItem*)item;
23 fd = socket->writable.fd;
24 while(1) {
25 int ret = send(fd, mem->data + socket->current_position, mem->len - socket->current_position, 0);
26 if(ret > 0) {
27 socket->current_position += ret;
28 if(socket->current_position == mem->len) { /* Done */
29 socket->current_position = 0;
30 return OS_RESULT_COMPLETE;
32 } else {
33 if(errno == EAGAIN) /* Need to try later */
34 return OS_RESULT_WAIT;
35 /* ERROR! */
36 printf("FAILED TO WRITE!\n");
37 return OS_RESULT_COMPLETE; /* ??? What to do here ??? */
42 static void free_OutStrategy_Memory_Socket(OutStrategy *strategy) {
43 free(strategy);
46 OutStrategy *new_OutStrategy_Memory_Socket() {
47 OutStrategy *strategy = (OutStrategy*)calloc(1, sizeof(OutStrategy));
48 if(!strategy) goto fail;
49 strategy->handledData = handledData;
50 strategy->handledTargets = handledTargets;
51 strategy->put = OutStrategy_Memory_Socket_put;
52 strategy->free = free_OutStrategy_Memory_Socket;
53 return strategy;
54 fail:
55 if(strategy) free(strategy);
56 return NULL;