コード例 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ServerConnection"/> class.
 /// </summary>
 /// <param name="port">The port.</param>
 /// <param name="ch">The ch.</param>
 public ServerConnection(int port, IClientHandler ch)
 {
     this.port            = port;
     this.ch              = ch;
     this.keepOpen        = "keep open";
     this.closeConnection = "close connection";
 }
コード例 #2
0
        private void NewMethod()
        {
            // This server generates a new connection for each client
            while (true)
            {
                TcpClient client = listener.AcceptTcpClient();

                // waiting for client that wants to connect and accept it
                client = listener.AcceptTcpClient();
                //  try
                {
                    Console.WriteLine("Client connected");
                    // handle this client (seperate thread)
                    IClientHandler currClientHandler = new IClientHandler(client);
                    currClientHandler.userConnected    += CurrClientHandler_userConnected;
                    currClientHandler.userDisconnected += CurrClientHandler_userDisconnected;
                    currClientHandler.userSentMessage  += CurrClientHandler_userSentMessage;
                    System.Threading.ThreadStart ts = new System.Threading.ThreadStart(currClientHandler.HandleClient);
                    System.Threading.Thread      t  = new System.Threading.Thread(ts);
                    t.Start();
                }
                //    catch (Exception ex)
                //    {
                //        Console.WriteLine(ex.Message);
                //   }
            }
        }
コード例 #3
0
ファイル: Server.cs プロジェクト: KarinHaim/MazeProject
        public Server(IClientHandler ch)
        {
            this.ch = ch;
            string     ipAddress = ConfigurationManager.AppSettings["ip"];
            IPEndPoint ep        = new IPEndPoint(IPAddress.Parse(ipAddress), Convert.ToInt32(ConfigurationManager.AppSettings["port"]));

            listener = new TcpListener(ep);
        }
コード例 #4
0
        private void CurrClientHandler_userSentMessage(IClientHandler sender, string MessageContent)
        {
            IClientHandler c = sender;

            Console.WriteLine(c.UserName + " sending message: " + MessageContent);
            sendMessageToAllUsers(c, MessageContent);

            // notifty the graphics
            UserSentMessage(this, MessageContent);
        }
コード例 #5
0
        private void CurrClientHandler_userDisconnected(IClientHandler sender, string MessageContent)
        {
            IClientHandler c = sender;

            connectedUsers.Remove(c.UserName);
            Console.WriteLine(c.UserName + " has disconnected");

            sendMessageToAllUsers(c, MessageContent);

            // notifty the graphics
            UserDisconnected(this, c.UserName);
        }
コード例 #6
0
        private void CurrClientHandler_userConnected(IClientHandler sender, string MessageContent)
        {
            IClientHandler c = sender;

            connectedUsers.Add(c.UserName, c);

            Console.WriteLine(c.UserName + " has connected");

            // send message to all users
            sendMessageToAllUsers(c, MessageContent);

            // notifty the graphics
            UserConected(this, c.UserName);
        }
コード例 #7
0
        private void sendMessageToAllUsers(IClientHandler sendingUser, string MessageContent)
        {
            IClientHandler c = sendingUser;

            Console.WriteLine(c.UserName + " sendMessageToAllUsers message: " + MessageContent);


            foreach (KeyValuePair <string, IClientHandler> currUser in connectedUsers)
            {
                if (c != currUser.Value)
                {
                    try
                    {
                        currUser.Value.sendMessage(MessageContent);
                    }
                    catch
                    {
                        Console.WriteLine("error sending message to: " + currUser.Key);
                    }
                }
            }
        }
コード例 #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Controller"/> class.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="clientHandler">The client handler.</param>
        /// <exception cref="System.ArgumentNullException">
        /// model
        /// or
        /// clientHandler
        /// </exception>
        public Controller(IModel model, IClientHandler clientHandler)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            if (clientHandler == null)
            {
                throw new ArgumentNullException(nameof(clientHandler));
            }
            this.model         = model;
            this.clientHandler = clientHandler;

            commands = new Dictionary <string, ICommand>();
            commands.Add("generate", new GenerateCommand(model));
            commands.Add("close", new CloseCommand(model));
            commands.Add("join", new JoinCommand(model));
            commands.Add("play", new PlayCommand(model));
            commands.Add("solve", new SolveCommand(model));
            commands.Add("list", new ListCommand(model));
            commands.Add("start", new StartCommand(model));
        }
コード例 #9
0
 /// <summary>
 /// constructor
 /// </summary>
 /// <param name="port">of the connection</param>
 /// <param name="ip">of the connection</param>
 /// <param name="ch">to handle incomming clients</param>
 public Server(int port, IPAddress ip, IClientHandler ch)
 {
     this.ch = ch;
     ep      = new IPEndPoint(ip, port);
 }
コード例 #10
0
ファイル: Server.cs プロジェクト: haim6678/Maze-game
 /// <summary>
 /// Initializes a new instance of the <see cref="Server"/> class.
 /// </summary>
 /// <param name="port">The port.</param>
 /// <param name="ch">The ch.</param>
 public Server(int port, IClientHandler ch)
 {
     this.port = port;
     this.ch   = ch;
     listener  = null;
 }
コード例 #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Server"/> class.
 /// </summary>
 /// <param name="port">The port.</param>
 /// <param name="ch">The ch.</param>
 public Server(int port, IClientHandler ch)
 {
     this.port = port;
     this.ch   = ch;
 }
コード例 #12
0
 /// <summary>
 /// Sets the client handler.
 /// </summary>
 /// <param name="ch">The ch.</param>
 public void SetClientHandler(IClientHandler ch)
 {
     this.clientHandler = ch;
 }
コード例 #13
0
 /// <summary>
 ///     constructor of the <see cref="Server" /> class.
 /// </summary>
 /// <param name="ch">The client handler.</param>
 public Server(IClientHandler ch)
 {
     _ch = ch;
 }
コード例 #14
0
 /// <summary>
 /// Add a <see cref="ClientHandler" />.
 /// </summary>
 /// <param name="userId">The client's <see cref="User" /> Id.</param>
 /// <param name="clientHandler">The <see cref="ClientHandler" /> to add.</param>
 public void AddClientHandler(int userId, IClientHandler clientHandler)
 {
     clientHandlersIndexedByUserId.Add(userId, clientHandler);
 }
コード例 #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MyServer"/> class.
 /// </summary>
 /// <param name="port">The port.</param>
 /// <param name="ch">The ch.</param>
 public MyServer(string port, IClientHandler ch)
 {
     this.portNum = int.Parse(port);
     this.ch      = ch;
 }