Main.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Exception.h"
  24. #include "Log.h"
  25. #include "Mutex.h"
  26. #include "Network.h"
  27. #include "Peer.h"
  28. #include "ProcessUtils.h"
  29. #include "Profiler.h"
  30. #include "StringUtils.h"
  31. #include <iostream>
  32. #include <string>
  33. #include <vector>
  34. #include <windows.h>
  35. #include "DebugNew.h"
  36. int main(int argc, char** argv)
  37. {
  38. std::vector<std::string> arguments;
  39. for (int i = 1; i < argc; ++i)
  40. arguments.push_back(std::string(argv[i]));
  41. if (arguments.size() < 1)
  42. {
  43. std::cout << "Usage (server): NetworkTest server\nUsage (client): NetworkTest <serveraddress> [username]" << std::endl;
  44. return 1;
  45. }
  46. Log log("NetworkTest.log", LOG_DEBUG);
  47. Network network;
  48. std::string userName;
  49. if (arguments[0] == "server")
  50. {
  51. if (!network.startServer(1234))
  52. {
  53. std::cout << "Failed to start server" << std::endl;
  54. return 1;
  55. }
  56. }
  57. else
  58. {
  59. if (arguments.size() > 1)
  60. userName = arguments[1];
  61. if (!network.connect(arguments[0], 1234))
  62. {
  63. std::cout << "Failed to connect" << std::endl;
  64. return 1;
  65. }
  66. }
  67. for (;;)
  68. {
  69. network.update();
  70. Peer* server = network.getServerPeer();
  71. // Client: check for server disconnection
  72. if ((!network.isServer()) && (!server))
  73. break;
  74. std::string command;
  75. if (getConsoleInput(command))
  76. {
  77. if (command == "shutdown")
  78. break;
  79. else
  80. {
  81. // Client: send the input in a packet, and optional username
  82. if (!network.isServer())
  83. {
  84. VectorBuffer packet;
  85. packet.writeString(command);
  86. if (!userName.empty())
  87. packet.writeString(userName);
  88. server->send(packet, 0, true);
  89. }
  90. }
  91. }
  92. const std::vector<SharedPtr<Peer> >& peers = network.getPeers();
  93. for (unsigned i = 0; i < peers.size(); ++i)
  94. {
  95. VectorBuffer packet;
  96. while (peers[i]->receive(packet))
  97. {
  98. std::string message = packet.readString();
  99. // Server: broadcast the message to all, with the sender prepended, otherwise just show it
  100. if (network.isServer())
  101. {
  102. // If packet contains also the username, show it, else just show IP and port
  103. std::string name;
  104. if (!packet.isEof())
  105. name = packet.readString();
  106. else
  107. name = peers[i]->getAddress() + ":" + toString(peers[i]->getPort());
  108. std::string nameAndMessage = "[" + name + "] " + message;
  109. VectorBuffer newPacket;
  110. newPacket.writeString(nameAndMessage);
  111. network.broadcast(newPacket, 0, true);
  112. std::cout << nameAndMessage << std::endl;
  113. }
  114. else
  115. std::cout << message << std::endl;
  116. }
  117. }
  118. }
  119. return 0;
  120. }