예제 #1
0
        /// <summary>
        /// Create a new Server object
        /// </summary>
        /// <param name="port">The port you want to use</param>
        public Server(int port)
        {
            //Initialize the array with a maximum of the MaxClients from the config file.
            client = new Client[Properties.Settings.Default.MaxNumberOfClients];

            //Create a new Listener object
            listener = new Listener(port);
            listener.userAdded += new ConnectionEvent(listener_userAdded);
            listener.Start();

            //Create the readers and writers.
            readStream = new MemoryStream();
            writeStream = new MemoryStream();
            reader = new BinaryReader(readStream);
            writer = new BinaryWriter(writeStream);

            //Set the singleton to the current object
            Server.singleton = this;
        }
예제 #2
0
        public void Start_Listen(string ipAddress, int portNum)
        {
            if (FileBaseDirectory.DlcDir == null || FileBaseDirectory.UserDir == null)
            {
                Console.WriteLine("Please specify file path");
            }

            if (Directory.Exists(FileBaseDirectory.DlcDir) == false)
            {
                Console.WriteLine("No directory on DlcPath");
                return;
            }

            if (Directory.Exists(FileBaseDirectory.UserDir) == false)
            {
                Console.WriteLine("No directory on UserPath");
            }

            _listen = new Listener(_communicatorPool);
            if (ipAddress != null)
            {
                _listen.Host = ipAddress;
            }
            if (portNum > 1023)
            {
                _listen.Port = portNum;
            }
            _listen.CallBackonNewclient += Newclient;//add Newclient callback handler to delegate
            //this delegate is called when new client is accepted
            _listen.ActivateAcception = true;
            _listen.Start(_backlog);
            while (true)
            {
                //              Thread.Sleep(1500);
                Console.ReadLine();
                Console.WriteLine("Communicator Stack size:" + _communicatorPool.StackSize());
            }
        }
예제 #3
0
 private void ServerWindowForm_Load(object sender, EventArgs e)
 {
     //Start the listener
     listener.Start();
 }
예제 #4
0
 public void Start()
 {
     Listener.Start();
     HandleRequests();
 }
예제 #5
0
 private void Main_Load(object sender, EventArgs e)
 {
     listener.Start();
 }
예제 #6
0
 void Main_load(Object sender, EventArgs e)
 {
     listener.Start();
 }
예제 #7
0
파일: Program.cs 프로젝트: hnefatl/RBS
        static void Main(string[] args)
        {
            #region Initialise Data
            using (DataRepository Repo = new DataRepository())
            {
                Repo.Departments.Add(new Department()
                {
                    Name = "Maths"
                });
                Repo.Departments.Add(new Department()
                {
                    Name = "Science"
                });
                Repo.Departments.Add(new Department()
                {
                    Name = "Computing/IT"
                });
                Repo.Departments.Add(new Department()
                {
                    Name = "History"
                });

                Repo.SaveChanges();

                List <string> ComputerNames = GenerateComputerNames();
                Repo.Rooms.Add(new Room()
                {
                    RoomName = "D6", SpecialSeats = 5, StandardSeats = 10, SpecialSeatType = "Computer", Department = Repo.Departments.Single(d => d.Name == "Computing/IT"), ComputerNames = ComputerNames
                });
                Repo.Rooms.Add(new Room()
                {
                    RoomName = "D12", SpecialSeats = 20, StandardSeats = 5, SpecialSeatType = "Workbench", Department = Repo.Departments.Single(d => d.Name == "Computing/IT"), ComputerNames = ComputerNames
                });
                Repo.Rooms.Add(new Room()
                {
                    RoomName = "Library", SpecialSeats = 20, StandardSeats = 30, SpecialSeatType = "Computer", Department = Repo.Departments.Single(d => d.Name == "Science")
                });
                Repo.Rooms.Add(new Room()
                {
                    RoomName = "Sports Hall", SpecialSeats = 0, StandardSeats = 100, SpecialSeatType = "", Department = Repo.Departments.Single(d => d.Name == "Maths")
                });

                //Repo.Periods.Add(new TimeSlot() { Name = "Period 1", Start = new TimeSpan(8, 50, 0), End = new TimeSpan(9, 50, 0) });
                Repo.Periods.Add(new TimeSlot()
                {
                    Name = "Period 1", Start = new TimeSpan(8, 50, 0), End = new TimeSpan(9, 50, 0)
                });
                Repo.Periods.Add(new TimeSlot()
                {
                    Name = "Period 2", Start = new TimeSpan(9, 50, 0), End = new TimeSpan(10, 50, 0)
                });
                Repo.Periods.Add(new TimeSlot()
                {
                    Name = "Period 3", Start = new TimeSpan(11, 10, 0), End = new TimeSpan(12, 10, 0)
                });
                Repo.Periods.Add(new TimeSlot()
                {
                    Name = "Period 4", Start = new TimeSpan(12, 10, 0), End = new TimeSpan(13, 10, 0)
                });
                Repo.Periods.Add(new TimeSlot()
                {
                    Name = "Period 5", Start = new TimeSpan(14, 0, 0), End = new TimeSpan(15, 0, 0)
                });

                string LogonName = DataRepository.Home ? "Keith" : "09135"; // For testing on home versus school computers
                Repo.Students.Add(new Student()
                {
                    FirstName = "Keith", LastName = "Collister", Form = "WT", Year = 13, LogonName = LogonName, Access = AccessMode.Admin
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Dan", LastName = "Wrenn", Form = "MB", Year = 13, LogonName = "09154", Access = AccessMode.Teacher
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Euan", LastName = "Rossie", Form = "WT", Year = 13, LogonName = "09185"
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Max", LastName = "Norman", Form = "WT", Year = 13, LogonName = "Max"
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Peter", LastName = "Champion", Form = "WT", Year = 13, LogonName = "Peter"
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Mia", LogonName = "Mia", LastName = "West", Form = "MB", Year = 13
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Matthew", LogonName = "Matthew", LastName = "Pilkington", Form = "WT", Year = 13
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Kaleb", LogonName = "Kaleb", LastName = "Poole", Form = "BR", Year = 11
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Sam", LogonName = "Sam", LastName = "Kitto", Form = "BR", Year = 11
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Isobel", LogonName = "Isobel", LastName = "Stephens", Form = "MI", Year = 11
                });

                Repo.Subjects.Add(new Subject()
                {
                    SubjectName = "Maths", Colour = Colors.Red
                });
                Repo.Subjects.Add(new Subject()
                {
                    SubjectName = "Physics", Colour = Colors.Orange
                });
                Repo.Subjects.Add(new Subject()
                {
                    SubjectName = "Computing", Colour = Colors.Blue
                });
                Repo.Subjects.Add(new Subject()
                {
                    SubjectName = "History", Colour = Colors.Green
                });

                Repo.SaveChanges();

                Repo.Teachers.Add(new Teacher()
                {
                    Title = "Mrs", LogonName = "mb", FirstName = "Mary", LastName = "Bogdiukiewicz", Department = Repo.Departments.ToList().Where(d => d.Name.Contains("Computing")).Single(), Email = "*****@*****.**"
                });
                Repo.Teachers.Add(new Teacher()
                {
                    Title = "Mr", LogonName = "cn", FirstName = "Patrick", LastName = "Count", Department = Repo.Departments.ToList().Where(d => d.Name.Contains("Computing")).Single(), Email = "*****@*****.**"
                });
                Repo.Teachers.Add(new Teacher()
                {
                    Title = "Mr", LogonName = "jk", FirstName = "James", LastName = "Kenny", Department = Repo.Departments.Where(d => d.Name == "Science").Single(), Email = "*****@*****.**"
                });
                Repo.Teachers.Add(new Teacher()
                {
                    Title = "Mrs", LogonName = "rb", FirstName = "Rosemary", LastName = "Britton", Department = Repo.Departments.Where(d => d.Name == "Maths").Single(), Email = "*****@*****.**"
                });
                Repo.Teachers.Add(new Teacher()
                {
                    Title = "Mrs", LogonName = "ed", FirstName = "Emma", LastName = "Denny", Department = Repo.Departments.ToList().Where(d => d.Name.Contains("History")).Single(), Email = "*****@*****.**"
                });

                Repo.SaveChanges();

                Repo.Classes.Add(new Class()
                {
                    ClassName = "Computing Yr13", Students = Repo.Students.Where(s => s.FirstName == "Keith" || s.FirstName == "Max" || s.FirstName == "Dan" || s.FirstName == "Peter").ToList(), Owner = Repo.Teachers.Where(t => t.LogonName == "mb").Single()
                });
                Repo.Classes.Add(new Class()
                {
                    ClassName = "Maths Yr13", Students = Repo.Students.Where(s => s.Form == "WT").ToList(), Owner = Repo.Teachers.Where(t => t.LogonName == "rb").Single()
                });
                Repo.Classes.Add(new Class()
                {
                    ClassName = "History Yr11", Students = Repo.Students.Where(s => s.Year == 11).ToList(), Owner = Repo.Teachers.Where(t => t.LogonName == "rb").Single()
                });

                Repo.SaveChanges();

                Repo.Bookings.Add(new Booking()
                {
                    Rooms       = Repo.Rooms.ToList().Where(r => r.RoomName[0] == 'D').ToList(),
                    Students    = Repo.Students.ToList().Where(s => s.FullForm == "13WT").ToList(),
                    BookingType = BookingType.Single,
                    Date        = DateTime.Now.Date,
                    Subject     = Repo.Subjects.Where(s => s.SubjectName == "Maths").Single(),
                    Teacher     = Repo.Teachers.Where(t => t.LastName == "Britton").Single(),
                    TimeSlot    = Repo.Periods.Where(p => p.Name == "Period 1").Single(),
                });
                Repo.Bookings.Add(new Booking()
                {
                    Rooms       = Repo.Rooms.Where(r => r.RoomName == "Library").ToList(),
                    Students    = Repo.Students.ToList().Where(s => s.FullForm == "13MB").ToList(),
                    BookingType = BookingType.Weekly,
                    Date        = DateTime.Now.Date,
                    Subject     = Repo.Subjects.Where(s => s.SubjectName == "Computing").Single(),
                    Teacher     = Repo.Teachers.Where(t => t.LastName == "Bogdiukiewicz").Single(),
                    TimeSlot    = Repo.Periods.Where(p => p.Name == "Period 2").Single(),
                });

                Repo.SaveChanges();
            }
            #endregion

            Print("Initialised data", ConsoleColor.Gray);

            // Load the settings from the Settings.txt file
            Settings.Load();
            Print("Loaded settings", ConsoleColor.Gray);


            // Flag to represent whether the server is in the middle of shutting down
            bool Closing = false;
            // Initialise the Listener with the port defined in the settings file
            Listener = new Listener(Settings.Port);
            try
            {
                // Hook up the event handlers - these define the actual action taken by the server
                Listener.ClientConnect         += ClientConnected;
                Listener.ClientDisconnect      += ClientDisconnect;
                Listener.ClientMessageReceived += ClientMessageReceived;

                // Start and don't buffer messages - use events instead
                Listener.Start(false);
                Print("Listener started...", ConsoleColor.Green);

                // Wait for a keypress (to signal exit)
                while (true)
                {
                    ConsoleKey Key = Console.ReadKey(true).Key;
                    if (Key == ConsoleKey.Escape)
                    {
                        break;
                    }
                    else if (Key == ConsoleKey.K)
                    {
                        Listener.Send(new TestMessage("kill"));
                    }
                }

                // Shut down the server, unhook the handlers
                Closing = true;
                Listener.Stop();
                Print("Listener stopped...", ConsoleColor.Red);
                Listener.ClientConnect         -= ClientConnected;
                Listener.ClientDisconnect      -= ClientDisconnect;
                Listener.ClientMessageReceived -= ClientMessageReceived;
            }
            catch (Exception e)
            {
                // If an exception is fired while the server's shutting down,
                // it's usually a send error and is safe to ignore
                if (!Closing)
                {
                    Print("Error: " + e.ToString(), ConsoleColor.Red);
                    Console.ReadKey(true);
                }
            }
            try
            {
                Listener.Dispose();
            }
            catch { }
        }