Connection.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 "Precompiled.h"
  24. #include "Connection.h"
  25. #include "Log.h"
  26. #include "Protocol.h"
  27. #include "ProtocolEvents.h"
  28. #include "Scene.h"
  29. #include "StringUtils.h"
  30. #include "DebugNew.h"
  31. static const float RTT_LERP_FACTOR = 0.1f;
  32. OBJECTTYPESTATIC(Connection);
  33. Connection::Connection(Context* context, Peer* peer) :
  34. Object(context),
  35. peer_(peer),
  36. frameNumber_(0),
  37. frameAck_(0),
  38. eventFrameNumber_(0),
  39. roundTripTime_(0.0f),
  40. challenge_(0),
  41. hasChallenge_(false),
  42. joinState_(JS_NOTINSCENE),
  43. position_(Vector3::ZERO)
  44. {
  45. }
  46. Connection::~Connection()
  47. {
  48. // Make sure any references in the scene are cleared
  49. LeftScene();
  50. }
  51. void Connection::SendReliable(const VectorBuffer& packet)
  52. {
  53. Send(packet, true);
  54. }
  55. void Connection::SendUnreliable(const VectorBuffer& packet)
  56. {
  57. Send(packet, false);
  58. }
  59. bool Connection::ReceiveReliable(VectorBuffer& packet)
  60. {
  61. return Receive(packet, true);
  62. }
  63. bool Connection::ReceiveUnreliable(VectorBuffer& packet)
  64. {
  65. return Receive(packet, false);
  66. }
  67. void Connection::Disconnect()
  68. {
  69. LeftScene();
  70. peer_->Disconnect();
  71. }
  72. void Connection::forceDisconnect()
  73. {
  74. LeftScene();
  75. peer_->ForceDisconnect();
  76. }
  77. void Connection::SetLoginData(const VariantMap& loginData)
  78. {
  79. loginData_ = loginData;
  80. }
  81. void Connection::SetScene(Scene* scene)
  82. {
  83. // Leave previous scene first
  84. if ((scene_) && (scene_ != scene))
  85. LeftScene();
  86. scene_ = scene;
  87. joinState_ = JS_PREPARESCENE;
  88. // Client: set the proxy flag to the scene
  89. if (peer_->GetPeerType() == PEER_SERVER)
  90. scene_->SetNetworkMode(NM_CLIENT);
  91. }
  92. void Connection::JoinedScene()
  93. {
  94. if (!scene_)
  95. return;
  96. if (scene_->GetNetworkMode() == NM_SERVER)
  97. {
  98. // Server: send event, so that game-specific code may create the player node/nodes
  99. using namespace ClientJoinedScene;
  100. VariantMap eventData;
  101. eventData[P_CONNECTION] = (void*)this;
  102. eventData[P_SCENE] = (void*)scene_.GetPtr();
  103. SendEvent(E_CLIENTJOINEDSCENE, eventData);
  104. LOGINFO("Client " + GetIdentity() + " joined scene " + scene_->GetName());
  105. }
  106. joinState_ = JS_SENDFULLUPDATE;
  107. }
  108. void Connection::LeftScene()
  109. {
  110. if (scene_)
  111. {
  112. NetworkMode mode = scene_->GetNetworkMode();
  113. if (mode == NM_SERVER)
  114. {
  115. // Server: send event so that game-specific code may remove the player node/nodes
  116. using namespace ClientLeftScene;
  117. VariantMap eventData;
  118. eventData[P_CONNECTION] = (void*)this;
  119. eventData[P_SCENE] = (void*)scene_.GetPtr();
  120. SendEvent(E_CLIENTLEFTSCENE, eventData);
  121. LOGINFO("Client " + GetIdentity() + " left scene " + scene_->GetName());
  122. }
  123. // Remove owner reference from any nodes
  124. scene_->ResetOwner(this);
  125. // Reset the client mode if set (scene can now be reused as singleplayer again)
  126. if (mode == NM_CLIENT)
  127. {
  128. scene_->Clear();
  129. scene_->SetNetworkMode(NM_NONETWORK);
  130. }
  131. }
  132. scene_.Reset();
  133. remoteEvents_.Clear();
  134. sceneState_.Clear();
  135. joinState_ = JS_NOTINSCENE;
  136. }
  137. void Connection::SetChallenge(unsigned challenge)
  138. {
  139. challenge_ = challenge;
  140. hasChallenge_ = true;
  141. }
  142. void Connection::SetJoinState(JoinState state)
  143. {
  144. joinState_ = state;
  145. }
  146. void Connection::SetFrameNumbers(unsigned short frameNumber, unsigned short frameAck)
  147. {
  148. frameNumber_ = frameNumber;
  149. frameAck_ = frameAck;
  150. }
  151. void Connection::UpdateRoundTripTime(int netFps, unsigned short frameNumber)
  152. {
  153. unsigned short frameDiff = frameNumber - frameAck_;
  154. if (frameDiff >= 0x8000)
  155. frameDiff = frameAck_ - frameNumber;
  156. float newRtt = (1.0f / netFps) * frameDiff;
  157. roundTripTime_ = Lerp(roundTripTime_, newRtt, RTT_LERP_FACTOR);
  158. }
  159. void Connection::SetControls(const Controls& controls)
  160. {
  161. controls_ = controls;
  162. }
  163. void Connection::SetPosition(const Vector3& position)
  164. {
  165. position_ = position;
  166. }
  167. void Connection::addRemoteEvent(const RemoteEvent& remoteEvent)
  168. {
  169. remoteEvents_.Push(remoteEvent);
  170. }
  171. bool Connection::CheckRemoteEventFrame(const RemoteEvent& remoteEvent, unsigned short previousEventFrameNumber)
  172. {
  173. // Check against previous event framenumber, and update current
  174. if (!CheckFrameNumber(remoteEvent.frameNumber_, previousEventFrameNumber, false))
  175. return false;
  176. if (CheckFrameNumber(remoteEvent.frameNumber_, eventFrameNumber_))
  177. eventFrameNumber_ = remoteEvent.frameNumber_;
  178. return true;
  179. }
  180. void Connection::PurgeAckedSceneState()
  181. {
  182. sceneState_.Acked(frameAck_);
  183. }
  184. void Connection::PurgeAckedRemoteEvents(unsigned short frameNumber)
  185. {
  186. for (Vector<RemoteEvent>::Iterator i = remoteEvents_.Begin(); i != remoteEvents_.End();)
  187. {
  188. bool erase = false;
  189. if (!CheckFrameNumber(i->frameNumber_, frameAck_, false))
  190. erase = true;
  191. else if (i->timeToLive_)
  192. {
  193. unsigned short expiryFrameNumber = i->frameNumber_ + i->timeToLive_;
  194. if (!expiryFrameNumber)
  195. ++expiryFrameNumber;
  196. if (!CheckFrameNumber(expiryFrameNumber, frameNumber))
  197. erase = true;
  198. }
  199. if (erase)
  200. i = remoteEvents_.Erase(i);
  201. else
  202. ++i;
  203. }
  204. }
  205. void Connection::ClearSceneState()
  206. {
  207. sceneState_.Clear();
  208. }
  209. void Connection::ClearRemoteEvents()
  210. {
  211. remoteEvents_.Clear();
  212. }
  213. String Connection::GetIdentity() const
  214. {
  215. if (peer_)
  216. return peer_->GetAddress() + ":" + peer_->GetPort();
  217. else
  218. return "Unknown";
  219. }
  220. bool Connection::IsConnected() const
  221. {
  222. return peer_->GetConnectionState() == CS_CONNECTED;
  223. }
  224. void Connection::Send(const VectorBuffer& packet, bool reliable)
  225. {
  226. if ((packet.GetSize()) && (peer_->GetConnectionState() == CS_CONNECTED))
  227. peer_->Send(packet, reliable ? CHN_RELIABLE : CHN_UNRELIABLE, reliable);
  228. }
  229. bool Connection::Receive(VectorBuffer& packet, bool reliable)
  230. {
  231. if (peer_->GetConnectionState() == CS_CONNECTED)
  232. return peer_->Receive(packet, reliable ? CHN_RELIABLE : CHN_UNRELIABLE);
  233. else
  234. {
  235. peer_->FlushPackets();
  236. return false;
  237. }
  238. }