Beispiel #1
0
        private void Setup(IPEndPoint bindAddress, NetworkHandler <T> handler)
        {
            ByteRecycler.AddPoolSize(2048);
            this.handler = handler;
            handler.SetDarkNetwork(this);
            socket = new Socket(bindAddress.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
#if NETFRAMEWORK
            try
            {
                socket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, 0);
            }
            catch (Exception e)
            {
                //We don't care.
            }
#else
            if (bindAddress.AddressFamily == AddressFamily.InterNetworkV6 && bindAddress.Equals(IPAddress.IPv6Any))
            {
                socket.DualMode = true;
            }
#endif
            socket.Bind(bindAddress);
            running         = true;
            sendThread      = new Thread(new ThreadStart(SendLoop));
            sendThread.Name = "DarkNetworkUDP Send Thread";
            sendThread.Start();
            receiveThread      = new Thread(new ThreadStart(ReceiveLoop));
            receiveThread.Name = "DarkNetworkUDP Receieve Thread";
            receiveThread.Start();
        }
        public void Destroy()
        {
            int usageLeft = Interlocked.Decrement(ref usageCount);

            if (usageLeft == 0)
            {
                if (data != null)
                {
                    ByteRecycler.ReleaseObject(data);
                }
                Recycler <NetworkMessage> .ReleaseObject(this);
            }
        }
        public static NetworkMessage Create(int type, int size, NetworkMessageType sendType)
        {
            NetworkMessage retVal = Recycler <NetworkMessage> .GetObject();

            retVal.type     = type;
            retVal.sendType = sendType;
            retVal.data     = null;
            if (size > 0)
            {
                retVal.data = ByteRecycler.GetObject(size);
            }
            retVal.usageCount = 1;
            return(retVal);
        }