Exemplo n.º 1
0
        public static UDPBoxBroadcast GenerateStandardUDPBoxBroadcastAndSetup(
            UDPBox_UDPClient udpClient
            , int broadcastSendPort
            , string netPrefixIP
            , UDPBoxContainer container)
        {
            var broadcast        = new UDPBoxBroadcast(udpClient, broadcastSendPort);
            var broadcastPackage = new BroadcastPackage(container.PackageHeadBytes);

            if (container.IsMaster)
            {
                broadcast.ListenBroadcast((bytes, endPoint) =>
                {
                    if (UDPBoxUtility.PackageIsBroken(bytes, container.PackageHeadBytes))
                    {
                        return;
                    }
                    if (!UDPBoxUtility.ComparePackageID(bytes, container.PackageHeadBytes, broadcastPackage.ID))
                    {
                        return;
                    }

                    if (!broadcastPackage.Deserialize(bytes))
                    {
                        return;
                    }

                    var ipEndPoint = new IPEndPoint(IPAddress.Parse(broadcastPackage.IpAddress), broadcastPackage.BeginPort);
                    if (ipEndPoint.Address.Equals(container.SelfIPAddress) &&
                        ipEndPoint.Port < container.UdpBoxEndPort && ipEndPoint.Port >= container.UdpBoxBeginPort)
                    {
                        return;
                    }
                    //Avoid self connect to the self.

                    var establishConnectPackage        = new EstablishConnectPackage(container.PackageHeadBytes);
                    establishConnectPackage.SenderType = EstablishConnectPackage.ESenderType.Server;
                    establishConnectPackage.IpAddress  = container.SelfIPAddress.ToString();
                    establishConnectPackage.BeginPort  = container.UdpBoxBeginPort;
                    establishConnectPackage.EndPort    = container.UdpBoxEndPort;
                    establishConnectPackage.IsReceipt  = false;
                    container.SendUDPMessage(establishConnectPackage.Serialize(), ipEndPoint);
                    //Server notify to client.
                });
            }
            else
            {
                broadcastPackage.IpAddress = container.SelfIPAddress.ToString();
                broadcastPackage.BeginPort = container.UdpBoxBeginPort;
                broadcastPackage.EndPort   = container.UdpBoxEndPort;

                broadcast.StartBroadcast(broadcastPackage.Serialize(), netPrefixIP);
                //Client through broadcast notify to server.
            }

            return(broadcast);
        }
Exemplo n.º 2
0
        public static UDPBox_UDPClient[] GeterateUdpClientsArray(int beginPort, int endPort)
        {
            var result = new UDPBox_UDPClient[endPort - beginPort];

            for (int i = 0; i < result.Length; i++)
            {
                result[i] = GeterateUdpClient(beginPort + i);
            }

            return(result);
        }
Exemplo n.º 3
0
        public void Start(bool isMaster)
        {
            IsMaster = isMaster;

            if (mUseInternalBroadcastLogic)
            {
                mBroadcastUdpClient = UDPBoxFactory.GeterateUdpClient(BroadcastListenPort);
                mUDPBoxBroadcast    = UDPBoxFactory.GenerateStandardUDPBoxBroadcastAndSetup(mBroadcastUdpClient, BroadcastSendPort, BroadcastNetPrefixIP, this);
            }

            UDPBox.RegistMessageIntercept(InterceptAndUpdateConnectState);
            UDPBox.RegistWorkThreadOperate(RefreshConnectStateInWorkThread);
            UDPBox.Start();
        }
Exemplo n.º 4
0
        public static UDPBox_UDPClient GeterateUdpClient(int port)
        {
            var mUdpClient = new UDPBox_UDPClient(port);

            mUdpClient.Client.SendTimeout    = 1000;
            mUdpClient.Client.ReceiveTimeout = 10000;

            uint IOC_IN            = 0x80000000;
            uint IOC_VENDOR        = 0x18000000;
            uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;

            mUdpClient.Client.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);
            //屏蔽目标机器没有接收逻辑时报错。

            return(mUdpClient);
        }
Exemplo n.º 5
0
        public void Initialization(string packageHead, bool useInternalBroadcastLogic)
        {
            mUseInternalBroadcastLogic = useInternalBroadcastLogic;

            var udpClientArray = new UDPBox_UDPClient[UdpBoxEndPort - UdpBoxBeginPort];

            for (int i = 0, port = UdpBoxBeginPort; port < UdpBoxEndPort; i++, port++)
            {
                udpClientArray[i] = UDPBoxFactory.GeterateUdpClient(port);
            }
            UDPBox.Initialization(udpClientArray, packageHead);

            mEstablishConnectPackage = new EstablishConnectPackage(PackageHeadBytes);
            mPingPongPackageTemplate = new PingPongPackage(PackageHeadBytes);
            SelfIPAddress            = SelfIPAddress ?? UDPBoxUtility.GetSelfIP(BroadcastNetPrefixIP);
        }
Exemplo n.º 6
0
 public UDPBoxBroadcast(UDPBox_UDPClient udpClient, int broadcastPort)
 {
     mBroadcastPort = broadcastPort;
     mUdpClient     = udpClient;
     mIsReleased    = false;
 }