Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Socket.h « DuetEthernet « DuetNG « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 96154a53de096d918a230ca6079790757c9ae119 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * Socket.h
 *
 *  Created on: 25 Dec 2016
 *      Author: David
 */

#ifndef SRC_DUETNG_DUETETHERNET_SOCKET_H_
#define SRC_DUETNG_DUETETHERNET_SOCKET_H_

#include "RepRapFirmware.h"
#include "NetworkDefs.h"

// Socket structure that we use to track TCP connections
class Socket
{
public:
	Socket();
	void Init(SocketNumber s, Port serverPort);
	void Poll(bool full);
	Port GetLocalPort() const { return localPort; }
	uint32_t GetRemoteIP() const { return remoteIPAddress; }
	Port GetRemotePort() const { return remotePort; }
	bool IsConnected() const;
	bool IsTerminated() const { return isTerminated; }
	void Close();
	void Terminate();
	SocketNumber GetNumber() const { return socketNum; }
	NetworkTransaction *GetTransaction() const { return currentTransaction; }
	bool ReadChar(char& c);
	bool ReadBuffer(const char *&buffer, size_t &len);
	bool HasMoreDataToRead() const;
	void ReleaseTransaction();
	bool IsPersistentConnection() const { return persistConnection; }
	bool CanWrite() const;
	void DiscardReceivedData();
	bool AcquireTransaction();

private:
	enum class SocketState : uint8_t
	{
		inactive,
		listening,
		connected,
		clientDisconnecting,
		closing
	};

	void ReInit();
	bool IsSending() const;								// Return true if we are in the sending phase

	bool TrySendData()									// Try to send data, returning true if all data has been sent and we ought to close the socket
	pre(IsSending());

	Port localPort, remotePort;							// The local and remote ports
	uint32_t remoteIPAddress;							// The remote IP address
	NetworkTransaction *currentTransaction;				// The transaction currently being processed on this socket
	NetworkBuffer *receivedData;						// List of buffers holding received data
	bool persistConnection;								// Do we expect this connection to stay alive?
	bool isTerminated;									// Will be true if the connection has gone down unexpectedly (TCP RST)
	SocketNumber socketNum;								// The W5500 socket number we are using
	SocketState state;
	bool isSending;										// True if we have written data to the W5500 to send and have not yet seen success or timeout
};

#endif /* SRC_DUETNG_DUETETHERNET_SOCKET_H_ */