private void UpdateRooms() { List <RoomInfo> roomsInfo = new List <RoomInfo>(); foreach (var _room in rooms) { roomsInfo.Add(new RoomInfo() { RoomName = _room.RoomName, MaxPlayersInRoom = _room.MaxPlayersInRoom, clientsInRoom = _room.clientsInRoom.Count }); } string content = JsonConvert.SerializeObject(new UpdateRoomsInfo() { rooms = roomsInfo }); foreach (var _client in clients) { if (_client.Value.isUsed) { _client.Value.Send(CommandPackager.PackCommand(new ServerComunication() { command = Enum.GetName(typeof(Command), Command.UpdateRooms), parameters = content })); } } }
//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); }
public void SendEventResult(EventTrigger eventTrigger) { string package = CommandPackager.PackCommand( new ServerComunication() { command = Interpreter.PackComandType(Command.Event), parameters = JsonConvert.SerializeObject(eventTrigger) } ); Send(package); }
private void UDPCallBack(IAsyncResult result) { try { IPEndPoint _clientEndPoint = new IPEndPoint(IPAddress.Any, 0); byte[] _data = udpListener.EndReceive(result, ref _clientEndPoint); string msg = Encoding.UTF8.GetString(_data); Packet package = new Packet(_data); Console.WriteLine("UDP Received"); UDPClientIdentity uDPClientIdentity = JsonConvert.DeserializeObject <UDPClientIdentity>(package.GetData()); ThreadManager.ExecuteOnMainThread(() => { if (clients[uDPClientIdentity.id].isUsed) { if (clients[uDPClientIdentity.id].udpClient == null) { Console.WriteLine("added" + _clientEndPoint.Address.ToString()); clients[uDPClientIdentity.id].udpClient = _clientEndPoint; } if (uDPClientIdentity.content == "Connect") { string confirm = CommandPackager.PackCommand( new ServerComunication() { command = Interpreter.PackComandType(Command.ConnectionID), parameters = "Conected" }); Packet confirmPackege = new Packet(confirm); udpListener.BeginSend(confirmPackege.writeData(), confirmPackege.writeData().Length, _clientEndPoint.Address.ToString(), _clientEndPoint.Port, null, null); } if (uDPClientIdentity.content != "Connect") { SendUDPTrafficVar(package.GetData(), uDPClientIdentity.id); } } }); } catch (Exception e) { Console.WriteLine("error \n " + e); } udpListener.BeginReceive(UDPCallBack, null); }
public void Connect(TcpClient _socket) { //Connect client clientSock = _socket; //Buffer Configs clientSock.ReceiveBufferSize = dataBufferSize; clientSock.SendBufferSize = dataBufferSize; //save stream socket in the class stream = clientSock.GetStream(); //receiveBufferSize; receiveBuffer = new byte[dataBufferSize]; //callBack to read messages from clients. stream.BeginRead(receiveBuffer, 0, dataBufferSize, ReceiveCallback, null); Send(CommandPackager.PackCommand(new ServerComunication() { command = Interpreter.PackComandType(Command.ConnectionID), parameters = id.ToString() })); }
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)); } }
public void SendUDPTrafficVar(string value, int id) { string data = CommandPackager.PackCommand( new ServerComunication() { command = Interpreter.PackComandType(Command.Event), parameters = JsonConvert.SerializeObject(new EventTrigger() { _event = ResultEvent.TrafficVar, eventResultResponse = value }) }); Packet package = new Packet(data); if (clients[id].room.RoomName == null || clients[id].room.RoomName == "") { Console.WriteLine("The player is not in a room"); return; } for (int x = 0; x < rooms.Count; x++) { foreach (var item in clients) { if (item.Value.udpClient != null) { Console.WriteLine("Send User var from Id : " + id); udpListener.BeginSend(package.writeData(), package.writeData().Length, item.Value.udpClient.Address.ToString(), item.Value.udpClient.Port, null, null); } } } }