Пример #1
0
 public bool SendPacket(Mule.Network.Packet packet, bool delpacket, Mule.Network.ServerSocket to)
 {
     if (to != null)
     {
         if (IsConnected)
         {
             connectedSocket_.SendPacket(packet, delpacket, true);
         }
         else
         {
             return(false);
         }
     }
     else
     {
         to.SendPacket(packet, delpacket, true);
     }
     return(true);
 }
Пример #2
0
        public void DestroySocket(Mule.Network.ServerSocket pSck)
        {
            if (pSck == null)
            {
                return;
            }
            // remove socket from list of opened sockets
            foreach (ServerSocket pTestSck in openSockets_)
            {
                if (pTestSck == pSck)
                {
                    openSockets_.Remove(pTestSck);
                    break;
                }
            }

            if (pSck != null)
            { // deadlake PROXYSUPPORT - changed to AsyncSocketEx
                pSck.Close();
            }
        }
Пример #3
0
        public void ConnectionFailed(Mule.Network.ServerSocket sender)
        {
            if (!IsConnecting && sender != connectedSocket_)
            {
                // just return, cleanup is done by the socket itself
                return;
            }

            ED2KServer pServer = MuleApplication.Instance.ServerList.GetServerByAddress(sender.CurrentServer.Address, sender.CurrentServer.Port);

            switch (sender.ConnectionState)
            {
            case ConnectionStateEnum.CS_FATALERROR:
                //TODO:Log
                break;

            case ConnectionStateEnum.CS_DISCONNECTED:
                MuleApplication.Instance.SharedFiles.ClearED2KPublishInfo();
                break;

            case ConnectionStateEnum.CS_SERVERDEAD:
                if (pServer != null)
                {
                    pServer.AddFailedCount();
                }
                break;

            case ConnectionStateEnum.CS_ERROR:
                break;

            case ConnectionStateEnum.CS_SERVERFULL:
                break;

            case ConnectionStateEnum.CS_NOTCONNECTED:
                break;
            }

            // IMPORTANT: mark this socket not to be deleted in StopConnectionTry(),
            // because it will delete itself after this function!
            sender.IsDeleting = true;

            switch (sender.ConnectionState)
            {
            case ConnectionStateEnum.CS_FATALERROR:
            {
                bool autoretry = !IsSingleConnect;
                StopConnectionTry();
                if (MuleApplication.Instance.Preference.IsReconnect && autoretry &&
                    retryTimer_ == null)
                {
                    // There are situations where we may get Winsock error codes which indicate
                    // that the network is down, although it is not. Those error codes may get
                    // thrown only for particular IPs. If the first server in our list has such
                    // an IP and will therefor throw such an error we would never connect to
                    // any server at all. To circumvent that, start the next auto-connection
                    // attempt with a different server (use the next server in the list).
                    startAutoConnectPos_ = 0;         // default: start at 0
                    if (pServer != null)
                    {
                        // If possible, use the "next" server.
                        int iPosInList =
                            MuleApplication.Instance.ServerList.GetPositionOfServer(pServer);
                        if (iPosInList >= 0)
                        {
                            startAutoConnectPos_ = (uint)((iPosInList + 1) %
                                                          MuleApplication.Instance.ServerList.ServerCount);
                        }
                    }

                    retryTimer_ =
                        new Timer(new TimerCallback(RetryConnectTimer),
                                  this, 0,
                                  MuleConstants.ONE_SEC_MS * MuleConstants.CS_RETRYCONNECTTIME);
                }
                break;
            }

            case ConnectionStateEnum.CS_DISCONNECTED:
            {
                MuleApplication.Instance.SharedFiles.ClearED2KPublishInfo();
                IsConnected = false;
                if (connectedSocket_ != null)
                {
                    connectedSocket_.Close();
                    connectedSocket_ = null;
                }
                MuleApplication.Instance.Statistics.ServerConnectTime = 0;
                MuleApplication.Instance.Statistics.Add2TotalServerDuration();
                if (MuleApplication.Instance.Preference.IsReconnect && !IsConnecting)
                {
                    ConnectToAnyServer();
                }
                break;
            }

            case ConnectionStateEnum.CS_ERROR:
            case ConnectionStateEnum.CS_NOTCONNECTED:
            {
                if (!IsConnecting)
                {
                    break;
                }
            }
                goto case ConnectionStateEnum.CS_SERVERDEAD;

            case ConnectionStateEnum.CS_SERVERDEAD:
            case ConnectionStateEnum.CS_SERVERFULL:
            {
                if (!IsConnecting)
                {
                    break;
                }
                if (IsSingleConnect)
                {
                    if (pServer != null && sender.IsServerCryptEnabledConnection &&
                        !MuleApplication.Instance.Preference.IsClientCryptLayerRequired)
                    {
                        // try reconnecting without obfuscation
                        ConnectToServer(pServer, false, true);
                        break;
                    }
                    StopConnectionTry();
                    break;
                }

                Dictionary <ulong, ServerSocket> .Enumerator pos = connectionAttemps_.GetEnumerator();
                while (pos.MoveNext())
                {
                    if (pos.Current.Value == sender)
                    {
                        connectionAttemps_.Remove(pos.Current.Key);
                        break;
                    }
                }
                TryAnotherConnectionRequest();
            }
            break;
            }
        }
Пример #4
0
        public void ConnectionEstablished(Mule.Network.ServerSocket sender)
        {
            if (!IsConnecting)
            {
                // we are already IsConnected to another server
                DestroySocket(sender);
                return;
            }

            InitLocalIP();
            if (sender.ConnectionState == ConnectionStateEnum.CS_WAITFORLOGIN)
            {
                ED2KServer pServer =
                    MuleApplication.Instance.ServerList.GetServerByAddress(sender.CurrentServer.Address,
                                                                           sender.CurrentServer.Port);
                if (pServer != null)
                {
                    pServer.ResetFailedCount();
                }

                // Send login packet
                SafeMemFile data = MpdObjectManager.CreateSafeMemFile(256);
                data.WriteHash16(MuleApplication.Instance.Preference.UserHash);
                data.WriteUInt32(ClientID);
                data.WriteUInt16(MuleApplication.Instance.Preference.Port);

                uint tagcount = 4;
                data.WriteUInt32(tagcount);

                Tag tagName = MpdObjectManager.CreateTag(TagTypeEnum.CT_NAME,
                                                         MuleApplication.Instance.Preference.UserNick);
                tagName.WriteTagToFile(data);

                Tag tagVersion = MpdObjectManager.CreateTag(TagTypeEnum.CT_VERSION, VersionsEnum.EDONKEYVERSION);
                tagVersion.WriteTagToFile(data);

                ServerFlagsEnum dwCryptFlags = 0;
                if (MuleApplication.Instance.Preference.IsClientCryptLayerSupported)
                {
                    dwCryptFlags |= ServerFlagsEnum.SRVCAP_SUPPORTCRYPT;
                }
                if (MuleApplication.Instance.Preference.IsClientCryptLayerRequested)
                {
                    dwCryptFlags |= ServerFlagsEnum.SRVCAP_REQUESTCRYPT;
                }
                if (MuleApplication.Instance.Preference.IsClientCryptLayerRequired)
                {
                    dwCryptFlags |= ServerFlagsEnum.SRVCAP_REQUIRECRYPT;
                }

                Tag tagFlags = MpdObjectManager.CreateTag(TagTypeEnum.CT_SERVER_FLAGS,
                                                          ServerFlagsEnum.SRVCAP_ZLIB | ServerFlagsEnum.SRVCAP_NEWTAGS |
                                                          ServerFlagsEnum.SRVCAP_LARGEFILES |
                                                          ServerFlagsEnum.SRVCAP_UNICODE | dwCryptFlags);

                tagFlags.WriteTagToFile(data);

                // eMule Version (14-Mar-2004: requested by lugdunummaster (need for LowID clients which have no chance
                // to send an Hello packet to the server during the callback test))
                Tag tagMuleVersion = MpdObjectManager.CreateTag(TagTypeEnum.CT_EMULE_VERSION,
                                                                (MuleApplication.Instance.Version.Major << 17) |
                                                                (MuleApplication.Instance.Version.Minor << 10) |
                                                                (MuleApplication.Instance.Version.Build << 7));
                tagMuleVersion.WriteTagToFile(data);

                Packet packet = MuleApplication.Instance.NetworkObjectManager.CreatePacket(data);
                packet.OperationCode = OperationCodeEnum.OP_LOGINREQUEST;
                MuleApplication.Instance.Statistics.AddUpDataOverheadServer(packet.Size);
                SendPacket(packet, true, sender);
            }
            else if (sender.ConnectionState == ConnectionStateEnum.CS_CONNECTED)
            {
                MuleApplication.Instance.Statistics.Reconnects++;
                MuleApplication.Instance.Statistics.ServerConnectTime = MpdUtilities.GetTickCount();
                IsConnected      = true;
                connectedSocket_ = sender;
                StopConnectionTry();
                MuleApplication.Instance.SharedFiles.ClearED2KPublishInfo();
                MuleApplication.Instance.SharedFiles.SendListToServer();

                if (MuleApplication.Instance.Preference.DoesAddServersFromServer)
                {
                    Packet packet =
                        MuleApplication.Instance.NetworkObjectManager.CreatePacket(
                            OperationCodeEnum.OP_GETSERVERLIST, 0);
                    MuleApplication.Instance.Statistics.AddUpDataOverheadServer(packet.Size);
                    SendPacket(packet, true);
                }
            }
        }