Example #1
0
        public Server(int port)
        {
            Contract.Requires(0 < port && port < 65536);

            tcpServer = new TcpServer(port);
            tcpServer.SocketConnected += SocketConnected;
            tcpServer.DataReceived += DataReceived;
            tcpServer.SocketDisconnected += SocketDisconnected;
        }
Example #2
0
        void DataReceived(object sender, TcpServer.DataReceivedEventArgs e)
        {
            Console.WriteLine("Data received!");
            try
            {
                // Converting received data to xml
                var xml = ASCIIEncoding.UTF8.GetString(e.Data, 0, e.Data.Length);
                Console.WriteLine("Data received from {0}: {1}", e.IPEndPoint, xml);
                XElement xmlElement = XElement.Parse(xml);
                Console.WriteLine("Xml data: " + xmlElement);

                // TODO: we're not dealing with splitted message. fix this later

                // Creating message from xml string
                Message message = messageConverter.ConvertFromXml(xmlElement);
                long currentMessageId = message.Id;
                Command command = message.Command;
                switch(command.CommandType)
                {
                    case CommandType.Ack:
                        // Server can't get this command
                        break;
                    case CommandType.Login:
                        HandleLoginMessage(currentMessageId, (LoginCommand)command, e.IPEndPoint);
                        break;
                    case CommandType.UserState:
                        HandleUserStateCommand(currentMessageId, (UserStateCommand)command);
                        break;
                    case CommandType.TextMessage:
                        HandleTextMessageCommand(currentMessageId, (TextMessageCommand)command);
                        break;
                    default:
                        Debug.Assert(false, "Unknown command type: " + command.CommandType);
                        break;
                }
                //tcpServer.SendData(e.Data, e.IPEndPoint);

                //Это может быть регистрационное сообщение
                //var userName = doc.Descendants("userName").SingleOrDefault();

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error processing data. {0}", ex);
            }
        }
Example #3
0
        void SocketDisconnected(object sender, TcpServer.SocketEndPointEventArgs e)
        {
            Console.WriteLine("Socket disconnected from: " + e.IPEndPoint);
            using (usersSync.UseUpgradeableLock())
            {
                // Finding appropriate user
                var userInfo = users.SingleOrDefault(ui => ui.EndPoint == e.IPEndPoint);
                if (userInfo == null)
                {
                    Console.WriteLine("Unknown user disconnected!!");
                    return;
                }

                // Changing it state
                using (usersSync.UseWriteLock())
                {
                    userInfo.OnDisconnect();
                }

                // And sending this information to al users
                SendUpdatedUserState(userInfo);
            }
        }
Example #4
0
 void SocketConnected(object sender, TcpServer.SocketEndPointEventArgs e)
 {
     Console.WriteLine("Socket connected from: {0}", e.IPEndPoint);
 }