@@ -56,106 +56,111 @@ uint8_t UDP::begin(uint16_t port) {
56
56
return 1 ;
57
57
}
58
58
59
- /* Send packet contained in buf of length len to peer at specified ip, and port */
60
- /* Use this function to transmit binary data that might contain 0x00 bytes*/
61
- /* This function returns sent data size for success else -1. */
62
- uint16_t UDP::sendPacket (uint8_t * buf, uint16_t len, uint8_t * ip, uint16_t port){
63
- return sendto (_sock,(const uint8_t *)buf,len,ip,port);
64
- }
65
-
66
- /* Send zero-terminated string str as packet to peer at specified ip, and port */
67
- /* This function returns sent data size for success else -1. */
68
- uint16_t UDP::sendPacket (const char str[], uint8_t * ip, uint16_t port){
69
- // compute strlen
70
- const char *s;
71
- for (s = str; *s; ++s);
72
- uint16_t len = (s-str);
73
- // send packet
74
- return sendto (_sock,(const uint8_t *)str,len,ip,port);
75
- }
76
59
/* Is data available in rx buffer? Returns 0 if no, number of available bytes if yes.
77
60
* returned value includes 8 byte UDP header!*/
78
61
int UDP::available () {
79
62
return W5100.getRXReceivedSize (_sock);
80
63
}
81
64
65
+ /* Release any resources being used by this UDP instance */
66
+ void UDP::stop ()
67
+ {
68
+ if (_sock == MAX_SOCK_NUM)
69
+ return ;
82
70
83
- /* Read a received packet into buffer buf (which is of maximum length len); */
84
- /* store calling ip and port as well. Call available() to make sure data is ready first. */
85
- /* NOTE: I don't believe len is ever checked in implementation of recvfrom(),*/
86
- /* so it's easy to overflow buffer. so we check and truncate. */
87
- /* returns number of bytes read, or negative number of bytes we would have needed if we truncated */
88
- int UDP::readPacket (uint8_t * buf, uint16_t bufLen, uint8_t *ip, uint16_t *port) {
89
- int packetLen = available ()-8 ; // skip UDP header;
90
- if (packetLen < 0 ) return 0 ; // no real data here
91
- if (packetLen > (int )bufLen) {
92
- // packet is too large - truncate
93
- // HACK - hand-parse the UDP packet using TCP recv method
94
- uint8_t tmpBuf[8 ];
95
- int i;
96
- // read 8 header bytes and get IP and port from it
97
- recv (_sock,tmpBuf,8 );
98
- ip[0 ] = tmpBuf[0 ];
99
- ip[1 ] = tmpBuf[1 ];
100
- ip[2 ] = tmpBuf[2 ];
101
- ip[3 ] = tmpBuf[3 ];
102
- *port = tmpBuf[4 ];
103
- *port = (*port << 8 ) + tmpBuf[5 ];
104
-
105
- // now copy first (bufLen) bytes into buf
106
- for (i=0 ;i<(int )bufLen;i++) {
107
- recv (_sock,tmpBuf,1 );
108
- buf[i]=tmpBuf[0 ];
109
- }
71
+ close (_sock);
110
72
111
- // and just read the rest byte by byte and throw it away
112
- while (available ()) {
113
- recv (_sock,tmpBuf,1 );
114
- }
73
+ EthernetClass::_server_port[_sock] = 0 ;
74
+ _sock = MAX_SOCK_NUM;
75
+ }
115
76
116
- return (-1 *packetLen);
77
+ int UDP::beginPacket (IPAddress ip, uint16_t port)
78
+ {
79
+ _offset = 0 ;
80
+ return startUDP (_sock, ip, port);
81
+ }
117
82
118
- // ALTERNATIVE: requires stdlib - takes a bunch of space
119
- /* //create new buffer and read everything into it
120
- uint8_t * tmpBuf = (uint8_t *)malloc(packetLen);
121
- recvfrom(_sock,tmpBuf,packetLen,ip,port);
122
- if(!tmpBuf) return 0; //couldn't allocate
123
- // copy first bufLen bytes
124
- for(unsigned int i=0; i<bufLen; i++) {
125
- buf[i]=tmpBuf[i];
126
- }
127
- //free temp buffer
128
- free(tmpBuf);
129
- */
83
+ int UDP::endPacket ()
84
+ {
85
+ return sendUDP (_sock);
86
+ }
130
87
88
+ void UDP::write (uint8_t byte)
89
+ {
90
+ write (&byte, 1 );
91
+ }
131
92
132
- }
133
- return recvfrom (_sock,buf,bufLen,ip,port);
93
+ void UDP::write (const char *str)
94
+ {
95
+ size_t len = strlen (str);
96
+ write ((const uint8_t *)str, len);
134
97
}
135
98
136
- /* Read a received packet, throw away peer's ip and port. See note above. */
137
- int UDP::readPacket (uint8_t * buf, uint16_t len) {
138
- uint8_t ip[4 ];
139
- uint16_t port[1 ];
140
- return recvfrom (_sock,buf,len,ip,port);
99
+ void UDP::write (const uint8_t *buffer, size_t size)
100
+ {
101
+ uint16_t bytes_written = bufferData (_sock, _offset, buffer, size);
102
+ _offset += bytes_written;
141
103
}
142
104
143
- int UDP::readPacket (char * buf, uint16_t bufLen, uint8_t *ip, uint16_t &port) {
144
- uint16_t myPort;
145
- uint16_t ret = readPacket ( (byte*)buf, bufLen, ip, &myPort);
146
- port = myPort;
147
- return ret;
105
+ int UDP::parsePacket ()
106
+ {
107
+ // HACK - hand-parse the UDP packet using TCP recv method
108
+ uint8_t tmpBuf[8 ];
109
+ int ret =0 ;
110
+ // read 8 header bytes and get IP and port from it
111
+ ret = recv (_sock,tmpBuf,8 );
112
+ if (ret > 0 )
113
+ {
114
+ _remoteIP = tmpBuf;
115
+ _remotePort = tmpBuf[4 ];
116
+ _remotePort = (_remotePort << 8 ) + tmpBuf[5 ];
117
+ // When we get here, any remaining bytes are the data
118
+ ret = available ();
119
+ }
120
+ return ret;
148
121
}
149
122
150
- /* Release any resources being used by this UDP instance */
151
- void UDP::stop ()
123
+ int UDP::read ()
152
124
{
153
- if (_sock == MAX_SOCK_NUM)
154
- return ;
125
+ uint8_t byte;
126
+ if (recv (_sock, &byte, 1 ) > 0 )
127
+ {
128
+ // We read things without any problems
129
+ return byte;
130
+ }
131
+ // If we get here, there's no data available
132
+ return -1 ;
133
+ }
155
134
156
- close (_sock);
135
+ int UDP::read (unsigned char * buffer, size_t len)
136
+ {
137
+ /* In the readPacket that copes with truncating packets, the buffer was
138
+ filled with this code. Not sure why it loops round reading out a byte
139
+ at a time.
140
+ int i;
141
+ for(i=0;i<(int)bufLen;i++) {
142
+ recv(_sock,tmpBuf,1);
143
+ buf[i]=tmpBuf[0];
144
+ }
145
+ */
146
+ return recv (_sock, buffer, len);
147
+ }
157
148
158
- EthernetClass::_server_port[_sock] = 0 ;
159
- _sock = MAX_SOCK_NUM;
149
+ int UDP::peek ()
150
+ {
151
+ uint8_t b;
152
+ // Unlike recv, peek doesn't check to see if there's any data available, so we must
153
+ if (!available ())
154
+ return -1 ;
155
+ ::peek (_sock, &b);
156
+ return b;
157
+ }
158
+
159
+ void UDP::flush ()
160
+ {
161
+ while (available ())
162
+ {
163
+ read ();
164
+ }
160
165
}
161
166
0 commit comments