Browse Source

Added Connection class missing from previous commit.

Lasse Öörni 14 years ago
parent
commit
2910f128f3
2 changed files with 170 additions and 0 deletions
  1. 95 0
      Engine/Network/Connection.cpp
  2. 75 0
      Engine/Network/Connection.h

+ 95 - 0
Engine/Network/Connection.cpp

@@ -0,0 +1,95 @@
+//
+// Urho3D Engine
+// Copyright (c) 2008-2011 Lasse Öörni
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#include "Precompiled.h"
+#include "Connection.h"
+
+#include "DebugNew.h"
+
+OBJECTTYPESTATIC(Connection);
+
+Connection::Connection(Context* context, kNet::SharedPtr<kNet::MessageConnection> connection) :
+    Object(context),
+    connection_(connection),
+    connectPending_(false)
+{
+}
+
+Connection::~Connection()
+{
+}
+
+void Connection::SendMessage(int messageID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes)
+{
+    // Make sure not to use kNet internal message ID's
+    if (messageID <= 0x4 || messageID >= 0x3ffffffe)
+        return;
+    
+    connection_->SendMessage(messageID, reliable, inOrder, 0, 0, (const char*)data, numBytes);
+}
+
+void Connection::SendMessage(int messageID, bool reliable, bool inOrder, const VectorBuffer& data)
+{
+    // Make sure not to use kNet internal message ID's
+    if (messageID <= 0x4 || messageID >= 0x3ffffffe)
+        return;
+    
+    connection_->SendMessage(messageID, reliable, inOrder, 0, 0, (const char*)data.GetData(), data.GetSize());
+}
+
+void Connection::Disconnect(int waitMSec)
+{
+    connection_->Disconnect(waitMSec);
+}
+
+kNet::MessageConnection* Connection::GetMessageConnection() const
+{
+    return const_cast<kNet::MessageConnection*>(connection_.ptr());
+}
+
+kNet::ConnectionState Connection::GetConnectionState() const
+{
+    return connection_->GetConnectionState();
+}
+
+bool Connection::IsConnected() const
+{
+    return connection_->GetConnectionState() == kNet::ConnectionOK;
+}
+
+String Connection::GetAddress() const
+{
+    kNet::Socket* socket = const_cast<kNet::MessageConnection*>(connection_.ptr())->GetSocket();
+    return socket ? String(socket->DestinationAddress()) : String();
+}
+
+unsigned short Connection::GetPort() const
+{
+    kNet::Socket* socket = const_cast<kNet::MessageConnection*>(connection_.ptr())->GetSocket();;
+    return socket ? socket->DestinationPort() : 0;
+}
+
+String Connection::ToString() const
+{
+    return GetAddress() + ":" + String(GetPort());
+}

+ 75 - 0
Engine/Network/Connection.h

@@ -0,0 +1,75 @@
+//
+// Urho3D Engine
+// Copyright (c) 2008-2011 Lasse Öörni
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#pragma once
+
+#include "Object.h"
+#include "VectorBuffer.h"
+
+#include <kNet.h>
+
+#ifdef SendMessage
+#undef SendMessage
+#endif
+
+/// Connection in a networked scene
+class Connection : public Object
+{
+    OBJECT(Connection);
+    
+    friend class Network;
+    
+public:
+    /// Construct with the context and message connection pointers
+    Connection(Context* context, kNet::SharedPtr<kNet::MessageConnection> connection);
+    /// Destruct
+    ~Connection();
+    
+    /// Send a message
+    void SendMessage(int messageID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes);
+    /// Send a message
+    void SendMessage(int messageID, bool reliable, bool inOrder, const VectorBuffer& data);
+    /// Disconnect. If wait time is non-zero, will block while waiting for disconnect to finish
+    void Disconnect(int waitMSec = 0);
+    
+    /// Return the message connection
+    kNet::MessageConnection* GetMessageConnection() const;
+    /// Return connection state
+    kNet::ConnectionState GetConnectionState() const;
+    /// Return whether is fully connected
+    bool IsConnected() const;
+    /// Return whether connection is pending
+    bool IsConnectPending() const { return connectPending_; }
+    /// Return remote address
+    String GetAddress() const;
+    /// Return remote port
+    unsigned short GetPort() const;
+    /// Return an address:port string
+    String ToString() const;
+    
+private:
+    /// kNet MessageConnection
+    kNet::SharedPtr<kNet::MessageConnection> connection_;
+    /// Connection pending flag
+    bool connectPending_;
+};