Marine systems simulation
Loading...
Searching...
No Matches
CTCP_client.h
1#ifndef CTCP_C_H
2#define CTCP_C_H
3
4#include <string>
5#include "SimObject.h"
6#include <Eigen/Eigen>
7#include <CPrintDuringExec.h>
8
9#ifdef _WIN32 // Windows
10#include <winsock2.h>
11#else // Linux
12#include <sys/types.h>
13#include <sys/socket.h>
14#include <netinet/in.h>
15#include <arpa/inet.h>
16#include <netdb.h>
17#include <unistd.h>
18#include <errno.h>
19#define SOCKET int
20#endif
21
22
23namespace Interface {
24
77class CTCP_client : public SimObject
78{
79public:
80 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
81
82 // Constructor
83 CTCP_client(std::string sSimObjectName, ISimObjectCreator* pCreator);
84 virtual void FinalSetup(const double dT, const double* const adX, ISimObjectCreator* const pCreator);
85 virtual void InitialConditionSetup(const double T, const double *const currentIC, double* updatedIC, ISimObjectCreator* creator);
86 virtual ~CTCP_client();
87
88 // Calculate the state derivatives.
89 virtual void OdeFcn(const double dT, const double* const adX, double* const adXDot, const bool bIsMajorTimeStep);
90
91 // Set up a tcp connection for a spesific IP address and port number
92 bool SetupTCP(const std::string& IPString, const int& port);
93
94 // Send a tcp message
95 bool SendMsg(const double dT, const double* const adX);
96
97 // Receive a tcp message
98 bool ReceiveMsg();
99
100 // Receive a tcp message in multiple rounds
101 bool ReceiveMsgMR();
102
103#ifdef FH_VISUALIZATION
104 // Initial specification of the simObject geometry for rendering.
105 virtual void RenderInit(Ogre::Root* const ogreRoot, ISimObjectCreator* const creator) {};
106
107 // Updating the simObject for rendering the next frame.
108 virtual void RenderUpdate(const double dT, const double* const adX) {};
109#endif
110
111protected:
112 // FhSim logger
113 CPrintDuringExec* m_Logger;
114
115 // Input ports
116 ISignalPort* m_InSig;
117 // Number of messages to send
118 int m_InNum;
119 // Input buffer
120 double* m_InMsg;
121
122 // Output ports
123 virtual const double* OutMsg(const double dT, const double* const adX);
124 // Number of messages to receive
125 int m_OutNum;
126 // Output buffer
127 double* m_OutMsg;
128
129 // Buffer for sending and receiving messages
130 char* m_MsgBuf;
131
132 // Server information
133 std::string m_TCPName;
134 std::string m_IPString;
135 int m_PortNum;
136
137 // Socket information
138#ifdef _WIN32
139 WSADATA m_WsaData;
140#endif
141 SOCKET m_ConnectSocket;
142
143 // To check if sending and reveiving messages successfully
144 bool m_TCP;
145
146 // Number of Bytes sent and received
147 int m_RecResult;
148 int m_SendResult;
149
150 // Options
151 bool m_RecMR; // True to receive a tcp message in multiple rounds
152 bool m_ShowTCPRst; // True to show TCP results on the screen
153 double m_ComDT; // Communication interval
154 double m_ComNT; // Time for next communication
155};
156
157};
158
159#endif
Definition CTCP_client.h:78