mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-20 14:54:31 +01:00
Fix issue related to multiple client opened on web server sketch
This commit is contained in:
parent
6a83038ccd
commit
9c30c73c2b
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@ -33,8 +33,6 @@ extern bool ifStatus;
|
||||
|
||||
static int isDataSentCount = 0;
|
||||
|
||||
bool pending_accept = false;
|
||||
|
||||
static err_t tcp_data_sent(void *arg, struct tcp_pcb *pcb, u16_t len);
|
||||
|
||||
static void atcp_init_pend_flags(struct ttcp* _ttcp)
|
||||
@ -44,7 +42,6 @@ static void atcp_init_pend_flags(struct ttcp* _ttcp)
|
||||
{
|
||||
if (_ttcp) _ttcp->pending_close[i] = false;
|
||||
}
|
||||
pending_accept = false;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -118,7 +115,7 @@ static err_t tcp_send_data(struct ttcp *ttcp) {
|
||||
len = ttcp->left;
|
||||
ttcp->buff_sent = 0;
|
||||
|
||||
if (len == 0) return;
|
||||
if (len == 0) return ERR_MEM;
|
||||
|
||||
INFO_TCP_VER("left=%d len:%d\n", ttcp->left, len);
|
||||
|
||||
@ -126,19 +123,20 @@ static err_t tcp_send_data(struct ttcp *ttcp) {
|
||||
if (len > ttcp->buflen)
|
||||
len = ttcp->buflen;
|
||||
|
||||
struct tcp_pcb * pcb = GET_FIRST_CLIENT_TCP_NV(ttcp);
|
||||
/* We cannot send more data than space available in the send
|
||||
buffer. */
|
||||
if (len > tcp_sndbuf(GET_FIRST_CLIENT_TCP(ttcp)))
|
||||
len = tcp_sndbuf(GET_FIRST_CLIENT_TCP(ttcp));
|
||||
if (len > tcp_sndbuf(pcb))
|
||||
len = tcp_sndbuf(pcb);
|
||||
|
||||
orig_len = len;
|
||||
|
||||
IF_TCP(startTime = timer_get_ms());
|
||||
err = tcp_write(GET_FIRST_CLIENT_TCP(ttcp), ttcp->payload, len, TCP_WRITE_FLAG_COPY);
|
||||
err = tcp_write(pcb, ttcp->payload, len, TCP_WRITE_FLAG_COPY);
|
||||
if (err != ERR_OK)
|
||||
{
|
||||
INFO_TCP("tcp_write failed %p state:%d len:%d err:%d\n",
|
||||
GET_FIRST_CLIENT_TCP(ttcp), GET_FIRST_CLIENT_TCP(ttcp)->state, len, err);
|
||||
pcb, pcb->state, len, err);
|
||||
ttcp->buff_sent = 0;
|
||||
}else{
|
||||
ttcp->buff_sent = 1;
|
||||
@ -235,10 +233,10 @@ static void atcp_conn_cli_err_cb(void *arg, err_t err) {
|
||||
|
||||
static err_t close_conn(struct ttcp *_ttcp, struct tcp_pcb* tpcb) {
|
||||
|
||||
if (_ttcp == NULL) return;
|
||||
if (_ttcp == NULL) return ERR_MEM;
|
||||
|
||||
int8_t id = getNewClientConnId(_ttcp, tpcb);
|
||||
if (id == NO_VALID_ID) return;
|
||||
if (id == NO_VALID_ID) return ERR_MEM;
|
||||
err_t err = close_conn_pcb(_ttcp->tpcb[id]);
|
||||
|
||||
if (err == ERR_MEM)
|
||||
@ -336,13 +334,13 @@ static err_t atcp_poll(void *arg, struct tcp_pcb *pcb) {
|
||||
INFO_TCP_POLL("keepAliveCnt:%d keep_idle:%d persist_cnt:%d\n",
|
||||
pcb->keep_cnt_sent, pcb->keep_idle, pcb->persist_cnt);
|
||||
|
||||
int8_t id = getNewClientConnId(_ttcp, pcb);
|
||||
if (_ttcp->left > 0)
|
||||
INFO_TCP("ARD TCP [%p-%p] arg=%p retries=%d pend.close:%d len:%d\n",
|
||||
(_ttcp)?GET_FIRST_CLIENT_TCP(_ttcp):0, pcb, arg,
|
||||
_ttcp->tcp_poll_retries, _ttcp->pending_close, (_ttcp)?_ttcp->left:0);
|
||||
_ttcp->tcp_poll_retries, _ttcp->pending_close[id], (_ttcp)?_ttcp->left:0);
|
||||
tcp_send_data(_ttcp);
|
||||
|
||||
int8_t id = getNewClientConnId(_ttcp, pcb);
|
||||
if ((id != NO_VALID_ID) && (_ttcp->pending_close[id]))
|
||||
{
|
||||
err_t err = ERR_OK;
|
||||
@ -411,23 +409,50 @@ static err_t atcp_poll_conn(void *arg, struct tcp_pcb *pcb) {
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
int8_t currConnId = 0;
|
||||
#define GET_IDX_CONN(I) ((I+currConnId)<MAX_CLIENT_ACCEPTED ? (I+currConnId) : (I+currConnId-MAX_CLIENT_ACCEPTED))
|
||||
|
||||
int8_t getCurrClientConnId() { return currConnId;}
|
||||
|
||||
int8_t getNewClientConnId(struct ttcp* _ttcp, struct tcp_pcb *newpcb)
|
||||
{
|
||||
if (_ttcp != NULL){
|
||||
int i = 0;
|
||||
for (; i<MAX_CLIENT_ACCEPTED; ++i)
|
||||
{
|
||||
if (_ttcp->tpcb[i] == newpcb)
|
||||
int idx = GET_IDX_CONN(i);
|
||||
|
||||
if (_ttcp->tpcb[idx] == newpcb)
|
||||
{
|
||||
INFO_TCP("ttcp:%p id=%d, tpcb=%p\n", _ttcp, i, newpcb);
|
||||
return i;
|
||||
INFO_TCP("ttcp:%p id=%d, tpcb=%p\n", _ttcp, idx, newpcb);
|
||||
return idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
WARN("No Valid Id for ttcp:%p pcb:%p\n");
|
||||
WARN("No Valid Id for ttcp:%p pcb:%p\n", _ttcp, newpcb);
|
||||
return NO_VALID_ID;
|
||||
}
|
||||
|
||||
struct tcp_pcb * getFirstClient(struct ttcp* _ttcp, bool verbose)
|
||||
{
|
||||
if (_ttcp != NULL){
|
||||
int i = 0;
|
||||
for (; i<MAX_CLIENT_ACCEPTED; ++i)
|
||||
{
|
||||
int idx = GET_IDX_CONN(i);
|
||||
if (_ttcp->tpcb[idx] != NULL)
|
||||
{
|
||||
if (verbose) INFO_TCP("ttcp:%p id=%d, tpcb=%p\n", _ttcp, idx, _ttcp->tpcb[idx]);
|
||||
currConnId = idx;
|
||||
return _ttcp->tpcb[idx];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (verbose) WARN("No Valid client for ttcp:%p\n", _ttcp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int8_t setNewClientConn(struct ttcp* _ttcp, struct tcp_pcb *newpcb, uint8_t id)
|
||||
{
|
||||
if ((_ttcp != NULL)&&(id>=0)&&(id<MAX_CLIENT_ACCEPTED)){
|
||||
@ -444,11 +469,12 @@ int8_t insertNewClientConn(struct ttcp* _ttcp, struct tcp_pcb *newpcb)
|
||||
int i = 0;
|
||||
for (; i<MAX_CLIENT_ACCEPTED; ++i)
|
||||
{
|
||||
if ((_ttcp->tpcb[i] == NULL)||(_ttcp->tpcb[i] == newpcb))
|
||||
int idx = GET_IDX_CONN(i);
|
||||
if ((_ttcp->tpcb[idx] == NULL)||(_ttcp->tpcb[idx] == newpcb))
|
||||
{
|
||||
INFO_TCP("ttcp:%p id=%d, tpcb=%p\n", _ttcp, i, newpcb);
|
||||
_ttcp->tpcb[i] = newpcb;
|
||||
return i;
|
||||
INFO_TCP("ttcp:%p id=%d, tpcb=%p\n", _ttcp, idx, newpcb);
|
||||
_ttcp->tpcb[idx] = newpcb;
|
||||
return idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -461,11 +487,12 @@ int8_t removeNewClientConn(struct ttcp* _ttcp, struct tcp_pcb *newpcb)
|
||||
int i = 0;
|
||||
for (; i<MAX_CLIENT_ACCEPTED; ++i)
|
||||
{
|
||||
if (_ttcp->tpcb[i] == newpcb)
|
||||
int idx = GET_IDX_CONN(i);
|
||||
if (_ttcp->tpcb[idx] == newpcb)
|
||||
{
|
||||
INFO_TCP("ttcp:%p id=%d, tpcb=%p\n", _ttcp, i, newpcb);
|
||||
_ttcp->tpcb[i] = NULL;
|
||||
return i;
|
||||
INFO_TCP("ttcp:%p id=%d, tpcb=%p\n", _ttcp, idx, newpcb);
|
||||
_ttcp->tpcb[idx] = NULL;
|
||||
return idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -494,27 +521,7 @@ static err_t atcp_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err) {
|
||||
|
||||
INFO_TCP("ARD TCP [%p]: accept new [%p]\n", _ttcp, newpcb);
|
||||
INFO_TCP("local:%d remote:%d state:%d\n", newpcb->local_port, newpcb->remote_port, newpcb->state);
|
||||
/*
|
||||
if (pending_accept)
|
||||
{
|
||||
WARN("Accepting another connection: %p-%p\n", ttcp->tpcb, newpcb);
|
||||
return ERR_OK;
|
||||
}
|
||||
pending_accept = true;
|
||||
tcp_setprio(newpcb, TCP_PRIO_MIN);
|
||||
tcp_poll_retries = 0;
|
||||
ttcp->tpcb = newpcb;
|
||||
tcp_recv(ttcp->tpcb, atcp_recv_cb);
|
||||
tcp_err(ttcp->tpcb, atcp_conn_err_cb);
|
||||
tcp_poll(ttcp->tpcb, atcp_poll, 4);
|
||||
|
||||
_ttcp->tcp_poll_retries = 0;
|
||||
/*
|
||||
if (_ttcp->tpcb == NULL)
|
||||
{
|
||||
WARN("Replace previous tpcb=0x%x with the new one 0x%x\n", _ttcp->tpcb, newpcb);
|
||||
}
|
||||
*/
|
||||
int8_t id = insertNewClientConn(_ttcp, newpcb);
|
||||
tcp_arg(_ttcp->tpcb[id], _ttcp);
|
||||
tcp_recv(_ttcp->tpcb[id], atcp_recv_cb);
|
||||
@ -552,14 +559,15 @@ static int atcp_start(struct ttcp* ttcp) {
|
||||
|
||||
if (ttcp->mode == TTCP_MODE_TRANSMIT) {
|
||||
setNewClientConn(ttcp, p, 0);
|
||||
tcp_err(GET_FIRST_CLIENT_TCP(ttcp), atcp_conn_cli_err_cb);
|
||||
tcp_recv(GET_FIRST_CLIENT_TCP(ttcp), atcp_recv_cb);
|
||||
tcp_sent(GET_FIRST_CLIENT_TCP(ttcp), tcp_data_sent);
|
||||
tcp_poll(GET_FIRST_CLIENT_TCP(ttcp), atcp_poll_conn, 4);
|
||||
struct tcp_pcb * pcb = GET_FIRST_CLIENT_TCP(ttcp);
|
||||
tcp_err(pcb, atcp_conn_cli_err_cb);
|
||||
tcp_recv(pcb, atcp_recv_cb);
|
||||
tcp_sent(pcb, tcp_data_sent);
|
||||
tcp_poll(pcb, atcp_poll_conn, 4);
|
||||
_connected = false;
|
||||
INFO_TCP("[tpcb]-%p payload:%p\n", GET_FIRST_CLIENT_TCP(ttcp), ttcp->payload);
|
||||
INFO_TCP("[tpcb]-%p payload:%p\n", pcb, ttcp->payload);
|
||||
DUMP_TCP_STATE(ttcp);
|
||||
if (tcp_connect(GET_FIRST_CLIENT_TCP(ttcp), &ttcp->addr, ttcp->port, tcp_connect_cb)
|
||||
if (tcp_connect(pcb, &ttcp->addr, ttcp->port, tcp_connect_cb)
|
||||
!= ERR_OK) {
|
||||
WARN("TTCP [%p]: tcp connect failed\n", ttcp);
|
||||
return -1;
|
||||
@ -778,7 +786,6 @@ int ard_tcp_start(struct ip_addr addr, uint16_t port, void *opaque,
|
||||
|
||||
return 0;
|
||||
|
||||
//fail: ard_tcp_abort(ttcp);
|
||||
fail: ard_tcp_destroy(ttcp);
|
||||
return -1;
|
||||
}
|
||||
@ -797,20 +804,15 @@ void ard_tcp_stop(void* ttcp) {
|
||||
}else{
|
||||
DUMP_TCP_STATE(_ttcp);
|
||||
|
||||
int i = 0;
|
||||
for (; i<MAX_CLIENT_ACCEPTED; ++i)
|
||||
int i = getCurrClientConnId();
|
||||
if ((_ttcp)&&(_ttcp->tpcb[i])&&(_ttcp->tpcb[i]->state!=LAST_ACK)&&(_ttcp->tpcb[i]->state!=CLOSED))
|
||||
{
|
||||
if ((_ttcp)&&(_ttcp->tpcb[i])&&(_ttcp->tpcb[i]->state!=LAST_ACK)&&(_ttcp->tpcb[i]->state!=CLOSED))
|
||||
{
|
||||
// Flush all the data
|
||||
err_t err=tcp_output(_ttcp->tpcb[i]);
|
||||
INFO_TCP("flush data: tpcb:%p err:%d\n", _ttcp->tpcb[i], err);
|
||||
// if any socket cannot be close stop the close connection
|
||||
if (close_conn(_ttcp, _ttcp->tpcb[i]) != ERR_OK) break;
|
||||
}
|
||||
// Flush all the data
|
||||
err_t err=tcp_output(_ttcp->tpcb[i]);
|
||||
INFO_TCP("flush data: tpcb:%p err:%d\n", _ttcp->tpcb[i], err);
|
||||
// if any socket cannot be close stop the close connection
|
||||
close_conn(_ttcp, _ttcp->tpcb[i]);
|
||||
}
|
||||
|
||||
pending_accept = false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -819,15 +821,16 @@ uint8_t getStateTcp(void* p, bool client) {
|
||||
|
||||
if (ifStatus == false)
|
||||
return CLOSED;
|
||||
if ((_ttcp != NULL) && ((GET_FIRST_CLIENT_TCP(_ttcp) != NULL) || (client==0))) {
|
||||
struct tcp_pcb * pcb = GET_FIRST_CLIENT_TCP_NV(_ttcp);
|
||||
if ((_ttcp != NULL) && ((pcb != NULL) || (client==0))) {
|
||||
IF_SPI_POLL(DUMP_TCP_STATE(_ttcp));
|
||||
if (client)
|
||||
return GET_FIRST_CLIENT_TCP(_ttcp)->state;
|
||||
return pcb->state;
|
||||
else
|
||||
return _ttcp->lpcb->state;
|
||||
} else {
|
||||
WARN_POLL("TCP not initialized ttcp:%p tpcb:%p lpcb:%p\n",
|
||||
_ttcp, ((_ttcp)?GET_FIRST_CLIENT_TCP(_ttcp):0), ((_ttcp)?_ttcp->lpcb:0));
|
||||
_ttcp, ((_ttcp)?pcb:0), ((_ttcp)?_ttcp->lpcb:0));
|
||||
}
|
||||
return CLOSED;
|
||||
}
|
||||
@ -887,23 +890,21 @@ int sendTcpData(void* p, uint8_t* buf, uint16_t len)
|
||||
return WL_FAILURE;
|
||||
}
|
||||
|
||||
INFO_TCP_VER("ttcp:%p pcb:%p buf:%p len:%d\n", _ttcp,
|
||||
GET_FIRST_CLIENT_TCP(_ttcp), buf, len);
|
||||
struct tcp_pcb * pcb = GET_FIRST_CLIENT_TCP_NV(_ttcp);
|
||||
INFO_TCP_VER("ttcp:%p pcb:%p buf:%p len:%d\n", _ttcp, pcb, buf, len);
|
||||
DUMP_TCP(buf,len);
|
||||
IF_TCP_VER(DUMP_TCP_STATE(_ttcp));
|
||||
|
||||
if ((_ttcp != NULL) && (GET_FIRST_CLIENT_TCP(_ttcp) != NULL) &&
|
||||
if ((_ttcp != NULL) && (pcb != NULL) &&
|
||||
(buf != NULL) && (len != 0) && (_ttcp->payload != NULL)) {
|
||||
if (GET_FIRST_CLIENT_TCP(_ttcp)->state == ESTABLISHED ||
|
||||
GET_FIRST_CLIENT_TCP(_ttcp)->state == CLOSE_WAIT ||
|
||||
GET_FIRST_CLIENT_TCP(_ttcp)->state == SYN_SENT ||
|
||||
GET_FIRST_CLIENT_TCP(_ttcp)->state == SYN_RCVD) {
|
||||
if (pcb->state == ESTABLISHED || pcb->state == CLOSE_WAIT ||
|
||||
pcb->state == SYN_SENT || pcb->state == SYN_RCVD) {
|
||||
|
||||
memcpy(_ttcp->payload, buf, len);
|
||||
_ttcp->payload[len]='\0';
|
||||
INFO_TCP_VER("'%s'\n", _ttcp->payload);
|
||||
_ttcp->left = len;
|
||||
tcp_sent(GET_FIRST_CLIENT_TCP(_ttcp), tcp_data_sent);
|
||||
tcp_sent(pcb, tcp_data_sent);
|
||||
tcp_send_data(_ttcp);
|
||||
|
||||
return WL_SUCCESS;
|
||||
|
@ -24,7 +24,9 @@ typedef void (ard_tcp_done_cb_t)(void *opaque, int result);
|
||||
// Maximum number of client connection accepted by server
|
||||
#define MAX_CLIENT_ACCEPTED 4
|
||||
#define NO_VALID_ID -1
|
||||
#define GET_FIRST_CLIENT_TCP(TTCP) ((TTCP!=NULL)?TTCP->tpcb[0] : NULL)
|
||||
|
||||
#define GET_FIRST_CLIENT_TCP(TTCP) getFirstClient(TTCP, 1)
|
||||
#define GET_FIRST_CLIENT_TCP_NV(TTCP) getFirstClient(TTCP, 0)
|
||||
#define GET_CLIENT_TCP(TTCP,ID) (((TTCP!=NULL)&&(ID>=0)&&(ID<MAX_CLIENT_ACCEPTED))?TTCP->tpcb[ID] : NULL)
|
||||
|
||||
|
||||
@ -92,6 +94,10 @@ bool cleanNewClientConn(struct ttcp* _ttcp);
|
||||
|
||||
int8_t getNewClientConnId(struct ttcp* _ttcp, struct tcp_pcb *newpcb);
|
||||
|
||||
int8_t getCurrClientConnId();
|
||||
|
||||
struct tcp_pcb * getFirstClient(struct ttcp* _ttcp, bool verbose);
|
||||
|
||||
void closeConnections();
|
||||
|
||||
#endif
|
||||
|
@ -181,7 +181,8 @@ void dumpPbuf(uint8_t sock)
|
||||
do {
|
||||
if (pBufStore[index][sock].data != NULL)
|
||||
{
|
||||
printk("Buf: %p Len:%d\n", pBufStore[index][sock].data, pBufStore[index][sock].len);
|
||||
printk("%d] pcb:%p Buf: %p Len:%d\n", pBufStore[index][sock].idx, pBufStore[index][sock].pcb,
|
||||
pBufStore[index][sock].data, pBufStore[index][sock].len);
|
||||
}
|
||||
++index;
|
||||
if (index == MAX_PBUF_STORED)
|
||||
@ -247,7 +248,7 @@ void ack_recved(void* pcb, int len);
|
||||
|
||||
void ackAndFreeData(void* pcb, int len, uint8_t sock, uint8_t* data)
|
||||
{
|
||||
INFO_UTIL("Ack pcb:%p len:%d sock:%d data:%p\n", pcb, len, sock, data);
|
||||
INFO_TCP("Ack pcb:%p len:%d sock:%d data:%p\n", pcb, len, sock, data);
|
||||
if (!IS_UDP_SOCK(sock))
|
||||
ack_recved(pcb, len);
|
||||
if (data != NULL)
|
||||
@ -278,7 +279,7 @@ bool isAvailTcpDataByte(uint8_t sock)
|
||||
uint16_t getAvailTcpDataByte(uint8_t sock)
|
||||
{
|
||||
uint16_t len = calcMergeLen(sock);
|
||||
INFO_UTIL("Availabled data: %d\n", len);
|
||||
INFO_UTIL_VER("Availabled data: %d\n", len);
|
||||
return len;
|
||||
}
|
||||
|
||||
@ -298,6 +299,8 @@ bool getTcpDataByte(uint8_t sock, uint8_t* payload, uint8_t peek)
|
||||
else
|
||||
*payload = buf[p->idx++];
|
||||
INFO_UTIL_VER("get:%d %p %d\n",p->idx, p->data, *payload);
|
||||
if (p->idx == p->len)
|
||||
ackAndFreeData(p->pcb, p->len, sock, p->data);
|
||||
return true;
|
||||
}else{
|
||||
ackAndFreeData(p->pcb, p->len, sock, p->data);
|
||||
|
@ -281,6 +281,7 @@ void initShell()
|
||||
console_add_cmd("status", cmd_status, NULL);
|
||||
console_add_cmd("debug", cmd_debug, NULL);
|
||||
console_add_cmd("dumpBuf", cmd_dumpBuf, NULL);
|
||||
console_add_cmd("ipconfig", cmd_set_ip, NULL);
|
||||
|
||||
#ifdef ADD_CMDS
|
||||
console_add_cmd("powersave", cmd_power, NULL);
|
||||
|
Loading…
x
Reference in New Issue
Block a user