You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
2.9 KiB
C++
148 lines
2.9 KiB
C++
14 years ago
|
#include "w5100.h"
|
||
|
#include "socket.h"
|
||
|
|
||
15 years ago
|
extern "C" {
|
||
|
#include "string.h"
|
||
|
}
|
||
|
|
||
|
#include "WProgram.h"
|
||
|
|
||
|
#include "Ethernet.h"
|
||
|
#include "Client.h"
|
||
|
#include "Server.h"
|
||
|
|
||
14 years ago
|
uint16_t Client::_srcport = 1024;
|
||
15 years ago
|
|
||
14 years ago
|
Client::Client(uint8_t sock) : _sock(sock) {
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
Client::Client(uint8_t *ip, uint16_t port) : _ip(ip), _port(port), _sock(MAX_SOCK_NUM) {
|
||
15 years ago
|
}
|
||
|
|
||
|
uint8_t Client::connect() {
|
||
14 years ago
|
if (_sock != MAX_SOCK_NUM)
|
||
15 years ago
|
return 0;
|
||
14 years ago
|
|
||
15 years ago
|
for (int i = 0; i < MAX_SOCK_NUM; i++) {
|
||
14 years ago
|
uint8_t s = W5100.readSnSR(i);
|
||
|
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
|
||
15 years ago
|
_sock = i;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
14 years ago
|
|
||
|
if (_sock == MAX_SOCK_NUM)
|
||
15 years ago
|
return 0;
|
||
14 years ago
|
|
||
15 years ago
|
_srcport++;
|
||
14 years ago
|
if (_srcport == 0) _srcport = 1024;
|
||
|
socket(_sock, SnMR::TCP, _srcport, 0);
|
||
|
|
||
15 years ago
|
if (!::connect(_sock, _ip, _port)) {
|
||
14 years ago
|
_sock = MAX_SOCK_NUM;
|
||
15 years ago
|
return 0;
|
||
|
}
|
||
14 years ago
|
|
||
|
while (status() != SnSR::ESTABLISHED) {
|
||
15 years ago
|
delay(1);
|
||
14 years ago
|
if (status() == SnSR::CLOSED) {
|
||
|
_sock = MAX_SOCK_NUM;
|
||
15 years ago
|
return 0;
|
||
|
}
|
||
|
}
|
||
14 years ago
|
|
||
15 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
|
void Client::write(uint8_t b) {
|
||
14 years ago
|
if (_sock != MAX_SOCK_NUM)
|
||
15 years ago
|
send(_sock, &b, 1);
|
||
|
}
|
||
|
|
||
|
void Client::write(const char *str) {
|
||
14 years ago
|
if (_sock != MAX_SOCK_NUM)
|
||
15 years ago
|
send(_sock, (const uint8_t *)str, strlen(str));
|
||
|
}
|
||
|
|
||
|
void Client::write(const uint8_t *buf, size_t size) {
|
||
14 years ago
|
if (_sock != MAX_SOCK_NUM)
|
||
15 years ago
|
send(_sock, buf, size);
|
||
|
}
|
||
|
|
||
|
int Client::available() {
|
||
14 years ago
|
if (_sock != MAX_SOCK_NUM)
|
||
|
return W5100.getRXReceivedSize(_sock);
|
||
15 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
|
int Client::read() {
|
||
|
uint8_t b;
|
||
|
if (!available())
|
||
|
return -1;
|
||
|
recv(_sock, &b, 1);
|
||
|
return b;
|
||
|
}
|
||
|
|
||
14 years ago
|
int Client::peek() {
|
||
|
uint8_t b;
|
||
|
if (!available())
|
||
|
return -1;
|
||
|
::peek(_sock, &b);
|
||
|
return b;
|
||
|
}
|
||
|
|
||
15 years ago
|
void Client::flush() {
|
||
|
while (available())
|
||
|
read();
|
||
|
}
|
||
|
|
||
|
void Client::stop() {
|
||
14 years ago
|
if (_sock == MAX_SOCK_NUM)
|
||
15 years ago
|
return;
|
||
14 years ago
|
|
||
15 years ago
|
// attempt to close the connection gracefully (send a FIN to other side)
|
||
|
disconnect(_sock);
|
||
|
unsigned long start = millis();
|
||
14 years ago
|
|
||
15 years ago
|
// wait a second for the connection to close
|
||
14 years ago
|
while (status() != SnSR::CLOSED && millis() - start < 1000)
|
||
15 years ago
|
delay(1);
|
||
14 years ago
|
|
||
15 years ago
|
// if it hasn't closed, close it forcefully
|
||
14 years ago
|
if (status() != SnSR::CLOSED)
|
||
15 years ago
|
close(_sock);
|
||
14 years ago
|
|
||
15 years ago
|
EthernetClass::_server_port[_sock] = 0;
|
||
14 years ago
|
_sock = MAX_SOCK_NUM;
|
||
15 years ago
|
}
|
||
|
|
||
|
uint8_t Client::connected() {
|
||
14 years ago
|
if (_sock == MAX_SOCK_NUM) return 0;
|
||
|
|
||
|
uint8_t s = status();
|
||
|
return !(s == SnSR::LISTEN || s == SnSR::CLOSED || s == SnSR::FIN_WAIT ||
|
||
|
(s == SnSR::CLOSE_WAIT && !available()));
|
||
15 years ago
|
}
|
||
|
|
||
|
uint8_t Client::status() {
|
||
14 years ago
|
if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED;
|
||
|
return W5100.readSnSR(_sock);
|
||
15 years ago
|
}
|
||
|
|
||
|
// the next three functions are a hack so we can compare the client returned
|
||
|
// by Server::available() to null, or use it as the condition in an
|
||
|
// if-statement. this lets us stay compatible with the Processing network
|
||
|
// library.
|
||
|
|
||
|
uint8_t Client::operator==(int p) {
|
||
14 years ago
|
return _sock == MAX_SOCK_NUM;
|
||
15 years ago
|
}
|
||
|
|
||
|
uint8_t Client::operator!=(int p) {
|
||
14 years ago
|
return _sock != MAX_SOCK_NUM;
|
||
15 years ago
|
}
|
||
|
|
||
|
Client::operator bool() {
|
||
14 years ago
|
return _sock != MAX_SOCK_NUM;
|
||
15 years ago
|
}
|