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 */
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
) {
16 Target_Socket
*socket
;
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
;
25 int ret
= send(fd
, mem
->data
+ socket
->current_position
, mem
->len
- socket
->current_position
, 0);
27 socket
->current_position
+= ret
;
28 if(socket
->current_position
== mem
->len
) { /* Done */
29 socket
->current_position
= 0;
30 return OS_RESULT_COMPLETE
;
33 if(errno
== EAGAIN
) /* Need to try later */
34 return OS_RESULT_WAIT
;
36 printf("FAILED TO WRITE!\n");
37 return OS_RESULT_COMPLETE
; /* ??? What to do here ??? */
42 static void free_OutStrategy_Memory_Socket(OutStrategy
*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
;
55 if(strategy
) free(strategy
);