|
| 1 | +#include <string.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <sys/types.h> |
| 6 | +#include <sys/socket.h> |
| 7 | +#include <netinet/tcp.h> |
| 8 | +#include <arpa/inet.h> |
| 9 | +#include <sys/un.h> |
| 10 | +#include <sys/ioctl.h> |
| 11 | +#include <errno.h> |
| 12 | + |
| 13 | +#include <ProcessingUnit.h> |
| 14 | + |
| 15 | +#include "pu-linux-tcp-client.h" |
| 16 | + |
| 17 | +struct ProcessingUnitStructureSocket{ |
| 18 | + int serverSockets; |
| 19 | + struct sockaddr_in local; |
| 20 | + int len; |
| 21 | +}; |
| 22 | + |
| 23 | +typedef struct ProcessingUnitStructureSocket ProcessingUnitStructureSocket; |
| 24 | + |
| 25 | +_Bool CreateLinuxTCPClient(ProcessingUnitStructure **pu, char *ip, int port){ |
| 26 | + ProcessingUnitStructureSocket *puS; |
| 27 | + bool success; |
| 28 | + int ret; |
| 29 | + |
| 30 | + success = true; |
| 31 | + |
| 32 | + *pu = malloc(sizeof(ProcessingUnitStructure)); // Alloc #1 |
| 33 | + puS = malloc(sizeof(ProcessingUnitStructureSocket)); // Alloc #1 |
| 34 | + (*pu)->p = puS; |
| 35 | + |
| 36 | + puS->serverSockets = socket(AF_INET, SOCK_STREAM, 0); |
| 37 | + |
| 38 | + if(puS->serverSockets != -1){ |
| 39 | + bzero(&puS->local, sizeof(puS->local)); |
| 40 | + |
| 41 | + puS->local.sin_family = AF_INET; |
| 42 | + puS->local.sin_addr.s_addr = inet_addr(ip); |
| 43 | + puS->local.sin_port = htons(port); |
| 44 | + |
| 45 | + puS->len = sizeof(puS->local); |
| 46 | + |
| 47 | + ret = connect(puS->serverSockets, (struct sockaddr *)&puS->local, puS->len); |
| 48 | + |
| 49 | + if(ret == 0){ |
| 50 | + }else{ |
| 51 | + success = false; |
| 52 | + printf("connect failed: %d\n", errno); |
| 53 | + } |
| 54 | + }else{ |
| 55 | + success = false; |
| 56 | + printf("socket failed: %d\n", errno); |
| 57 | + } |
| 58 | + |
| 59 | + //printf("Client success: %d\n", success); |
| 60 | + |
| 61 | + return success; |
| 62 | +} |
| 63 | + |
| 64 | +void CloseLinuxTCPClient(ProcessingUnitStructure *pu){ |
| 65 | + ProcessingUnitStructureSocket *puS; |
| 66 | + |
| 67 | + puS = (ProcessingUnitStructureSocket*)pu->p; |
| 68 | + |
| 69 | + close(puS->serverSockets); |
| 70 | + |
| 71 | + free(pu); // Free #1 |
| 72 | + free(puS); // Free #2 |
| 73 | +} |
| 74 | + |
| 75 | +#include "socklib.c" |
| 76 | + |
| 77 | +void Send(ProcessingUnitStructure *pu, uint8_t *message, size_t messageLength){ |
| 78 | + ProcessingUnitStructureSocket *puS; |
| 79 | + double length = messageLength; |
| 80 | + char lengthString[20]; |
| 81 | + uint8_t *buffer; |
| 82 | + long i; |
| 83 | + |
| 84 | + puS = (ProcessingUnitStructureSocket*)pu->p; |
| 85 | + |
| 86 | + sprintf(lengthString, "%15Ld", (long long)messageLength); |
| 87 | + |
| 88 | + sendAll(puS->serverSockets, (uint8_t*)lengthString, 15); |
| 89 | + sendAll(puS->serverSockets, message, messageLength); |
| 90 | +} |
| 91 | + |
| 92 | +void Receive(ProcessingUnitStructure *pu, ByteArrayReference *message){ |
| 93 | + ProcessingUnitStructureSocket *puS; |
| 94 | + double length; |
| 95 | + char lengthStr[15]; |
| 96 | + _Bool success; |
| 97 | + uint8_t *buffer; |
| 98 | + long i; |
| 99 | + |
| 100 | + puS = (ProcessingUnitStructureSocket*)pu->p; |
| 101 | + |
| 102 | + success = recvAll(puS->serverSockets, (uint8_t*)lengthStr, 15); |
| 103 | + |
| 104 | + if(success){ |
| 105 | + length = atof(lengthStr); |
| 106 | + } |
| 107 | + |
| 108 | + message->byteArrayLength = length; |
| 109 | + message->byteArray = malloc(message->byteArrayLength); |
| 110 | + |
| 111 | + recvAll(puS->serverSockets, message->byteArray, length); |
| 112 | +} |
| 113 | + |
| 114 | +bool Check(ProcessingUnitStructure *pu){ |
| 115 | + ProcessingUnitStructureSocket *puS = (ProcessingUnitStructureSocket*)pu->p; |
| 116 | + int count; |
| 117 | + ioctl(puS->serverSockets, FIONREAD, &count); |
| 118 | + return count > 0; |
| 119 | +} |
| 120 | + |
| 121 | +void Call(ProcessingUnitStructure *pu, uint8_t *s, size_t sLength, ByteArrayReference *d){ |
| 122 | + Send(pu, s, sLength); |
| 123 | + Receive(pu, d); |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
0 commit comments