Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
tcp/tcb.c@61:aad055f1b0d1, 2018-01-11 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Jan 11 17:38:21 2018 +0000
- Revision:
- 61:aad055f1b0d1
- Parent:
- tcp/tcb.cpp@57:e0fb648acf48
- Child:
- 65:37acccf2752f
Removed dependence on Mbed OS
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| andrewboyson | 61:aad055f1b0d1 | 1 | #include <stdint.h> |
| andrewboyson | 61:aad055f1b0d1 | 2 | |
| andrewboyson | 52:fbc5a46b5e16 | 3 | #include "log.h" |
| andrewboyson | 52:fbc5a46b5e16 | 4 | #include "tcp.h" |
| andrewboyson | 52:fbc5a46b5e16 | 5 | #include "tcb.h" |
| andrewboyson | 52:fbc5a46b5e16 | 6 | #include "clock.h" |
| andrewboyson | 52:fbc5a46b5e16 | 7 | |
| andrewboyson | 10:f0854784e960 | 8 | #define TCB_COUNT 10 |
| andrewboyson | 10:f0854784e960 | 9 | |
| andrewboyson | 10:f0854784e960 | 10 | #define TIMEOUT_SYN_RECEIVED 2 |
| andrewboyson | 10:f0854784e960 | 11 | #define TIMEOUT_ESTABLISHED 20 |
| andrewboyson | 10:f0854784e960 | 12 | #define TIMEOUT_CLOSING 2 |
| andrewboyson | 10:f0854784e960 | 13 | |
| andrewboyson | 10:f0854784e960 | 14 | struct tcb tcbs[TCB_COUNT]; |
| andrewboyson | 10:f0854784e960 | 15 | |
| andrewboyson | 10:f0854784e960 | 16 | uint32_t TcbGetIsn() |
| andrewboyson | 10:f0854784e960 | 17 | { |
| andrewboyson | 10:f0854784e960 | 18 | static uint32_t isn = 0; |
| andrewboyson | 10:f0854784e960 | 19 | isn += 100000; //Gives each tcb 100,000 packets and won't repeat before 42,940 tcbs. |
| andrewboyson | 10:f0854784e960 | 20 | return isn; |
| andrewboyson | 10:f0854784e960 | 21 | } |
| andrewboyson | 10:f0854784e960 | 22 | struct tcb* TcbGetExisting(uint16_t port) |
| andrewboyson | 10:f0854784e960 | 23 | { |
| andrewboyson | 10:f0854784e960 | 24 | for (int i = 0; i < TCB_COUNT; i++) |
| andrewboyson | 10:f0854784e960 | 25 | { |
| andrewboyson | 10:f0854784e960 | 26 | struct tcb* pTcb = tcbs + i; |
| andrewboyson | 57:e0fb648acf48 | 27 | if (pTcb->state != TCB_EMPTY && pTcb->port == port) return pTcb; |
| andrewboyson | 10:f0854784e960 | 28 | } |
| andrewboyson | 10:f0854784e960 | 29 | return NULL; |
| andrewboyson | 10:f0854784e960 | 30 | } |
| andrewboyson | 10:f0854784e960 | 31 | struct tcb* TcbGetEmpty() |
| andrewboyson | 10:f0854784e960 | 32 | { |
| andrewboyson | 10:f0854784e960 | 33 | for (int i = 0; i < TCB_COUNT; i++) |
| andrewboyson | 10:f0854784e960 | 34 | { |
| andrewboyson | 10:f0854784e960 | 35 | struct tcb* pTcb = tcbs + i; |
| andrewboyson | 57:e0fb648acf48 | 36 | if (pTcb->state == TCB_EMPTY) return pTcb; |
| andrewboyson | 10:f0854784e960 | 37 | } |
| andrewboyson | 10:f0854784e960 | 38 | return NULL; |
| andrewboyson | 10:f0854784e960 | 39 | } |
| andrewboyson | 10:f0854784e960 | 40 | |
| andrewboyson | 52:fbc5a46b5e16 | 41 | uint32_t TcbElapsed = 0; |
| andrewboyson | 52:fbc5a46b5e16 | 42 | static void reap() |
| andrewboyson | 10:f0854784e960 | 43 | { |
| andrewboyson | 52:fbc5a46b5e16 | 44 | static struct tcb* pTcb = tcbs; |
| andrewboyson | 52:fbc5a46b5e16 | 45 | |
| andrewboyson | 57:e0fb648acf48 | 46 | if (pTcb->state == TCB_EMPTY) return; |
| andrewboyson | 52:fbc5a46b5e16 | 47 | |
| andrewboyson | 52:fbc5a46b5e16 | 48 | uint32_t limit; |
| andrewboyson | 52:fbc5a46b5e16 | 49 | switch (pTcb->state) |
| andrewboyson | 10:f0854784e960 | 50 | { |
| andrewboyson | 52:fbc5a46b5e16 | 51 | case TCB_SYN_RECEIVED: limit = TIMEOUT_SYN_RECEIVED; break; |
| andrewboyson | 52:fbc5a46b5e16 | 52 | case TCB_ESTABLISHED: limit = TIMEOUT_ESTABLISHED; break; |
| andrewboyson | 57:e0fb648acf48 | 53 | case TCB_FIN_WAIT: limit = TIMEOUT_CLOSING; break; |
| andrewboyson | 57:e0fb648acf48 | 54 | case TCB_ACK_WAIT: limit = TIMEOUT_CLOSING; break; |
| andrewboyson | 52:fbc5a46b5e16 | 55 | } |
| andrewboyson | 52:fbc5a46b5e16 | 56 | |
| andrewboyson | 52:fbc5a46b5e16 | 57 | if (TcbElapsed - pTcb->elapsed > limit) |
| andrewboyson | 52:fbc5a46b5e16 | 58 | { |
| andrewboyson | 52:fbc5a46b5e16 | 59 | if (TcpTrace) LogTimeF("Reaping TCB %d port %d\r\n", pTcb - tcbs, pTcb->port); |
| andrewboyson | 57:e0fb648acf48 | 60 | pTcb->state = TCB_EMPTY; |
| andrewboyson | 52:fbc5a46b5e16 | 61 | } |
| andrewboyson | 52:fbc5a46b5e16 | 62 | |
| andrewboyson | 52:fbc5a46b5e16 | 63 | pTcb++; |
| andrewboyson | 52:fbc5a46b5e16 | 64 | if (pTcb >= tcbs + TCB_COUNT) pTcb = tcbs; |
| andrewboyson | 52:fbc5a46b5e16 | 65 | } |
| andrewboyson | 52:fbc5a46b5e16 | 66 | |
| andrewboyson | 52:fbc5a46b5e16 | 67 | void TcbMain() |
| andrewboyson | 52:fbc5a46b5e16 | 68 | { |
| andrewboyson | 52:fbc5a46b5e16 | 69 | if (ClockTicked) TcbElapsed++; |
| andrewboyson | 52:fbc5a46b5e16 | 70 | reap(); |
| andrewboyson | 10:f0854784e960 | 71 | } |
| andrewboyson | 10:f0854784e960 | 72 | void TcbInit() |
| andrewboyson | 10:f0854784e960 | 73 | { |
| andrewboyson | 57:e0fb648acf48 | 74 | for (int i = 0; i < TCB_COUNT; i++) tcbs[i].state = TCB_EMPTY; |
| andrewboyson | 52:fbc5a46b5e16 | 75 | } |