Exemplo n.º 1
0
        private static async void OnPeerDiscovered(ShortGuid peeridentifier, Dictionary <ConnectionType, List <EndPoint> > endPoints)
        {
            var info = new EndPointInfo(Environment.MachineName);

            var eps      = endPoints[ConnectionType.UDP];//.Where(x=>((IPEndPoint)x).Port==7777);
            var localEPs = Connection.AllExistingLocalListenEndPoints();

            foreach (var value in eps)
            {
                var ip = value as IPEndPoint;
                if (ip?.AddressFamily != AddressFamily.InterNetwork)
                {
                    continue;
                }

                foreach (var localEP in localEPs[ConnectionType.UDP])
                {
                    var lEp = (IPEndPoint)localEP;
                    if (lEp.AddressFamily != AddressFamily.InterNetwork)
                    {
                        continue;
                    }
                    if (!ip.Address.IsInSameSubnet(lEp.Address))
                    {
                        continue;
                    }
                    info.IP   = lEp.Address.ToString();
                    info.Port = lEp.Port;
                    await info.Send(ip);
                }
            }
        }
Exemplo n.º 2
0
        public static void Start()
        {
            if (_started)
            {
                return;
            }
            _started = true;

            NetworkComms.DisableLogging();

            var serializer = DPSManager.GetDataSerializer <ProtobufSerializer>();

            NetworkComms.DefaultSendReceiveOptions = new SendReceiveOptions(serializer,
                                                                            NetworkComms.DefaultSendReceiveOptions.DataProcessors, NetworkComms.DefaultSendReceiveOptions.Options);

            PeerDiscovery.EnableDiscoverable(PeerDiscovery.DiscoveryMethod.UDPBroadcast);

            NetworkComms.AppendGlobalIncomingPacketHandler <AndroidInfo>(AndroidInfo.GetHeader(), AndroidHandler.HandShakeHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <StudentInfoRequest>(StudentInfoRequest.GetHeader(), AndroidHandler.StudentInfoRequested);
            NetworkComms.AppendGlobalIncomingPacketHandler <EndPointInfo>(EndPointInfo.GetHeader(), HandShakeHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <GetWork>(GetWork.GetHeader(), GetWorkHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <Login>(Login.GetHeader(), LoginHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <Logout>(Logout.GetHeader(), LogoutHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <SchedulesRequest>(SchedulesRequest.GetHeader(), AndroidHandler.ScheduleRequestHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <EnrollRequest>(EnrollRequest.GetHeader(), AndroidHandler.EnrollRequestHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <RegisterStudent>(RegisterStudent.GetHeader(), AndroidHandler.RegisterStudentHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <SaveWork>(SaveWork.GetHeader(), SaveWorkHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <GetCourses>(GetCourses.GetHeader(), AndroidHandler.GetCoursesHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <GetCoursesDesktop>(GetCoursesDesktop.GetHeader(), GetCoursesHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <Pong>(Pong.GetHeader(), PongHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <EnrollStudent>(EnrollStudent.GetHeader(), EnrollStudentHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <StartEnrollment>(StartEnrollment.GetHeader(), AndroidHandler.StartEnrollmentHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <AddSchedule>(AddSchedule.GetHeader(), AndroidHandler.AddScheduleHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <CommitEnrollment>(CommitEnrollment.GetHeader(), AndroidHandler.CommitEnrollmentHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <StatusRequest>(StatusRequest.GetHeader(), AndroidHandler.StatusRequestHandler);
            NetworkComms.AppendGlobalIncomingPacketHandler <CancelEnrollment>(CancelEnrollment.GetHeader(), AndroidHandler.CancelEnrollmentHandler);
            //  try
            // {
            Connection.StartListening(ConnectionType.UDP, new IPEndPoint(IPAddress.Any, 0), true);
            // }
            // catch (Exception e)
            // {
            //
            // }
        }
Exemplo n.º 3
0
        public static async void HandShakeHandler(PacketHeader packetheader, Connection connection, EndPointInfo ep)
        {
            lock (_clientsLock)
                _handshakeTasks.Enqueue(new Task(async() =>
                {
                    //Get known client or create new one.
                    //  Client client = null;
                    //  lock (_clients)
                    //  {
                    var client = Client.Cache.FirstOrDefault(x => x.IP == ep.IP);
                    if (client == null)
                    {
                        client = new Client()
                        {
                            IP = ep.IP
                        };
                    }
                    // }
                    client.Hostname      = ep.Hostname;
                    client.Port          = ep.Port;
                    client.LastHeartBeat = DateTime.Now;
                    var isNew            = client.Id == 0;
                    await client.SaveAsync();
                    client.IsOnline = true;

                    StartPinger(client);

                    if (isNew)
                    {
                        TerminalLog.Add(client.Id, "Encoder terminal added.");
                    }

                    TerminalLog.Add(client.Id, "Terminal has connected.");

                    var localEPs   = Connection.AllExistingLocalListenEndPoints();
                    var serverInfo = new ServerInfo(Environment.MachineName);
                    var ip         = new IPEndPoint(IPAddress.Parse(ep.IP), ep.Port);

                    foreach (var localEP in localEPs[ConnectionType.UDP])
                    {
                        var lEp = localEP as IPEndPoint;

                        if (lEp == null)
                        {
                            continue;
                        }
                        if (lEp.AddressFamily != AddressFamily.InterNetwork)
                        {
                            continue;
                        }
                        if (!ip.Address.IsInSameSubnet(lEp.Address))
                        {
                            continue;
                        }

                        serverInfo.IP   = lEp.Address.ToString();
                        serverInfo.Port = lEp.Port;
                        await serverInfo.Send(ip);
                        break;
                    }

                    await SendEncoderUpdates(Client.Cache.ToList());

                    await TaskEx.Delay(100);
                }));
            if (_handshakeTasksRunning)
            {
                return;
            }
            _handshakeTasksRunning = true;

            await Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    Task task = null;
                    lock (_clientsLock)
                    {
                        if (_handshakeTasks.Count == 0)
                        {
                            break;
                        }
                        task = _handshakeTasks.Dequeue();

                        if (task == null)
                        {
                            continue;
                        }
                        task.Start();
                        task.Wait();
                    }
                }
                _handshakeTasksRunning = false;
            });
        }