Example #1
0
        UdpSocket(UdpPlatform platform, UdpSerializerFactory serializerFactory, UdpConfig config)
        {
            this.platform          = platform;
            this.serializerFactory = serializerFactory;
            this.Config            = config.Duplicate();
            this.configCopy        = config;

            state          = UdpSocketState.Created;
            random         = new System.Random();
            stats          = new UdpStats();
            availableEvent = new AutoResetEvent(false);

            if (this.Config.NoiseFunction == null)
            {
                this.Config.NoiseFunction = delegate() { return((float)random.NextDouble()); };
            }

            readStream  = new UdpStream(new byte[config.PacketSize * 2]);
            writeStream = new UdpStream(new byte[config.PacketSize * 2]);
            streamPool  = new UdpStreamPool(this);

            eventQueueIn  = new Queue <UdpEvent>(config.InitialEventQueueSize);
            eventQueueOut = new Queue <UdpEvent>(config.InitialEventQueueSize);

            threadSocket              = new Thread(NetworkLoop);
            threadSocket.Name         = "udpkit thread";
            threadSocket.IsBackground = true;
            threadSocket.Start();
        }
Example #2
0
        UdpSocket(UdpPlatform platform, UdpSerializerFactory serializerFactory, UdpConfig config)
        {
            this.platform          = platform;
            this.serializerFactory = serializerFactory;
            this.Config            = config.Duplicate();

            random        = new Random(500);
            state         = udpSocketState.Created;
            receiveBuffer = new byte[config.MtuMax * 2];
            objectBuffer  = new byte[config.MtuMax * 2];

            eventQueueIn  = new Queue <UdpEvent>(config.InitialEventQueueSize);
            eventQueueOut = new Queue <UdpEvent>(config.InitialEventQueueSize);

            threadSocket              = new Thread(NetworkLoop);
            threadSocket.Name         = "udpkit thread";
            threadSocket.IsBackground = true;
            threadSocket.Start();
        }
Example #3
0
        public static UdpSocketMultiplexer CreateMultiplexer <TPlatform, TSerializer> (UdpIPv4Address address, ushort portMin, ushort portMax, UdpConfig config)
            where TPlatform : UdpPlatform, new()
            where TSerializer : UdpSerializer, new()
        {
            if (portMin > portMax)
            {
                throw new ArgumentOutOfRangeException("portMin was larger then portMax");
            }

            List <UdpSocket> sockets = new List <UdpSocket>();

            for (; portMin <= portMax; portMin += 1)
            {
                // create and start socket
                UdpSocket s = Create <TPlatform, TSerializer>(config);
                s.Start(new UdpEndPoint(address, portMin));

                // add to list
                sockets.Add(s);
            }

            return(CreateMultiplexer(sockets.ToArray()));
        }
Example #4
0
 public static UdpSocket Create <TPlatform> (UdpConfig config)
     where TPlatform : UdpPlatform, new()
 {
     return(Create(new TPlatform(), config));
 }
Example #5
0
 public static UdpSocket Create(UdpPlatform platform, UdpConfig config)
 {
     return(Create(platform, () => new UdpStreamSerializer(), config));
 }
Example #6
0
 public static UdpSocket Create <TPlatform, TSerializer> (UdpConfig config)
     where TPlatform : UdpPlatform, new()
     where TSerializer : UdpSerializer, new()
 {
     return(new UdpSocket(new TPlatform(), () => new TSerializer(), config));
 }
Example #7
0
        /*
         #region Partial Methods
         * void DelayPacket (UdpEndPoint ep, byte[] data, int length);
         * void RecvDelayedPackets ();
         #endregion*/

        public static UdpSocket Create(UdpPlatform platform, UdpSerializerFactory serializer, UdpConfig config)
        {
            return(new UdpSocket(platform, serializer, config));
        }
Example #8
0
        static void Server()
        {
            #if DISABLE_AUTO_ACCEPT
            UdpConfig config = new UdpConfig();
            config.AutoAcceptIncommingConnections = false;
            #else
            UdpConfig config = new UdpConfig();
            #endif
            UdpSocket server = UdpSocket.Create<UdpPlatformManaged, DummySerializer>(config);
            server.Start(new UdpEndPoint(UdpIPv4Address.Localhost, 14000));

            while (true) {
                UdpEvent ev = default(UdpEvent);

                while (server.Poll(ref ev)) {
                    UdpLog.User("Event raised {0}", ev.EventType);

                    switch (ev.EventType) {
                        case UdpEventType.Connected:
                            UdpLog.User("Client connected from {0}, total clients connected: {1}", ev.Connection.RemoteEndPoint, server.ConnectionCount);
                            break;

            #if ENABLE_MANUAL_ACCEPT
                        case UdpEventType.ConnectRequest:
                            UdpLog.User("Connection requested from {0}", ev.EndPoint);
                            server.Accept(ev.EndPoint);
                            break;
            #endif
                    }
                }

                // Simulate ~60fps game loop
                Thread.Sleep(16);
            }
        }