OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
TstC4NetIO.cpp File Reference
#include "C4Include.h"
#include "network/C4NetIO.h"
#include "lib/C4Random.h"
#include <iostream>
#include <sstream>
#include <time.h>
#include <arpa/inet.h>
Include dependency graph for TstC4NetIO.cpp:

Go to the source code of this file.

Classes

class  MyCBClass
 

Macros

#define ASYNC_CONNECT
 
#define USE_UDP
 

Functions

int main (int argc, char *argv[])
 

Variables

bool fHost
 
int iCnt = 0
 
int iSize = 0
 
char DummyData [1024 *1024]
 

Macro Definition Documentation

#define ASYNC_CONNECT

Definition at line 38 of file TstC4NetIO.cpp.

#define USE_UDP

Definition at line 41 of file TstC4NetIO.cpp.

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 98 of file TstC4NetIO.cpp.

References C4NetIOMan::AddIO(), C4NetIOTCP::Close(), C4NetIOTCP::Connect(), DummyData, fHost, FixedRandom(), C4NetIO::GetError(), iCnt, C4NetIOTCP::Init(), C4NetIOTCP::InitBroadcast(), iSize, C4NetIO::Send(), StdSchedulerThread::Start(), and StdSchedulerThread::Stop().

99 {
100 
101  int i;
102  for (i = 0; i < sizeof(DummyData); i++)
103  DummyData[i] = 'A' + i % 100;
104 
105  FixedRandom(time(nullptr));
106 
107 #ifdef USE_UDP
108  C4NetIOUDP NetIO;
109 #else
110  C4NetIOTCP NetIO;
111 #endif
112 
113  C4NetIO *pNetIO = &NetIO;
114  MyCBClass CBClass;
115  CBClass.AddIO(pNetIO);
116 
117 #ifdef HAVE_WINSOCK
118  WSADATA wsaData;
119 
120  WSAStartup(0x0202, &wsaData);
121 #endif
122 
123  C4NetIO::addr_t addr;
124  // Default
125  addr.sin_addr.s_addr = inet_addr("127.0.0.1");
126  addr.sin_port = htons(11111);
127  addr.sin_family = AF_INET;
128  short iPort = 0;
129 
130  for (i = 1; i < argc; ++i)
131  {
132  std::string arg(argv[i]);
133  int n;
134  if (arg == "--server")
135  {
136  fHost = true;
137  if (!iPort) iPort = 11111;
138  }
139  else if ((n = arg.find("--port=")) != -1)
140  {
141  std::istringstream stream(std::string(arg.begin() + n + sizeof("--port="), arg.end()));
142  stream >> iPort;
143  }
144  else if ((n = arg.find("--size=")) != -1)
145  {
146  std::istringstream stream(std::string(arg.begin() + n + sizeof("--size="), arg.end()));
147  stream >> iSize;
148  }
149  else
150  {
151  if (!ResolveAddress(argv[i], &addr, 11111)) cout << "Fehler in ResolveAddress(" << argv[i] << ")" << std::endl;
152  if (!iPort) iPort = 11112;
153  }
154  }
155  if (argc == 1)
156  {
157 #ifndef _WIN32
158  cout << "Possible usage: " << argv[0] << " [--server] [address[:port]] --port=port --size=size" << std::endl << std::endl;
159 #endif
160 
161  cout << "Server? (j/n)";
162  char cChoice;
163  do
164  cin >> cChoice;
165  while (tolower(cChoice) != 'n' && tolower(cChoice) != 'j');
166 
167  fHost = (tolower(cChoice) == 'j');
168 
169  if (cChoice == 'J' || cChoice == 'N')
170  {
171  iPort = (cChoice == 'J' ? 11111 : 11112);
172  iCnt = 0;
173  iSize = 0;
174  }
175  else
176  {
177  cout << "Port?";
178  cin >> iPort;
179 
180  cout << "Dummys?";
181  do
182  cin >> iCnt;
183  while (iCnt < 0);
184 
185  cout << "Größe?";
186  do
187  cin >> iSize;
188  while (iSize < 1 || iSize > 1024);
189  }
190  if (!fHost)
191  {
192  char szAddr[1024];
193  int iDPort;
194  if (cChoice == 'N')
195  {
196  strcpy(szAddr, "127.0.0.1");
197  iDPort = 11111;
198  }
199  else
200  {
201  cout << "Wohin (Adresse)? ";
202  cin >> szAddr;
203 
204  cout << "Wohin (Port)? ";
205  cin >> iDPort;
206  }
207  addr.sin_addr.s_addr = inet_addr(szAddr);
208  addr.sin_port = htons(iDPort);
209  addr.sin_family = AF_INET;
210  }
211  }
212  cout << "\nC4NetIO Init...";
213 
214  if (!NetIO.Init(iPort))
215  {
216  cout << " Fehler: " << NetIO.GetError() << endl;
217  return 0;
218  }
219 
220  cout << " ok" << endl;
221 
222 
223  if (fHost)
224  {
225 
226  cout << " Broadcast...";
227  C4NetIO::addr_t addr;
228  if (!NetIO.InitBroadcast(&addr))
229  {
230  cout << " Fehler: " << NetIO.GetError() << endl;
231  return 0;
232  }
233 
234  cout << " ok" << endl;
235 
236  cout << " Thread...";
237 
238  if (!CBClass.Start())
239  {
240  cout << " Fehler!" << endl;
241  return 0;
242  }
243 
244  cout << " ok" << endl;
245 
246  cout << "listening... " << endl;
247 
248  }
249  else
250  {
251  cout << " Thread...";
252 
253  if (!CBClass.Start())
254  {
255  cout << " Fehler!" << endl;
256  return 0;
257  }
258 
259  cout << " ok" << endl;
260 
261  cout << "verbinden mit " << inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port) << "...\n";
262 #ifdef ASYNC_CONNECT
263  NetIO.Connect(addr);
264 #else
265  if (!fHost)
266  {
267  int iBufLen = 5000;
268  char *pBuf = new char [iBufLen];
269  for (int i = 0; i < 10; i++)
270  {
271  *pBuf = i;
272  if (!pNetIO->Send(C4NetIOPacket(pBuf, iBufLen, true, addr)))
273  cout << " Fehler: " << (pNetIO->GetError() ? pNetIO->GetError() : "bla") << endl;
274  }
275  }
276 #endif
277  }
278 
279  while (std::cin.get() == 10);
280 
281  CBClass.Stop();
282  NetIO.Close();
283 
284  return 1;
285 }
bool fHost
Definition: TstC4NetIO.cpp:34
virtual bool Connect(const addr_t &addr)
Definition: C4NetIO.cpp:1167
virtual const char * GetError() const
Definition: C4NetIO.h:285
virtual bool InitBroadcast(addr_t *pBroadcastAddr)
Definition: C4NetIO.cpp:864
int iCnt
Definition: TstC4NetIO.cpp:35
virtual bool Close()
Definition: C4NetIO.cpp:870
char DummyData[1024 *1024]
Definition: TstC4NetIO.cpp:36
virtual bool Send(const class C4NetIOPacket &rPacket)=0
virtual bool Init(uint16_t iPort=addr_t::IPPORT_NONE)
Definition: C4NetIO.cpp:824
void FixedRandom(uint64_t seed)
Definition: C4Random.cpp:37
void AddIO(C4NetIO *pNetIO, bool fSetCallback=true)
Definition: C4NetIO.cpp:3950
int iSize
Definition: TstC4NetIO.cpp:35

Here is the call graph for this function:

Variable Documentation

char DummyData[1024 *1024]

Definition at line 36 of file TstC4NetIO.cpp.

Referenced by main(), MyCBClass::OnConn(), and MyCBClass::OnPacket().

bool fHost