Beispiel #1
0
        /// <summary>
        /// Creates the network peer.
        /// </summary>
        /// <param name="peer">The peer.</param>
        /// <returns></returns>
        internal static NetworkPeerRemote CreateNetworkPeer(NetworkPeer peer)
        {
            if (peer == null)
            {
                ThrowHelper.ThrowArgumentNullException("peer");
            }

            NetworkContext networkContext = NetworkContext.GetNetworkContextByName(peer.NetworkContext);

            if (networkContext == null)
            {
                networkContext = NetworkContext.CreateNetworkContext(peer.NetworkContext);
            }
            NetworkPeerRemote remotePeer = new NetworkPeerRemote();

            remotePeer.HostName       = peer.HostName;
            remotePeer.Id             = peer.Id;
            remotePeer.NetworkContext = networkContext;
            remotePeer.PeerType       = PeerTypeEnum.Remote;
            RefreshNetworkPeerWithoutRelations(peer, remotePeer);
            List <INetworkPeerRemote> list = networkContext.InternalNetworkPeers;

            lock (list)
            {
                list.Add(remotePeer);
                list.Sort();
            }
            return(remotePeer);
        }
Beispiel #2
0
        /// <summary>
        /// Refreshes the network peer without relations.
        /// </summary>
        /// <param name="sourcePeer">The source peer.</param>
        /// <param name="targetPeer">The target peer.</param>
        internal static void RefreshNetworkPeerOnlyRelations(NetworkPeer sourcePeer, NetworkPeerRemote targetPeer)
        {
            if (sourcePeer == null)
            {
                ThrowHelper.ThrowArgumentNullException("sourcePeer");
            }
            if (targetPeer == null)
            {
                ThrowHelper.ThrowArgumentNullException("targetPeer");
            }

            //if (sourcePeer.PeerRelations.StateId > targetPeer.PeerRelationPairs.StateId)
            {
                //targetPeer.PeerRelationPairs.StateId = sourcePeer.PeerRelations.StateId;
                //targetPeer.PeerRelationPairs.PeerRelationPairs.Clear();
                if (sourcePeer.PeerRelations.PeerRelations != null)
                {
                    foreach (PeerRelation r in sourcePeer.PeerRelations.PeerRelations)
                    {
                        targetPeer.PeerRelationPairs.AddOrUpdatePeerRelationForce(
                            targetPeer,
                            NetworkManager.Instance.InternalLocalhost.Id.Equals(r.PeerB) ? NetworkManager.Instance.InternalLocalhost : (NetworkPeerRemote)NetworkPeerContext.GetNetworkPeerById(r.PeerB), r.Connected, r.StateId);
                    }
                }
            }
        }
Beispiel #3
0
 /// <summary>
 /// Waits for sent event, if the message is TCP and sent by me.
 /// </summary>
 /// <param name="timeout">The timeout.</param>
 internal void WaitForSentEvent(int timeout)
 {
     if (mMessage.MessageType == MessageTypeEnum.Tcp && NetworkManager.Instance.InternalLocalhost.Id.Equals(mMessage.SenderId))
     {
         // csak a saját TCP üzeneteimre lehet várakozni
         lock (mLockObject)
         {
             if (mWatch == null)
             {
                 mWatch = Stopwatch.StartNew();
             }
             if (mSentEvent == null)
             {
                 mSentEvent = new ManualResetEvent(false);
             }
         }
         if (mSentEvent.WaitOne(timeout) && IsSuccess)
         {
             // replytime feljegyzése
             NetworkPeerRemote networkPeer = (NetworkPeerRemote)NetworkPeerContext.GetNetworkPeerById(mMessage.TargetId);
             networkPeer.Session.ReplyTime = ReplyTime;
         }
     }
     else
     {
         IsSuccess = true;
     }
 }
Beispiel #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NetworkPeerSession"/> class.
 /// </summary>
 /// <param name="remotePeer">The remote peer.</param>
 internal NetworkPeerSession(NetworkPeerRemote remotePeer)
 {
     if (remotePeer == null)
     {
         ThrowHelper.ThrowArgumentNullException("remotePeer");
     }
     this.mRemotePeer = remotePeer;
 }
Beispiel #5
0
 private void ProcessConnection(object state)
 {
     if (!NetworkManager.Instance.IsShutdown)
     {
         ConnectionTask task = (ConnectionTask)state;
         try
         {
             task.NetworkStream = null;
             Synapse.NetworkStream stream = NetworkManager.Instance.InternalNetworkManager.Connect(task.EndPoint); // dobhat hibát
             stream.SendBufferSize    = NetworkManager.Instance.InternalConfiguration.Settings.DefaultLowLevelSocketSendBufferSize;
             stream.ReceiveBufferSize = NetworkManager.Instance.InternalConfiguration.Settings.DefaultLowLevelSocketReceiveBufferSize;
             stream.NoDelay           = NetworkManager.Instance.InternalConfiguration.Settings.DefaultLowLevelNoDelay;
             task.NetworkStream       = stream;
             NetworkPeerRemote result = NetworkManager.Instance.ProcessConnection(stream, task.ConnectionTimeout, true, !NetworkManager.Instance.InternalConfiguration.Settings.EnableMultipleConnectionWithNetworkPeers);
             if (result == null)
             {
                 if (task.ReconnectOnFailure)
                 {
                     // újra időzítés
                     QueueToReconnect(task);
                 }
                 else
                 {
                     lock (mConnectionTasks)
                     {
                         mConnectionTasks.Remove(task);
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             // failed to resolve host
             if (LOGGER.IsErrorEnabled)
             {
                 LOGGER.Error(string.Format("CONNECTION_MANAGER, failed to establish connection to a remote host. Reason: {0}", ex.Message));
             }
             if (task.ReconnectOnFailure)
             {
                 // újra időzítés
                 QueueToReconnect(task);
             }
             else
             {
                 lock (mConnectionTasks)
                 {
                     mConnectionTasks.Remove(task);
                 }
             }
         }
     }
 }
Beispiel #6
0
        /// <summary>
        /// Refreshes the network peer.
        /// </summary>
        /// <param name="sourcePeer">The source peer.</param>
        /// <param name="targetPeer">The target peer.</param>
        internal static void RefreshNetworkPeerWithoutRelations(NetworkPeer sourcePeer, NetworkPeerRemote targetPeer)
        {
            if (sourcePeer == null)
            {
                ThrowHelper.ThrowArgumentNullException("sourcePeer");
            }
            if (targetPeer == null)
            {
                ThrowHelper.ThrowArgumentNullException("targetPeer");
            }

            if (sourcePeer.BlackHoleContainer.StateId > targetPeer.BlackHoleContainer.StateId)
            {
                targetPeer.BlackHoleContainer.StateId     = targetPeer.BlackHoleContainer.StateId;
                targetPeer.BlackHoleContainer.IsBlackHole = targetPeer.BlackHoleContainer.IsBlackHole;
            }
            if (sourcePeer.NATGateways.StateId > targetPeer.NATGatewayCollection.StateId)
            {
                targetPeer.NATGatewayCollection.StateId = sourcePeer.NATGateways.StateId;
                targetPeer.NATGatewayCollection.NATGateways.Clear();
                if (sourcePeer.NATGateways.Gateways != null)
                {
                    List <NATGateway> backup = new List <NATGateway>(targetPeer.NATGatewayCollection.NATGateways);
                    targetPeer.NATGatewayCollection.NATGateways.Clear();
                    foreach (AddressEndPoint ep in sourcePeer.NATGateways.Gateways)
                    {
                        NATGateway gw    = new NATGateway(ep);
                        bool       found = false;
                        foreach (NATGateway g in backup)
                        {
                            if (g.Equals(gw))
                            {
                                targetPeer.NATGatewayCollection.NATGateways.Add(g);
                                found = true;
                                break;
                            }
                        }
                        if (!found)
                        {
                            targetPeer.NATGatewayCollection.NATGateways.Add(gw);
                        }
                    }
                }
                else
                {
                    targetPeer.NATGatewayCollection.NATGateways.Clear();
                }
            }
            if (sourcePeer.PeerContext.StateId > targetPeer.InternalPeerContext.StateId)
            {
                targetPeer.InternalPeerContext.StateId     = sourcePeer.PeerContext.StateId;
                targetPeer.InternalPeerContext.PeerContext = sourcePeer.PeerContext.PeerContext;
            }
            if (sourcePeer.TCPServers.StateId > targetPeer.TCPServerCollection.StateId)
            {
                targetPeer.TCPServerCollection.StateId = sourcePeer.TCPServers.StateId;
                if (sourcePeer.TCPServers.Servers != null)
                {
                    List <TCPServer> backup = new List <TCPServer>(targetPeer.TCPServerCollection.TCPServers);
                    targetPeer.TCPServerCollection.TCPServers.Clear();
                    foreach (AddressEndPoint ep in sourcePeer.TCPServers.Servers)
                    {
                        TCPServer sw    = new TCPServer(ep);
                        bool      found = false;
                        foreach (TCPServer s in backup)
                        {
                            if (s.Equals(sw))
                            {
                                targetPeer.TCPServerCollection.TCPServers.Add(s);
                                found = true;
                                break;
                            }
                        }
                        if (!found)
                        {
                            targetPeer.TCPServerCollection.TCPServers.Add(sw);
                        }
                    }
                }
                else
                {
                    targetPeer.TCPServerCollection.TCPServers.Clear();
                }
            }
            targetPeer.Version = sourcePeer.Version;
        }
Beispiel #7
0
        private static void ConnectionTask(object state)
        {
            UdpBroadcastMessage message = (UdpBroadcastMessage)state;

            if (LOGGER.IsDebugEnabled)
            {
                LOGGER.Debug(string.Format("BROADCAST_SERVER, processing message of sender, id: {0}", message.SenderId));
            }
            if (message.TCPServers != null && message.TCPServers.Length > 0)
            {
                foreach (AddressEndPoint ep in message.TCPServers)
                {
                    if (ep.AddressFamily == AddressFamily.InterNetwork || (ep.AddressFamily == AddressFamily.InterNetworkV6 && NetworkManager.Instance.InternalConfiguration.Settings.EnableIPV6))
                    {
                        try
                        {
                            if (LOGGER.IsInfoEnabled)
                            {
                                LOGGER.Info(string.Format("BROADCAST_SERVER, connecting to [{0}] with address [{1}] and port {2}.", message.SenderId, ep.Host, ep.Port));
                            }
                            Synapse.NetworkStream stream = NetworkManager.Instance.InternalNetworkManager.Connect(ep);
                            stream.SendBufferSize    = NetworkManager.Instance.InternalConfiguration.Settings.DefaultLowLevelSocketSendBufferSize;
                            stream.ReceiveBufferSize = NetworkManager.Instance.InternalConfiguration.Settings.DefaultLowLevelSocketReceiveBufferSize;
                            stream.NoDelay           = NetworkManager.Instance.InternalConfiguration.Settings.DefaultLowLevelNoDelay;
                            NetworkPeerRemote remotePeer = NetworkManager.Instance.ProcessConnection(stream, NetworkManager.Instance.InternalConfiguration.Settings.DefaultConnectionTimeoutInMS, true, !NetworkManager.Instance.InternalConfiguration.Settings.EnableMultipleConnectionWithNetworkPeers);
                            if (NetworkManager.Instance.IsShutdown)
                            {
                                stream.Close();
                                break;
                            }
                            if (remotePeer != null && remotePeer.Id.Equals(message.SenderId))
                            {
                                // létrejött a kapcsolat, tovább nem próbálkozunk
                                if (LOGGER.IsInfoEnabled)
                                {
                                    LOGGER.Info(string.Format("BROADCAST_SERVER, successfully connected to '{0}' on TCP server.", message.SenderId));
                                }
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            if (LOGGER.IsErrorEnabled)
                            {
                                LOGGER.Error(string.Format("BROADCAST_SERVER, failed to connect to [{0}] with address [{1}] and port {2}. Reason: {3}", message.SenderId, ep.Host, ep.Port, ex.Message));
                            }
                        }
                    }
                    else
                    {
                        if (LOGGER.IsInfoEnabled)
                        {
                            LOGGER.Info(string.Format("BROADCAST_SERVER, connecting to [{0}] with address [{1}] not allowed. IPV6 protocol disabled.", message.SenderId, ep.Host));
                        }
                    }
                }
            }
            INetworkPeerRemote peer = NetworkPeerContext.GetNetworkPeerById(message.SenderId);

            if (peer == null || (peer != null && peer.Distance != 1))
            {
                if (message.NATGateways != null && message.NATGateways.Length > 0)
                {
                    foreach (AddressEndPoint ep in message.NATGateways)
                    {
                        if (ep.AddressFamily == AddressFamily.InterNetwork || (ep.AddressFamily == AddressFamily.InterNetworkV6 && NetworkManager.Instance.InternalConfiguration.Settings.EnableIPV6))
                        {
                            try
                            {
                                if (LOGGER.IsInfoEnabled)
                                {
                                    LOGGER.Info(string.Format("BROADCAST_SERVER, connecting to [{0}] with NAT address [{1}] and port {2}.", message.SenderId, ep.Host, ep.Port));
                                }
                                Synapse.NetworkStream stream = NetworkManager.Instance.InternalNetworkManager.Connect(ep);
                                stream.SendBufferSize    = NetworkManager.Instance.InternalConfiguration.Settings.DefaultLowLevelSocketSendBufferSize;
                                stream.ReceiveBufferSize = NetworkManager.Instance.InternalConfiguration.Settings.DefaultLowLevelSocketReceiveBufferSize;
                                stream.NoDelay           = NetworkManager.Instance.InternalConfiguration.Settings.DefaultLowLevelNoDelay;
                                NetworkPeerRemote remotePeer = NetworkManager.Instance.ProcessConnection(stream, NetworkManager.Instance.InternalConfiguration.Settings.DefaultConnectionTimeoutInMS, true, !NetworkManager.Instance.InternalConfiguration.Settings.EnableMultipleConnectionWithNetworkPeers);
                                if (NetworkManager.Instance.IsShutdown)
                                {
                                    stream.Close();
                                    break;
                                }
                                if (remotePeer != null && remotePeer.Id.Equals(message.SenderId))
                                {
                                    // létrejött a kapcsolat, tovább nem próbálkozunk
                                    if (LOGGER.IsInfoEnabled)
                                    {
                                        LOGGER.Info(string.Format("BROADCAST_SERVER, successfully connected to '{0}' on NAT address.", message.SenderId));
                                    }
                                    break;
                                }
                            }
                            catch (Exception ex)
                            {
                                if (LOGGER.IsErrorEnabled)
                                {
                                    LOGGER.Error(string.Format("BROADCAST_SERVER, failed to connect to [{0}] with address [{1}] and port {2}. Reason: {3}", message.SenderId, ep.Host, ep.Port, ex.Message));
                                }
                            }
                        }
                        else
                        {
                            if (LOGGER.IsInfoEnabled)
                            {
                                LOGGER.Info(string.Format("BROADCAST_SERVER, connecting to [{0}] with address [{1}] not allowed. IPV6 protocol disabled.", message.SenderId, ep.Host));
                            }
                        }
                    }
                }
            }
        }