OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
TstC4NetIO.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2005-2006, Peter Wortmann
5  * Copyright (c) 2005, Günther Brammer
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 #include "C4Include.h"
19 #include "network/C4NetIO.h"
20 #include "lib/C4Random.h"
21 
22 #include <iostream>
23 #include <sstream>
24 #include <time.h>
25 #ifdef _WIN32
27 #include <mmsystem.h>
28 #else
29 #include <arpa/inet.h>
30 #endif
31 
32 using namespace std;
33 
34 bool fHost;
35 int iCnt = 0, iSize = 0;
36 char DummyData[1024 * 1024];
37 
38 #define ASYNC_CONNECT
39 
40 // TCP otherwise
41 #define USE_UDP
42 
43 class MyCBClass : public C4NetIOMan
44 {
45  unsigned int tTime, iPcks;
46 public:
47  virtual bool OnConn(const C4NetIO::addr_t &addr, const C4NetIO::addr_t &addr2, C4NetIO *pNetIO)
48  {
49  cout << "got connection from " << inet_ntoa(addr.sin_addr) << endl;
50  tTime = C4TimeMilliseconds::Now();
51  iPcks = 0;
52 
53 #ifdef ASYNC_CONNECT
54  if (!fHost)
55  {
56  DummyData[0] = 0;
57  if (!pNetIO->Send(C4NetIOPacket(DummyData, iSize, true, addr)))
58  cout << " Fehler: " << (pNetIO->GetError() ? pNetIO->GetError() : "bla") << endl;
59  }
60 #endif
61  return true;
62  }
63  virtual void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO)
64  {
66  if (tNow > tTime + 1000)
67  {
68  cout << iPcks << " packets in " << tNow - tTime << " ms (" << iPcks * 1000 / (tNow - tTime) << " per second, " << (iPcks ? (tNow - tTime) * 1000 / iPcks : -1u) << "us per packet)" << endl;
69  tTime = C4TimeMilliseconds::Now();
70  iPcks = 0;
71  }
72  if (!rPacket.getStatus())
73  {
74  // dummys
75  DummyData[0] = 1;
76  C4NetIOPacket Dummy(DummyData, iSize, true, rPacket.getAddr());
77  for (int i = 0; i < iCnt; i++)
78  {
79  if (!pNetIO->Send(Dummy))
80  cout << " Fehler: " << (pNetIO->GetError() ? pNetIO->GetError() : "bla") << endl;
81  }
82  // pong
83  pNetIO->Send(rPacket);
84  iPcks++;
85  }
86  // cout << "got " << rPacket.GetSize() << " bytes of data (" << int(*rPacket.GetData()) << ")" << endl;
87  }
88  virtual void OnDisconn(const C4NetIO::addr_t &addr, C4NetIO *pNetIO, const char *szReason)
89  {
90  cout << "client from " << inet_ntoa(addr.sin_addr) << " disconnected (" << szReason << ")" << endl;
91  }
92  virtual void OnError(const char *strError, C4NetIO *pNetIO)
93  {
94  cout << "network error: " << strError << endl;
95  };
96 };
97 
98 int main(int argc, char * argv[])
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 }
virtual void OnDisconn(const C4NetIO::addr_t &addr, C4NetIO *pNetIO, const char *szReason)
Definition: TstC4NetIO.cpp:88
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 OnConn(const C4NetIO::addr_t &addr, const C4NetIO::addr_t &addr2, C4NetIO *pNetIO)
Definition: TstC4NetIO.cpp:47
virtual bool InitBroadcast(addr_t *pBroadcastAddr)
Definition: C4NetIO.cpp:864
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:316
int main(int argc, char *argv[])
Definition: TstC4NetIO.cpp:98
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 void OnError(const char *strError, C4NetIO *pNetIO)
Definition: TstC4NetIO.cpp:92
uint8_t getStatus() const
Definition: C4NetIO.h:318
virtual void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO)
Definition: TstC4NetIO.cpp:63
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
static C4TimeMilliseconds Now()
int iSize
Definition: TstC4NetIO.cpp:35