Commit 0f1d2b904a15d01bf653b3b1f3e8b5f1c20e1b20
1 parent
3f373aab
use new SizedData from trbase and remove automatic cast.
Showing
9 changed files
with
91 additions
and
130 deletions
@@ -33,17 +33,16 @@ | @@ -33,17 +33,16 @@ | ||
33 | #include "tr/socket.h" | 33 | #include "tr/socket.h" |
34 | 34 | ||
35 | TR_CLASS(TR_RemoteData) { | 35 | TR_CLASS(TR_RemoteData) { |
36 | - unsigned char * data; | ||
37 | - size_t ndata; | ||
38 | - TR_Socket socket; | 36 | + TR_EXTENDS(TR_SizedData); |
37 | + | ||
38 | + TR_Socket remote; // TR_Socket might become a more abstract TR_Remote one day. | ||
39 | + int free_remote; | ||
39 | }; | 40 | }; |
40 | TR_INSTANCE_INIT(TR_RemoteData); | 41 | TR_INSTANCE_INIT(TR_RemoteData); |
41 | TR_CLASSVARS_DECL(TR_RemoteData) {}; | 42 | TR_CLASSVARS_DECL(TR_RemoteData) {}; |
42 | 43 | ||
43 | extern TR_RemoteData TR_emptyRemoteData; | 44 | extern TR_RemoteData TR_emptyRemoteData; |
44 | 45 | ||
45 | -void TR_remoteDataSetData(TR_RemoteData, unsigned char *, size_t); | ||
46 | - | ||
47 | #endif // __TR_REMOTE_DATA_H__ | 46 | #endif // __TR_REMOTE_DATA_H__ |
48 | 47 | ||
49 | // vim: set ts=4 sw=4: | 48 | // vim: set ts=4 sw=4: |
@@ -67,47 +67,50 @@ TR_CLASS(TR_Socket) { | @@ -67,47 +67,50 @@ TR_CLASS(TR_Socket) { | ||
67 | TR_INSTANCE_INIT(TR_Socket); | 67 | TR_INSTANCE_INIT(TR_Socket); |
68 | TR_CLASSVARS_DECL(TR_Socket) {}; | 68 | TR_CLASSVARS_DECL(TR_Socket) {}; |
69 | 69 | ||
70 | -#define TR_socketLog(socket) (((TR_Socket)(socket))->log) | ||
71 | -#define TR_socketFlags(socket) (((TR_Socket)(socket))->flags) | ||
72 | -#define TR_socketType(socket) (((TR_Socket)(socket))->type) | ||
73 | -#define TR_socketHost(socket) (((TR_Socket)(socket))->host) | ||
74 | -#define TR_socketPort(socket) (((TR_Socket)(socket))->port) | ||
75 | -#define TR_socketCname(socket) (((TR_Socket)(socket))->cname) | ||
76 | -#define TR_socketTtl(socket) (((TR_Socket)(socket))->ttl) | ||
77 | -#define TR_socketAddr(socket) ((void *)&((TR_Socket)(socket))->addr) | ||
78 | -#define TR_socketAddrlen(socket) (((TR_Socket)(socket))->addrlen) | ||
79 | -#define TR_socketHandle(socket) (((TR_Socket)(socket))->handle) | ||
80 | -#define TR_socketFinState(socket) (((TR_Socket)(socket))->fin_state) | ||
81 | -#define TR_socketFinRd(socket) ((TR_socketFinState((socket)) & 1) == 1) | ||
82 | -#define TR_socketFinWr(socket) ((TR_socketFinState((socket)) & 2) == 2) | ||
83 | -#define TR_socketFinRdWr(socket) ((TR_socketFinState((socket)) & 3) == 3) | ||
84 | - | ||
85 | -#define TR_socketAddrPort(socket) \ | ||
86 | - (((TR_Socket)(socket))->addr.info.sa_family == AF_INET \ | ||
87 | - ? ((TR_Socket)(socket))->addr.in.sin_port \ | ||
88 | - : ((TR_Socket)(socket))->addr.info.sa_family == AF_INET6 \ | ||
89 | - ? ((TR_Socket)(socket))->addr.in6.sin6_port \ | 70 | +#define TR_socketLog(socket) ((socket)->log) |
71 | +#define TR_socketFlags(socket) ((socket)->flags) | ||
72 | +#define TR_socketType(socket) ((socket)->type) | ||
73 | +#define TR_socketHost(socket) ((socket)->host) | ||
74 | +#define TR_socketPort(socket) ((socket)->port) | ||
75 | +#define TR_socketCname(socket) ((socket)->cname) | ||
76 | +#define TR_socketTtl(socket) ((socket)->ttl) | ||
77 | +#define TR_socketAddr(socket) ((void *)&(socket)->addr) | ||
78 | +#define TR_socketAddrlen(socket) ((socket)->addrlen) | ||
79 | +#define TR_socketHandle(socket) ((socket)->handle) | ||
80 | +#define TR_socketFinState(socket) ((socket)->fin_state) | ||
81 | +#define TR_socketFinRd(socket) \ | ||
82 | + ((TR_socketFinState(socket) & TR_FIN_RD) == TR_FIN_RD) | ||
83 | +#define TR_socketFinWr(socket) \ | ||
84 | + ((TR_socketFinState(socket) & TR_FIN_WR) == TR_FIN_WR) | ||
85 | +#define TR_socketFinRdWr(socket) \ | ||
86 | + ((TR_socketFinState(socket) & TR_FIN_RDWR) == TR_FIN_RDWR) | ||
87 | + | ||
88 | +#define TR_socketAddrPort(socket) \ | ||
89 | + ((socket)->addr.info.sa_family == AF_INET \ | ||
90 | + ? (socket)->addr.in.sin_port \ | ||
91 | + : (socket)->addr.info.sa_family == AF_INET6 \ | ||
92 | + ? (socket)->addr.in6.sin6_port \ | ||
90 | : 0) | 93 | : 0) |
91 | 94 | ||
92 | -#define TR_socketAddrIp(socket) \ | ||
93 | - (((TR_Socket)(socket))->addr.info.sa_family == AF_INET \ | ||
94 | - ? (void *)&((TR_Socket)(socket))->addr.in.sin_addr.s_addr \ | ||
95 | - : ((TR_Socket)(socket))->addr.info.sa_family == AF_INET6 \ | ||
96 | - ? (void *)&((TR_Socket)(socket))->addr.in6.sin6_addr.s6_addr \ | 95 | +#define TR_socketAddrIp(socket) \ |
96 | + ((socket)->addr.info.sa_family == AF_INET \ | ||
97 | + ? (void *)&(socket)->addr.in.sin_addr.s_addr \ | ||
98 | + : (socket)->addr.info.sa_family == AF_INET6 \ | ||
99 | + ? (void *)&(socket)->addr.in6.sin6_addr.s6_addr \ | ||
97 | : NULL) | 100 | : NULL) |
98 | 101 | ||
99 | -#define TR_socketAddrIplen(socket) \ | ||
100 | - (((TR_Socket)(socket))->addr.info.sa_family == AF_INET \ | ||
101 | - ? sizeof(((TR_Socket)(socket))->addr.in.sin_addr) \ | ||
102 | - : ((TR_Socket)(socket))->addr.info.sa_family == AF_INET6 \ | ||
103 | - ? sizeof(((TR_Socket)(socket))->addr.in6.sin6_addr) \ | 102 | +#define TR_socketAddrIplen(socket) \ |
103 | + ((socket)->addr.info.sa_family == AF_INET \ | ||
104 | + ? sizeof((socket)->addr.in.sin_addr) \ | ||
105 | + : (socket)->addr.info.sa_family == AF_INET6 \ | ||
106 | + ? sizeof((socket)->addr.in6.sin6_addr) \ | ||
104 | : 0) | 107 | : 0) |
105 | 108 | ||
106 | -#define TR_socketAddrIpStr(socket, buffer, nbuffer) \ | ||
107 | - if (TR_socketAddrIp((socket))) \ | ||
108 | - inet_ntop( \ | ||
109 | - ((TR_Socket)(socket))->addr.info.sa_family, \ | ||
110 | - TR_socketAddrIp((socket)), \ | 109 | +#define TR_socketAddrIpStr(socket, buffer, nbuffer) \ |
110 | + if (TR_socketAddrIp((socket))) \ | ||
111 | + inet_ntop( \ | ||
112 | + (socket)->addr.info.sa_family, \ | ||
113 | + TR_socketAddrIp((socket)), \ | ||
111 | buffer, nbuffer) | 114 | buffer, nbuffer) |
112 | 115 | ||
113 | TR_CLASS(TR_TcpSocket) { | 116 | TR_CLASS(TR_TcpSocket) { |
@@ -17,7 +17,6 @@ TRIO = stream.c \ | @@ -17,7 +17,6 @@ TRIO = stream.c \ | ||
17 | tcp_socket.c \ | 17 | tcp_socket.c \ |
18 | udp_socket.c \ | 18 | udp_socket.c \ |
19 | remote_data.c \ | 19 | remote_data.c \ |
20 | - remote_data_set_data.c \ | ||
21 | i_socket.c \ | 20 | i_socket.c \ |
22 | i_reader.c \ | 21 | i_reader.c \ |
23 | i_writer.c | 22 | i_writer.c |
@@ -55,7 +55,7 @@ TR_socketRecv(void * _this, size_t size) | @@ -55,7 +55,7 @@ TR_socketRecv(void * _this, size_t size) | ||
55 | 55 | ||
56 | TR_RETCALL(_this, TR_Socket, recv, remote_data, size); | 56 | TR_RETCALL(_this, TR_Socket, recv, remote_data, size); |
57 | 57 | ||
58 | - if (remote_data->ndata < 0) { | 58 | + if (((TR_SizedData)remote_data)->size < 0) { |
59 | switch (errno) { | 59 | switch (errno) { |
60 | case (EAGAIN|EWOULDBLOCK): | 60 | case (EAGAIN|EWOULDBLOCK): |
61 | TR_delete(remote_data); | 61 | TR_delete(remote_data); |
@@ -72,7 +72,7 @@ TR_socketRecv(void * _this, size_t size) | @@ -72,7 +72,7 @@ TR_socketRecv(void * _this, size_t size) | ||
72 | TR_delete(remote_data); | 72 | TR_delete(remote_data); |
73 | return NULL; | 73 | return NULL; |
74 | } | 74 | } |
75 | - } else if (remote_data->ndata == 0) { | 75 | + } else if (((TR_SizedData)remote_data)->size == 0) { |
76 | // this is a remote close... | 76 | // this is a remote close... |
77 | TR_delete(remote_data); | 77 | TR_delete(remote_data); |
78 | return NULL; | 78 | return NULL; |
@@ -31,6 +31,7 @@ | @@ -31,6 +31,7 @@ | ||
31 | #include <netdb.h> | 31 | #include <netdb.h> |
32 | 32 | ||
33 | #include "trbase.h" | 33 | #include "trbase.h" |
34 | + | ||
34 | #include "tr/remote_data.h" | 35 | #include "tr/remote_data.h" |
35 | #include "tr/socket.h" | 36 | #include "tr/socket.h" |
36 | 37 | ||
@@ -38,28 +39,25 @@ static | @@ -38,28 +39,25 @@ static | ||
38 | int | 39 | int |
39 | remoteDataCtor(void * _this, va_list * params) | 40 | remoteDataCtor(void * _this, va_list * params) |
40 | { | 41 | { |
41 | - TR_RemoteData this = _this; | ||
42 | - | ||
43 | - this->socket = va_arg(*params, TR_Socket); | ||
44 | - | ||
45 | - this->data = NULL; | ||
46 | - this->ndata = 0; | 42 | + TR_PARENTCALL(_this, TR_Class, ctor, params); |
43 | + ((TR_RemoteData)_this)->remote = va_arg(*params, TR_Socket); | ||
47 | 44 | ||
48 | return 0; | 45 | return 0; |
49 | } | 46 | } |
50 | 47 | ||
51 | static | 48 | static |
52 | void | 49 | void |
53 | -remoteDataDtor(void * _this) | ||
54 | -{ | ||
55 | - TR_RemoteData this = _this; | 50 | +remoteDataDtor(void * _this) { |
51 | + TR_PARENTCALL(_this, TR_Class, dtor); | ||
56 | 52 | ||
57 | - TR_MEM_FREE(this->data); | 53 | + if (((TR_RemoteData)_this)->free_remote) { |
54 | + TR_delete(((TR_RemoteData)_this)->remote); | ||
55 | + } | ||
58 | } | 56 | } |
59 | 57 | ||
60 | TR_INIT_IFACE(TR_Class, remoteDataCtor, remoteDataDtor, NULL); | 58 | TR_INIT_IFACE(TR_Class, remoteDataCtor, remoteDataDtor, NULL); |
61 | -TR_CREATE_CLASS(TR_RemoteData, NULL, NULL, TR_IF(TR_Class)); | 59 | +TR_CREATE_CLASS(TR_RemoteData, TR_SizedData, NULL, TR_IF(TR_Class)); |
62 | 60 | ||
63 | -TR_INSTANCE(TR_RemoteData, TR_emptyRemoteData, NULL, 0, NULL, NULL); | 61 | +TR_INSTANCE(TR_RemoteData, TR_emptyRemoteData, { NULL, 0 }, NULL); |
64 | 62 | ||
65 | // vim: set ts=4 sw=4: | 63 | // vim: set ts=4 sw=4: |
src/remote_data_set_data.c
deleted
100644 → 0
1 | -/** | ||
2 | - * \file | ||
3 | - * | ||
4 | - * \author Georg Hopp | ||
5 | - * | ||
6 | - * \copyright | ||
7 | - * Copyright © 2014 Georg Hopp | ||
8 | - * | ||
9 | - * This program is free software: you can redistribute it and/or modify | ||
10 | - * it under the terms of the GNU General Public License as published by | ||
11 | - * the Free Software Foundation, either version 3 of the License, or | ||
12 | - * (at your option) any later version. | ||
13 | - * | ||
14 | - * This program is distributed in the hope that it will be useful, | ||
15 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | - * GNU General Public License for more details. | ||
18 | - * | ||
19 | - * You should have received a copy of the GNU General Public License | ||
20 | - * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
21 | - */ | ||
22 | - | ||
23 | -#include <string.h> // for atoi() and exit() | ||
24 | -#include <errno.h> // for errno | ||
25 | -#include <stdio.h> | ||
26 | -#include <unistd.h> | ||
27 | - | ||
28 | -#include "tr/remote_data.h" | ||
29 | -#include "tr/logger.h" | ||
30 | - | ||
31 | -void | ||
32 | -TR_remoteDataSetData(TR_RemoteData this, unsigned char * data, size_t size) | ||
33 | -{ | ||
34 | - if (this->data && TR_getSize(this->data) < size) { | ||
35 | - TR_MEM_FREE(this->data); | ||
36 | - } | ||
37 | - | ||
38 | - if (! this->data) { | ||
39 | - this->data = TR_malloc(size); | ||
40 | - } | ||
41 | - | ||
42 | - this->ndata = size; | ||
43 | - memcpy(this->data, data, size); | ||
44 | -} | ||
45 | - | ||
46 | -// vim: set ts=4 sw=4: |
@@ -40,12 +40,13 @@ | @@ -40,12 +40,13 @@ | ||
40 | TR_TcpSocket | 40 | TR_TcpSocket |
41 | TR_socketAccept(TR_TcpSocket this) | 41 | TR_socketAccept(TR_TcpSocket this) |
42 | { | 42 | { |
43 | - TR_Socket remote = TR_new(TR_TcpSocket, TR_socketLog(this), NULL, 0, 0); | 43 | + TR_Socket remote = |
44 | + TR_new(TR_TcpSocket, TR_socketLog((TR_Socket)this), NULL, 0, 0); | ||
44 | //int flags; | 45 | //int flags; |
45 | 46 | ||
46 | remote->addrlen = ((TR_Socket)this)->addrlen; | 47 | remote->addrlen = ((TR_Socket)this)->addrlen; |
47 | remote->handle = accept( | 48 | remote->handle = accept( |
48 | - TR_socketHandle(this), | 49 | + TR_socketHandle((TR_Socket)this), |
49 | &(remote->addr.info), | 50 | &(remote->addr.info), |
50 | &(remote->addrlen)); | 51 | &(remote->addrlen)); |
51 | 52 | ||
@@ -58,8 +59,8 @@ TR_socketAccept(TR_TcpSocket this) | @@ -58,8 +59,8 @@ TR_socketAccept(TR_TcpSocket this) | ||
58 | } else { | 59 | } else { |
59 | TR_socketAddrIpStr(remote, remote->host, TR_MAX_HOST); | 60 | TR_socketAddrIpStr(remote, remote->host, TR_MAX_HOST); |
60 | remote->port = TR_socketAddrPort(remote); | 61 | remote->port = TR_socketAddrPort(remote); |
61 | - remote->type = TR_socketType(this); | ||
62 | - remote->flags = TR_socketFlags(this); | 62 | + remote->type = TR_socketType((TR_Socket)this); |
63 | + remote->flags = TR_socketFlags((TR_Socket)this); | ||
63 | } | 64 | } |
64 | 65 | ||
65 | return (TR_TcpSocket)remote; | 66 | return (TR_TcpSocket)remote; |
@@ -42,9 +42,9 @@ tcpSocketCtor(void * _this, va_list * params) | @@ -42,9 +42,9 @@ tcpSocketCtor(void * _this, va_list * params) | ||
42 | 42 | ||
43 | TR_PARENTCALL(_this, TR_Class, ctor, params); | 43 | TR_PARENTCALL(_this, TR_Class, ctor, params); |
44 | 44 | ||
45 | - TR_socketType(this) = SOCK_STREAM; | ||
46 | - this->listen = FALSE; | ||
47 | - this->connected = FALSE; | 45 | + TR_socketType((TR_Socket)this) = SOCK_STREAM; |
46 | + this->listen = FALSE; | ||
47 | + this->connected = FALSE; | ||
48 | 48 | ||
49 | return 0; | 49 | return 0; |
50 | } | 50 | } |
@@ -62,7 +62,7 @@ tcpSocketBind(void * _this) | @@ -62,7 +62,7 @@ tcpSocketBind(void * _this) | ||
62 | return -1; | 62 | return -1; |
63 | } | 63 | } |
64 | 64 | ||
65 | - if (listen(TR_socketHandle(this), SOMAXCONN) != 0) { | 65 | + if (listen(TR_socketHandle((TR_Socket)this), SOMAXCONN) != 0) { |
66 | // error | 66 | // error |
67 | return -1; | 67 | return -1; |
68 | } | 68 | } |
@@ -76,28 +76,29 @@ int | @@ -76,28 +76,29 @@ int | ||
76 | tcpSocketConnect(void * _this) | 76 | tcpSocketConnect(void * _this) |
77 | { | 77 | { |
78 | TR_Socket this = _this; | 78 | TR_Socket this = _this; |
79 | - | ||
80 | - return connect( | 79 | + int retval = connect( |
81 | this->handle, | 80 | this->handle, |
82 | (struct sockaddr *)&(this->addr), | 81 | (struct sockaddr *)&(this->addr), |
83 | this->addrlen); | 82 | this->addrlen); |
83 | + | ||
84 | + ((TR_TcpSocket)_this)->connected = TRUE; | ||
85 | + return retval; | ||
84 | } | 86 | } |
85 | 87 | ||
86 | static | 88 | static |
87 | TR_RemoteData | 89 | TR_RemoteData |
88 | tcpSocketRecv(TR_Socket this, size_t size) | 90 | tcpSocketRecv(TR_Socket this, size_t size) |
89 | { | 91 | { |
90 | - TR_RemoteData rdata = TR_new(TR_RemoteData, &(this->addr), this->addrlen); | 92 | + TR_RemoteData rdata = NULL; |
91 | unsigned char buffer[size]; | 93 | unsigned char buffer[size]; |
92 | ssize_t received; | 94 | ssize_t received; |
93 | 95 | ||
94 | received = recv(this->handle, buffer, size, this->flags); | 96 | received = recv(this->handle, buffer, size, this->flags); |
95 | 97 | ||
96 | if (-1 == received) { | 98 | if (-1 == received) { |
97 | - perror("recv"); | ||
98 | - TR_delete(rdata); | 99 | + rdata = TR_new(TR_RemoteData, NULL, received, this); |
99 | } else { | 100 | } else { |
100 | - TR_remoteDataSetData(rdata, buffer, received); | 101 | + rdata = TR_new(TR_RemoteData, buffer, received, this); |
101 | } | 102 | } |
102 | 103 | ||
103 | return rdata; | 104 | return rdata; |
@@ -107,7 +108,11 @@ static | @@ -107,7 +108,11 @@ static | ||
107 | ssize_t | 108 | ssize_t |
108 | tcpSocketSend(TR_Socket this, TR_RemoteData data) | 109 | tcpSocketSend(TR_Socket this, TR_RemoteData data) |
109 | { | 110 | { |
110 | - return send(this->handle, data->data, data->ndata, this->flags); | 111 | + return send( |
112 | + this->handle, | ||
113 | + ((TR_SizedData)data)->data, | ||
114 | + ((TR_SizedData)data)->size, | ||
115 | + this->flags); | ||
111 | } | 116 | } |
112 | 117 | ||
113 | TR_INIT_IFACE(TR_Class, tcpSocketCtor, NULL, NULL); | 118 | TR_INIT_IFACE(TR_Class, tcpSocketCtor, NULL, NULL); |
@@ -51,27 +51,29 @@ static | @@ -51,27 +51,29 @@ static | ||
51 | TR_RemoteData | 51 | TR_RemoteData |
52 | udpSocketRecv(TR_Socket this, size_t size) | 52 | udpSocketRecv(TR_Socket this, size_t size) |
53 | { | 53 | { |
54 | - TR_RemoteData rdata; | ||
55 | - struct sockaddr_in addr; | ||
56 | - socklen_t addrlen = this->addrlen; | ||
57 | - unsigned char buffer[size]; | ||
58 | - ssize_t received; | 54 | + unsigned char buffer[size]; |
55 | + ssize_t received; | ||
56 | + TR_RemoteData rdata; | ||
57 | + TR_Socket remote = TR_new(TR_UdpSocket, TR_socketLog(this), NULL, 0, 0); | ||
58 | + | ||
59 | + remote->addrlen = this->addrlen; | ||
59 | 60 | ||
60 | received = recvfrom( | 61 | received = recvfrom( |
61 | this->handle, | 62 | this->handle, |
62 | buffer, | 63 | buffer, |
63 | size, | 64 | size, |
64 | this->flags, | 65 | this->flags, |
65 | - (struct sockaddr *)&addr, | ||
66 | - &addrlen); | 66 | + (struct sockaddr *)&(remote->addr), |
67 | + &remote->addrlen); | ||
67 | 68 | ||
68 | if (-1 == received) { | 69 | if (-1 == received) { |
69 | - perror("recvfrom"); | 70 | + rdata = TR_new(TR_RemoteData, NULL, received, remote); |
70 | } else { | 71 | } else { |
71 | - rdata = TR_new(TR_RemoteData, &addr, addrlen); | ||
72 | - TR_remoteDataSetData(rdata, buffer, received); | 72 | + rdata = TR_new(TR_RemoteData, buffer, received, remote); |
73 | } | 73 | } |
74 | 74 | ||
75 | + rdata->free_remote = 1; | ||
76 | + | ||
75 | return rdata; | 77 | return rdata; |
76 | } | 78 | } |
77 | 79 | ||
@@ -83,11 +85,11 @@ udpSocketSend(TR_Socket this, TR_RemoteData data) | @@ -83,11 +85,11 @@ udpSocketSend(TR_Socket this, TR_RemoteData data) | ||
83 | 85 | ||
84 | send = sendto( | 86 | send = sendto( |
85 | this->handle, | 87 | this->handle, |
86 | - data->data, | ||
87 | - data->ndata, | 88 | + ((TR_SizedData)data)->data, |
89 | + ((TR_SizedData)data)->size, | ||
88 | this->flags, | 90 | this->flags, |
89 | - &(data->socket->addr.info), | ||
90 | - data->socket->addrlen); | 91 | + &(data->remote->addr.info), |
92 | + data->remote->addrlen); | ||
91 | 93 | ||
92 | if (-1 == send) { | 94 | if (-1 == send) { |
93 | perror("sendto"); | 95 | perror("sendto"); |
Please
register
or
login
to post a comment