This repository has been archived on 2025-02-01. You can view files and clone it, but cannot push or open issues or pull requests.
reprapfirmware-dc42/Network.h
David Crocker db3f5adda9 Tidied up http/lwip interface
Removed file httpd.c and moved its functions into Network.cpp. This
gives us type-checking across more interface boundaries and eliminates a
number of global functions. Also removed unused files fs.c, fsdata.c,
httpd-original.c.
2014-04-07 13:10:39 +01:00

119 lines
3.1 KiB
C++

/****************************************************************************************************
RepRapFirmware - Network: RepRapPro Ormerod with Duet controller
Separated out from Platform.h by dc42
****************************************************************************************************/
#ifndef NETWORK_H
#define NETWORK_H
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <limits.h>
// Platform-specific includes
#include "Arduino.h"
#include "ethernet_sam.h"
// This class handles the network - typically an Ethernet.
// The size of the http output buffer is critical to getting fast load times in the browser.
// If this value is less than the TCP MSS, then Chrome under Windows will delay ack messages by about 120ms,
// which results in very slow page loading. Any value higher than that will cause the TCP packet to be split
// into multiple transmissions, which avoids this behaviour. Using a value of twice the MSS is most efficient because
// each TCP packet will be full.
// Currently we set the MSS (in file network/lwipopts.h) to 1432 which matches the value used by most versions of Windows
// and therefore avoids additional memory use and fragmentation.
const unsigned int httpOutputBufferSize = 2 * 1432;
// HttpState structure that we use to track Http connections. This could be combined with class RequestState.
struct HttpState
{
// Receive fields
struct pbuf *pb;
// Transmit fields
char *file;
uint16_t left;
uint8_t retries;
bool SendInProgress() const { return left > 0; }
};
// Start with a class to hold input and output from the network that needs to be responded to.
class RequestState
{
public:
friend class Network;
protected:
RequestState(RequestState* n);
void Set(const char* d, int l, void* pc, HttpState* h);
bool Read(char& b);
void SentPacketAcknowledged();
void Write(char b);
void Write(const char* s);
bool Close();
bool TrySendData();
void SetConnectionLost();
bool LostConnection() const;
private:
void Reset();
void* pcb;
HttpState* hs;
RequestState* next;
const char* inputData;
int inputLength;
int inputPointer;
uint8_t sentPacketsOutstanding; // count of TCP packets we have sent that have not been acknowledged
char outputBuffer[httpOutputBufferSize];
int outputPointer;
bool closePending;
FileStore *fileBeingSent;
float closeRequestedTime;
};
// The main network class that drives the network.
class Network
{
public:
void ReceiveInput(const char* data, int length, void* pc, HttpState* h);
void InputBufferReleased(HttpState *hs, void* pb);
void SentPacketAcknowledged(HttpState *hs);
void ConnectionError(HttpState* h);
bool Active() const;
bool LinkIsUp();
bool Read(char& b);
void Write(char b);
void Write(const char* s);
void SendAndClose(FileStore *f);
bool HaveData() const;
Network();
void Init();
void Spin();
private:
void AppendTransaction(RequestState** list, RequestState *r);
RequestState *freeTransactions;
RequestState *readyTransactions;
RequestState *writingTransactions;
RequestState *closingTransactions;
bool active;
};
#endif