Connection.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "DebugNew.h"
  26. OBJECTTYPESTATIC(Connection);
  27. Connection::Connection(Context* context, kNet::SharedPtr<kNet::MessageConnection> connection) :
  28. Object(context),
  29. connection_(connection),
  30. connectPending_(false)
  31. {
  32. }
  33. Connection::~Connection()
  34. {
  35. }
  36. void Connection::SendMessage(int messageID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes)
  37. {
  38. // Make sure not to use kNet internal message ID's
  39. if (messageID <= 0x4 || messageID >= 0x3ffffffe)
  40. return;
  41. connection_->SendMessage(messageID, reliable, inOrder, 0, 0, (const char*)data, numBytes);
  42. }
  43. void Connection::SendMessage(int messageID, bool reliable, bool inOrder, const VectorBuffer& data)
  44. {
  45. // Make sure not to use kNet internal message ID's
  46. if (messageID <= 0x4 || messageID >= 0x3ffffffe)
  47. return;
  48. connection_->SendMessage(messageID, reliable, inOrder, 0, 0, (const char*)data.GetData(), data.GetSize());
  49. }
  50. void Connection::Disconnect(int waitMSec)
  51. {
  52. connection_->Disconnect(waitMSec);
  53. }
  54. kNet::MessageConnection* Connection::GetMessageConnection() const
  55. {
  56. return const_cast<kNet::MessageConnection*>(connection_.ptr());
  57. }
  58. kNet::ConnectionState Connection::GetConnectionState() const
  59. {
  60. return connection_->GetConnectionState();
  61. }
  62. bool Connection::IsConnected() const
  63. {
  64. return connection_->GetConnectionState() == kNet::ConnectionOK;
  65. }
  66. String Connection::GetAddress() const
  67. {
  68. kNet::Socket* socket = const_cast<kNet::MessageConnection*>(connection_.ptr())->GetSocket();
  69. return socket ? String(socket->DestinationAddress()) : String();
  70. }
  71. unsigned short Connection::GetPort() const
  72. {
  73. kNet::Socket* socket = const_cast<kNet::MessageConnection*>(connection_.ptr())->GetSocket();;
  74. return socket ? socket->DestinationPort() : 0;
  75. }
  76. String Connection::ToString() const
  77. {
  78. return GetAddress() + ":" + String(GetPort());
  79. }