embkernel
 All Classes Functions Variables Typedefs Groups Pages
NetTcpSocket.hpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
5 
6 #ifndef NET_TCP_SOCKET_HPP_
7 #define NET_TCP_SOCKET_HPP_
8 
9 #include "NetDefs.hpp"
10 #include "LibLinkedList.hpp"
11 #include "LibStreamOut.hpp"
12 #include "LibStreamIn.hpp"
13 #include "RtosSemaphore.hpp"
14 #include "RtosMsgBox.hpp"
15 #include "LibStringFormat.hpp"
16 
17 class NetTcpSocket: public LIB_LINKED_LIST_ITERABLE<NetTcpSocket>, public LibStreamIn, public LibStreamOut {
18  friend class NetTcp;
19 
20 private:
21  static uint32_t sStartSequenceCounter;
22  static uint16_t sAutomaticPort;
23 
24  typedef enum
25  :uint8_t {
26  STATE_CLOSED = 0,
27  STATE_LISTEN,
28  STATE_SYN_SENT,
29  STATE_SYN_RCVD,
30  STATE_EST,
31  STATE_CLOSE_WAIT,
32  STATE_LAST_ACK,
33  STATE_FIN_WAIT_1,
34  STATE_FIN_WAIT_2,
35  STATE_CLOSING,
36  STATE_TIME_WAIT,
37  STATE_RESET
38  } STATE;
39 
40  typedef struct {
41  uint16_t dataLen;
42  uint16_t index;
43  }__attribute__((packed)) RX_SEGMENT_HEADER;
44 
45  typedef struct {
46  RX_SEGMENT_HEADER hdr;
47  uint8_t data[NetDefs::TCP_MAX_DATA_LEN]; //
48  }__attribute__((packed)) RX_SEGMENT;
49 
50  typedef struct {
51  uint32_t seq;
52  uint32_t dataChecksum;
53  uint16_t len;
54  uint16_t maxLen;
55  uint8_t timeout;
56  uint8_t retries;
57  uint8_t flushed;
58  uint8_t spare;
59  } TX_SEGMENT_HEADER;
60 
61  typedef struct tagTX_SEGMENT {
62  tagTX_SEGMENT* next;
63  TX_SEGMENT_HEADER hdr;
64  NetDefs::FRAME frame; //
65  }__attribute__((packed)) TX_SEGMENT;
66 
67 public:
68  NetTcpSocket();
69  virtual ~NetTcpSocket();
70 
71  bool connect(NetDefs::IP_ADDR& remoteIpAddr, uint16_t remotePort, uint16_t localPort = 0);
72  void listen(NetDefs::TCP_PORT localPort);
73  bool accept(Rtos::TICK timeout);
74  void autoListenAndAccept(NetDefs::TCP_PORT localPort);
75  bool isConnected();
76  void close();
77  void setWindowSize(uint16_t windowsSize);
78  void setMaxTxSegmentDataLen(uint16_t maxTxSegmentDataLen);
79  void flush();
80  void discardAllTxSegments();
81  void discardAllRxSegments();
82 
83  virtual int read(void* buffer, int len, Rtos::TICK timeout = Rtos::TICK_INFINITE);
84  virtual int skip(int len, Rtos::TICK timeout = Rtos::TICK_INFINITE);
85  virtual int write(const void* buffer, int len, Rtos::TICK timeout = Rtos::TICK_INFINITE);
86 private:
87  static const int MAX_QUEUED_TX_SEGMENTS = 8;
88  static const size_t MAX_TX_FRAGMENT_DATA_SIZE = 512;
89  NetDefs::TCP_PORT mLocalPort;
90  NetDefs::TCP_PORT mRemotePort;
91  NetDefs::IP_ADDR mRemoteIpAddr;
92  NetDefs::MAC_ADDR mRemoteMacAddr;
93  uint32_t mRecvCount;
94  uint32_t mSentCount;
95  uint16_t mWindowSize;
96  uint16_t mMaxTxSegmentDataLen;
97 
98  RX_SEGMENT* mRxCurrentSegment;
99  TX_SEGMENT* mTxFirstSegment;
100  TX_SEGMENT* mTxCurrentSegment;
101 
102  RtosSemaphore mSemaEvent;
103  RtosMsgBox<RX_SEGMENT*, 4> mRxMsgBox;
104 
105  uint16_t mTimer;
106  uint8_t mRetries;
107  STATE mState;
108 
109  bool mIsRxDataFinished;
110  bool mIsAutoListenAndAccept;
111 };
112 
113 #endif /* NET_TCP_SOCKET_HPP_ */