📄 tcpnet.h
字号:
// CTCPReceivedData - Buffer for storing received data and information about it
// CTCPErrorInfo - Buffer for storing error information
// CTCPNet - Class, which implements TCP
// Declaration file
//
// (c) Lev Naumov, CAMEL Laboratory
// E-mail: camellab@mail.ru
// For more information see http://camel.ifmo.ru or
// http://www.codeproject.com/internet/ctp.asp
/////////////////////////////////////////////////////////////////////////////
struct CTCPReceivedData {
// Constructor. Parameters:
// +) size - amount of data to be stored;
// +) from - ip address of host, that sends this data;
// +) buf - points to buffer, which stores received data. If NULL then
// data copying will be skipped (only allocation performs)
CTCPReceivedData(unsigned __int64 size, unsigned long from, char* buf) {this->size=size; this->from=IPAddr(from); pBuf=new char[(unsigned int)size]; if (buf) memcpy(pBuf,buf,(unsigned int)size);};
// Destructor
virtual ~CTCPReceivedData() {delete[] pBuf;};
unsigned __int64 size; // Message size (48 bit)
IPAddr from; // Host, that had sent this data
char* pBuf; // Data
};
struct CTCPErrorInfo {
// Constructor. Parameters:
// +) type - error type. When it occurs:
// +) 0 - on socket creation;
// +) 1 - on socket binding or tuning;
// +) 2 - on data sending;
// +) 3 - on data receiving;
// +) code - WinSock error code;
// +) addr - address of host, which causes error (not always can be
// interpreted, if can not the just equals localhost)
CTCPErrorInfo(unsigned char type,int code,IPAddr addr) {this->type=type; this->code=code; this->addr=addr; GetTimeStamp(timestamp);};
// Put time stamp to string s and returns it
static char* GetTimeStamp(char* s);
unsigned char type; // Error type
int code; // WinSock error code
IPAddr addr; // Address of host, which causes error
char timestamp[22]; // Time stamp, when error occurred
};
class CTCPNet: public NetSender
{
public:
// Constructor creates server with all necessary parameter and tunes client
// for fast data sending:
// +) receiver - receiver of arrived data and errors;
// +) port - number of port, which to listen;
// +) maxthreads - maximum amount of server deliverers threads, that can be
// put in single packet. If message is bigger than it is the large
// message
CTCPNet(NetReceiver* receiver,unsigned short port,unsigned short maxthreads=50);
// Destructor
virtual ~CTCPNet();
// Structures for messages and error information delivery
enum DeliveryType {
ReceivedData,
ErrorInfo
};
struct Delivery {
// Constructor
Delivery(CTCPErrorInfo* data) {this->data=data; this->type=DeliveryType::ErrorInfo;};
Delivery(CTCPReceivedData* data) {this->data=data; this->type=DeliveryType::ReceivedData;};
Delivery() {data=NULL; type=(DeliveryType)NULL;}; // Only for STL compliance
void* data; // Data
DeliveryType type; // Delivery type
};
typedef list<Delivery> DeliveriesList;
// Parameter access routines
// Port routines
unsigned short GetPort() {return m_uPort;}
void SetPort(unsigned short port) {closesocket(m_SendSocket); closesocket(m_RecvSocket); m_uPort=port; CreateSockets();}
// Maximal threads amount routines
void SetMaxDeliverers(unsigned short maxthreads) {m_uMaxDeliverers=maxthreads;};
unsigned short GetMaxDeliverers() {return m_uMaxDeliverers;};
// Info target routines
NetReceiver* GetDefaultReceiver() {return m_DefReceiver;}
void SetDefaultReceiver(NetReceiver* receiver) {m_DefReceiver=receiver;}
// Suspending status routines
bool GetSuspended() {return m_bSuspended;};
void SetSuspended(bool suspended) {m_bSuspended=suspended;};
// Operations
// Send smart buffer sb to address to. Parameter command represents command
// id. Parameter options - sending options. If parameter storeiffail is
// true then sent command will be stored in sent packets storage even if
// sending fails and will not otherwise. Returns true if succeeded (message
// has gone) and false otherwise
virtual bool Send(SmartBuffer& sb, unsigned __int16 command, IPAddr to, unsigned __int8 options=0, bool storeiffail=true);
protected:
// Free all planned deliveries
inline void FreeDeliveries();
// Creates sending and receiving sockets. Returns true if succeeded and
// false otherwise
bool CreateSockets();
public:
// Socket for data receiving
SOCKET m_RecvSocket;
// Receiving buffer
char* m_pBuffer;
// Equals true if server and delivery threads needs to be finished
bool m_bKill;
// Maximal amount of delivery threads
unsigned short m_uMaxDeliverers;
// Deliveries (received messages and error information)
DeliveriesList m_Deliveries;
// Threads handles
CWinThread* m_pServerTrd; // Main servers thread
CWinThread* m_pDelManTrd; // Delivery manager thread
list<CWinThread*> m_pDeliverTrds; // Deliverers threads
// Critical section for delivery threads
CCriticalSection m_CriticalSection;
// Stores amount of busy deliverers threads
unsigned short m_uBusy;
protected:
// Points to receiver which will get messages and error information by
// default (if no special receiver will be present)
NetReceiver* m_DefReceiver;
// Socket for data sending
SOCKET m_SendSocket;
// Local addree, used for data receiving
SOCKADDR_IN m_Local;
// Port on wich to work;
unsigned short m_uPort;
// Determines if this workstation is offline or not
bool m_bSuspended;
};
// Main server threads function
unsigned int TCPServerFunction(void* pNet);
// Deliveries managers threads function
unsigned int TCPDelManFunction(void* pNet);
// Deliverers threads function
unsigned int TCPDeliverFunction(void* pNet);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -