示例#1
0
        private void CommandExecuter(ServerComunication serverComunication)
        {
            Command command = Interpreter.GetCommand(serverComunication.command);


            if (command == Command.UserVariable)
            {
                AddUserVariable(serverComunication.parameters);
            }
            else if (command == Command.CreateRoom)
            {
                RoomConfig room = JsonConvert.DeserializeObject <RoomConfig>(serverComunication.parameters);
                onClientNeedToCreateRoom?.Invoke(id, room.RoomName, room.MaxPlayersInRoom);
            }
            else if (command == Command.JoinInRoom)
            {
                onClientNeedToJoinRoom?.Invoke(id, serverComunication.parameters);
                updateClients?.Invoke(room);
            }
            else if (command == Command.ExitRoom)
            {
                onClientNeedToExitRoom?.Invoke(id, room.RoomName);
                updateClients?.Invoke(room);
            }
            else if (command == Command.InfoMessage)
            {
                MessageInfo message = JsonConvert.DeserializeObject <MessageInfo>(serverComunication.parameters);

                onMessageInfo?.Invoke(message);
            }
        }
示例#2
0
        //Receive callback.
        private void ReceiveCallback(IAsyncResult _result)
        {
            try
            {
                //Lenght of message;
                int _byteLength = stream.EndRead(_result);


                if (_byteLength <= 0)
                {
                    ClientDisconnect();
                    // TODO: disconnect
                    return;
                }
                byte[] copyBuffer = new byte[dataBufferSize];

                Array.Copy(receiveBuffer, copyBuffer, _byteLength);



                //Decode message


                /*string datareceived = Encoding.UTF8.GetString(copyBuffer);
                 *
                 * Console.WriteLine("\n  Data Received From cliente id: " + id);
                 * Console.WriteLine("\n"+ datareceived + "\n");
                 * Console.WriteLine("-----------------\n");
                 * Console.WriteLine("Parsing.... \n ");*/
                Packet pacote = new Packet(copyBuffer);
                //Packet pacote2 =  new Packet(datareceived);
                byte[] TT = new byte[pacote.PackageLenght()];
                Array.Copy(copyBuffer, 4, TT, 0, pacote.PackageLenght());
                string datareceived = Encoding.UTF8.GetString(TT);

                //Unpack message to parse
                ServerComunication serverComunication = CommandPackager.UnpackCommand(datareceived);
                receiveBuffer = null;
                receiveBuffer = new byte[dataBufferSize];
                //Register Calback Again.

                //if ocurred an error parsing.
                if (serverComunication != null)
                {
                    Console.WriteLine("Parsed, executing Command \n ");
                    CommandExecuter(serverComunication);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error receiving data: " + e);
            }
            stream.BeginRead(receiveBuffer, 0, dataBufferSize, ReceiveCallback, null);
        }
示例#3
0
        public static ServerComunication UnpackCommand(string value)
        {
            try
            {
                ServerComunication serverComunication = JsonConvert.DeserializeObject <ServerComunication>(value);
                return(serverComunication);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error parsing command" + e);
            }

            return(null);
        }
示例#4
0
        public void SendMessageInfo(string message, int id)
        {
            //Create User Variable
            MessageInfo messageInfo = new MessageInfo()
            {
                message = message,
                id      = id
            };

            //Serialize to json
            string var = JsonConvert.SerializeObject(messageInfo);

            //Serialize Comunication to Json
            ServerComunication serverComunication = new ServerComunication()
            {
                command    = Enum.GetName(typeof(Command), Command.InfoMessage),
                parameters = JsonConvert.SerializeObject(messageInfo)
            };

            //Send
            Send(JsonConvert.SerializeObject(serverComunication));
        }
示例#5
0
        private void UpdateInfoFromUserInRoom(Room room)
        {
            if (room.RoomName == null || room.RoomName == "")
            {
                return;
            }

            Clients _clients = new Clients();

            _clients.clients = GetAllUsersFromRoom(room);
            ServerComunication serverComunication = new ServerComunication()
            {
                command    = Enum.GetName(typeof(Command), Command.UpdateClientsInfo),
                parameters = JsonConvert.SerializeObject(_clients)
            };



            foreach (var user in room.clientsInRoom)
            {
                user.Send(CommandPackager.PackCommand(serverComunication));
            }
        }
示例#6
0
        public static string PackCommand(ServerComunication serverComunication)
        {
            string ComandPackage = JsonConvert.SerializeObject(serverComunication);

            return(ComandPackage);
        }