コード例 #1
0
ファイル: Program.cs プロジェクト: ystannly/udpkit
        static void Client(int count)
        {
            // random object used to generate random port
            Random rnd = new Random();

            UdpSocket client = UdpSocket.Create <UdpPlatformManaged, DummySerializer>();

            client.Start(UdpEndPoint.Any);
            client.Connect(new UdpEndPoint(UdpIPv4Address.Localhost, (ushort)(14000 + rnd.Next(count))));

            while (true)
            {
                UdpEvent ev;

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

                    switch (ev.EventType)
                    {
                    case UdpEventType.Connected:
                        UdpLog.User("Connected to server at {0}", ev.Connection.RemoteEndPoint);
                        break;
                    }
                }

                // Simulate ~60fps game loop
                Thread.Sleep(16);
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: wuzhen/udpkit
        static void Client()
        {
            UdpSocket client = UdpSocket.Create <UdpPlatformManaged, DummySerializer>();

            client.Start(UdpEndPoint.Any);
            client.Connect(new UdpEndPoint(UdpIPv4Address.Localhost, 14000));

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

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

                    switch (ev.EventType)
                    {
                    case UdpEventType.Connected:
                        UdpLog.User("Connected to server at {0}", ev.Connection.RemoteEndPoint);
                        break;

#if DISABLE_AUTO_ACCEPT
                    case UdpEventType.ConnectFailed:
                        UdpLog.User("Connection to {0} failed", ev.EndPoint);
                        break;
#endif
                    }
                }

                // Simulate ~60fps game loop
                Thread.Sleep(16);
            }
        }
コード例 #3
0
ファイル: main.cs プロジェクト: wuzhen/udpkit
        static void EventLoop(UdpSocket socket)
        {
            UdpConnection c = null;

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

                if (socket.Poll(ref ev))
                {
                    UdpLog.User(ev.EventType.ToString());

                    switch (ev.EventType)
                    {
                    case UdpEventType.ConnectRequest:
                        socket.Accept(ev.EndPoint);
                        break;

                    case UdpEventType.Connected:
                        c = ev.Connection;
                        break;
                    }
                }

                if (c != null)
                {
                    c.Send(10u);
                }

                Thread.Sleep(100);
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: wuzhen/udpkit
        public bool Process()
        {
            UdpEvent         ev  = default(UdpEvent);
            ConnectionObject co  = null;
            pair             val = default(pair);

            if (socket.Poll(ref ev))
            {
                switch (ev.EventType)
                {
                case UdpEventType.Connected:
                    ev.Connection.UserToken = co = new ConnectionObject();
                    co.connection           = ev.Connection;
                    connections.Add(ev.Connection);

                    for (int i = 0; i < 16; ++i)
                    {
                        co.QueueNext();
                        co.Send();
                    }

                    break;

                case UdpEventType.ObjectLost:
                    val = (pair)ev.Object;
                    co  = (ConnectionObject)ev.Connection.UserToken;
                    co.Nack(val);
                    co.Send();
                    break;

                case UdpEventType.ObjectDelivered:
                    val = (pair)ev.Object;
                    co  = (ConnectionObject)ev.Connection.UserToken;
                    co.Ack(val);
                    co.QueueNext();
                    co.Send();
                    break;

                case UdpEventType.ObjectReceived:
                    val = (pair)ev.Object;
                    co  = (ConnectionObject)ev.Connection.UserToken;
                    co.Recv(val);
                    co.DequeueReceived();
                    break;

                case UdpEventType.ObjectRejected:
                    Console.WriteLine("REJECTED");
                    break;

                case UdpEventType.ObjectSendFailed:
                    Console.WriteLine("SENDFAILED");
                    break;
                }

                return(true);
            }

            return(false);
        }
コード例 #5
0
        public void Loop()
        {
            int      connections = 0;
            UdpEvent ev;

            while (true)
            {
                while (socket.Poll(out ev))
                {
                    switch (ev.EventType)
                    {
                    case UdpEventType.Connected:
                        ++connections;

                        // log in local console
                        UdpLog.User("Client connected from {0}, total clients {1}", ev.Connection.RemoteEndPoint, connections);

                        // send welcome message
                        ev.Connection.Send("Welcome to the chat server!");

                        // send message to all other clients
                        SendToAllClients("Client connected from {0}", ev.Connection.RemoteEndPoint);

                        // add to client list
                        clients.Add(ev.Connection);
                        break;

                    case UdpEventType.Disconnected:
                        --connections;

                        // log in local console
                        UdpLog.User("Client at {0} disconnected, total clients {1}", ev.Connection.RemoteEndPoint, connections);

                        // remove from client list
                        clients.Remove(ev.Connection);

                        // Send message to all others
                        SendToAllClients("Client at {0} disconnected", ev.Connection.RemoteEndPoint);
                        break;

                    // When we receive, just forward to all clients
                    case UdpEventType.ObjectReceived:
                        SendToAllClients(ev.Object as string);
                        break;

                    // If lost, resend to connection it was lost on
                    case UdpEventType.ObjectLost:
                        ev.Connection.Send(ev.Object);
                        break;
                    }
                }

                // Simulate ~60fps game loop
                Thread.Sleep(16);
            }
        }
コード例 #6
0
        public void Loop()
        {
            StreamReader input = new StreamReader(Console.OpenStandardInput());

            Char[]        buffer     = new Char[1024];
            ReadLine      read       = Console.ReadLine;
            IAsyncResult  result     = null;
            UdpConnection connection = null;

            UdpEvent ev;

            while (true)
            {
                while (socket.Poll(out ev))
                {
                    switch (ev.EventType)
                    {
                    case UdpEventType.Connected:
                        UdpLog.User("Connected to server at {0}", ev.Connection.RemoteEndPoint);
                        connection = ev.Connection;
                        break;

                    case UdpEventType.Disconnected:
                        UdpLog.User("Disconnected from server at {0}", ev.Connection.RemoteEndPoint);
                        connection = null;
                        break;

                    case UdpEventType.ObjectReceived:
                        Console.WriteLine(": " + (ev.Object as string));
                        break;
                    }
                }

                if (result == null)
                {
                    result = read.BeginInvoke(null, null);
                }

                if (result.IsCompleted)
                {
                    if (connection != null)
                    {
                        connection.Send(read.EndInvoke(result));
                    }

                    result = read.BeginInvoke(null, null);
                }

                // Simulate ~60fps game loop
                Thread.Sleep(16);
            }
        }
コード例 #7
0
    void Update()
    {
        UdpEvent ev = default(UdpEvent);

        while (socket.Poll(ref ev))
        {
            switch (ev.EventType)
            {
            case UdpEventType.Connected:
                UdpLog.User("Client connect from {0}", ev.Connection.RemoteEndPoint);
                break;
            }
        }
    }
コード例 #8
0
        //for out event update..
        public void Update()
        {
            UdpEvent ev;

            byte[] buffer;
            while (server.Poll(out ev))
            {
                switch (ev.EventType)
                {
                case UdpEventType.Connected:
                {
                    Log.info("P2PClientNetWork", "Client connect from :" + ev.Connection.RemoteEndPoint);
                    ProcessUdpConnection(ev.Connection);
                    break;
                }

                case UdpEventType.ObjectReceived:
                {
                    buffer = ev.Object as byte[];
                    Log.info("P2PClientNetWork", "Client received from " + ev.Connection.RemoteEndPoint + " DataLength:" + buffer.Length);
                    CtrlOwner.Reader.ReadP2PData(buffer, buffer.Length);
                    break;
                }

                case UdpEventType.Disconnected:
                {
                    Log.info("P2PClientNetWork", "Client disconnect from :" + ev.Connection.RemoteEndPoint);
                    ProcessUdpDisConnection(ev.Connection);
                    break;
                }

                case UdpEventType.ObjectLost:
                {
                    Log.info("P2PClientNetWork", "Client lost, reconection :" + ev.Connection.RemoteEndPoint);
                    ev.Connection.Send(ev.Object);
                    break;
                }

                default:
                    Log.info("P2PClientNetWork", "Receive event, not process:" + ev.EventType.ToString());
                    break;
                }
            }
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: wuzhen/udpkit
        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);
            }
        }
コード例 #10
0
ファイル: Program.cs プロジェクト: jcherrabi/OpenThreadDotNet
        private static void UDPListenerThread(IPv6Address ipAddress, ushort port)
        {
            UdpSocket receiver = new UdpSocket();

            receiver.Bind(ipAddress, port);
            IPv6EndPoint remoteIp = null;

            isUdpListenerRunning = true;

            while (isUdpListenerRunning)
            {
                if (receiver.Poll(-1, SelectMode.SelectRead))
                {
                    byte[] data    = receiver.Receive(ref remoteIp);
                    string message = Encoding.ASCII.GetString(data);
                    Console.WriteLine("\n");
                    Console.WriteLine("{0} bytes from {1} {2} {3}", message.Length, remoteIp.Address, remoteIp.Port, message);
                    Console.WriteLine(">");
                }
            }

            receiver.Close();
            receiver = null;
        }
コード例 #11
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                string[] ports = SerialPort.GetPortNames();
                Console.WriteLine("COM port parameter not provided.");
                Console.WriteLine("Available serial ports: ");
                foreach (var serialPort in ports)
                {
                    Console.WriteLine(serialPort);
                }
                Console.ReadKey();
                return;
            }

            StreamUART uartStream = new StreamUART(args[0]);

            ncpInterface = new NcpInterface();

            try
            {
                ncpInterface.Open(uartStream);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
                return;
            }

            // NetworkingInterface.SetupInterface(ncpInterface);

            Console.Write("Networkname:");
            string networkname = Console.ReadLine();

            Console.Write("Channel:");
            byte channel = Convert.ToByte(Console.ReadLine());

            Console.Write("Masterkey:");
            string masterkey = Console.ReadLine();

            Console.Write("Panid:");
            ushort panid = Convert.ToUInt16(Console.ReadLine());

            Console.Write("Listener port:");
            ushort port = Convert.ToUInt16(Console.ReadLine());

            try
            {
                ncpInterface.Form(networkname, channel, masterkey, panid);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
                return;
            }

            UdpSocket receiver = new UdpSocket();

            receiver.Bind(IPv6Address.IPv6Any, port);
            IPv6EndPoint remoteIp = null;

            while (true)
            {
                if (receiver.Poll(-1, SelectMode.SelectRead))
                {
                    byte[] data    = receiver.Receive(ref remoteIp);
                    string message = Encoding.ASCII.GetString(data);
                    Console.WriteLine("\n");
                    Console.WriteLine("{0} bytes from {1} {2} {3}", message.Length, remoteIp.Address, remoteIp.Port, message);
                    Console.WriteLine(">");
                }
            }
        }
コード例 #12
0
        public override bool ConnectSync(IPEndPoint localAddress, IPEndPoint remoteAddress, int timeout)
        {
            long num = DateTime.Now.ToFileTime() / 10000L;
            bool result;

            if (IsConnected())
            {
                UdpIPv4Address udpIPv4Address = new UdpIPv4Address(NetUtil.GetLongAddress(remoteAddress.Address.GetAddressBytes()));
                UdpIPv4Address address        = this.udpRemote.Address;
                if (address.Equals(udpIPv4Address) && (int)udpRemote.Port == remoteAddress.Port)
                {
                    result = true;
                    return(result);
                }
                LoggerManager.Instance.Warn("ConnectSync found already connected new addr {0} old addr {1}", remoteAddress.ToString(), udpRemote.ToString());
            }

            if (!IsConnecting())
            {
                udpRemote = new UdpEndPoint(new UdpIPv4Address(NetUtil.GetLongAddress(remoteAddress.Address.GetAddressBytes())), (ushort)remoteAddress.Port);
                remote    = remoteAddress;
                socket.Connect(udpRemote);
            }

            object obj = statusLock;

            Monitor.Enter(obj);
            try
            {
                connectionStatus = ConnectionStatus.Connecting;
            }
            finally
            {
                Monitor.Exit(obj);
            }

            long num2 = DateTime.Now.ToFileTime() / 10000L;

            while (num2 - num < (long)(timeout * 1000))
            {
                UdpEvent ev;
                if (socket.Poll(out ev))
                {
                    UdpEventType eventType = ev.EventType;
                    switch (eventType)
                    {
                    case UdpEventType.ConnectFailed:
                    case UdpEventType.ConnectRefused:
                    case UdpEventType.Connected:
                        ProcessConnect(ev);
                        result = (ev.EventType == UdpEventType.Connected);
                        return(result);

                    case (UdpEventType)5:
                    case (UdpEventType)7:
                        break;

                    default:
                        if (eventType == UdpEventType.ServerForceQuit)
                        {
                            ProcessServerForceQuit(ev);
                            result = false;
                            return(result);
                        }
                        break;
                    }
                }
                Thread.Sleep(10);
                num2 = DateTime.Now.ToFileTime() / 10000L;
            }

            Monitor.Enter(obj = this.statusLock);
            try
            {
                connectionStatus = ConnectionStatus.Disconnected;
            }
            finally
            {
                Monitor.Exit(obj);
            }
            result = false;
            return(result);
        }