private UserInfo userInfoFromBytes(byte[] buffer, int offset, out int newOffset) { newOffset = offset; var guid = guidFromBytes(buffer, newOffset, out newOffset); var name = PacketHelper.GetStringFromBytes(buffer, newOffset, out newOffset); return(new UserInfo(name, guid)); }
private void handleIncomingPacket(UserThread sender, Packet packet) { int newOffset; if (packet.PacketType == NetConstants.PacketTypes.UserRegister) { var registerString = PacketHelper.GetStringFromBytes(packet.DataBytes, 0, out newOffset); if (registerString == NetConstants.UserRegisterString && !sender.IsRegistered) { var name = PacketHelper.GetStringFromBytes(packet.DataBytes, newOffset, out newOffset); sender.Username = name; sender.IsRegistered = true; var joinPacket = PacketHelper.CreateUserJoinedPacket(sender); sendPacketToAllClientsExcept(joinPacket, sender.UserGuid, true); var clientsPacket = PacketHelper.CreateUsersAlreadyHerePacket(AllRegisteredClients); //Send info of all clients already in lobby to new client sendPacketToClient(clientsPacket, sender); } } if (packet.PacketType == NetConstants.PacketTypes.UserLeft) { var leftPacket = PacketHelper.CreateUserLeftPacket(sender); sendPacketToAllClientsExcept(leftPacket, sender.UserGuid, true); } if (packet.PacketType == NetConstants.PacketTypes.MemoryData && sender.IsRegistered) { //Add the clients Guid in front of the data var newData = PacketHelper.ConcatBytes(sender.UserGuid.ToByteArray(), packet.DataBytes); var newPacket = new Packet(NetConstants.PacketTypes.MemoryData, newData); sendPacketToAllClientsExcept(newPacket, sender.UserGuid, true); } if (packet.PacketType == NetConstants.PacketTypes.UserChat) { var text = PacketHelper.GetStringFromBytes(packet.DataBytes, 0, out newOffset); var chatPacket = PacketHelper.CreateServerToUserChatMessage(sender, text); sendPacketToAllClients(chatPacket, true); } }
private void acceptIncomingConnections(TcpListener tcp) { while (!_stopCalled) { try { var client = tcp.AcceptTcpClient(); var clientThread = new UserThread(client); var registerPacket = PacketHelper.CreateServerRegistrationPacket(clientThread.UserGuid); sendPacketToClient(registerPacket, clientThread); _clients.Add(clientThread); clientThread.Start(clientListenLoop, clientStopped); } catch (SocketException) { break; } } sendShutdownToAll(); }
private void handleIncomingPacket(Packet packet) { int newOffset; if (packet.PacketType == NetConstants.PacketTypes.ServerRegister) { if (packet.DataSize == 0) { return; } var text = PacketHelper.GetStringFromBytes(packet.DataBytes, 0, out newOffset); if (text == NetConstants.ServerRegisterString) { var guidBytes = new byte[16]; Array.Copy(packet.DataBytes, newOffset, guidBytes, 0, 16); var guid = new Guid(guidBytes); ClientGuid = guid; var registerPacket = PacketHelper.CreateUserRegistrationPacket(Nickname); SendPacketToServer(registerPacket); } else { onDisconnected("Invalid server response"); Disconnect(); } } if (packet.PacketType == NetConstants.PacketTypes.UserJoined) { newOffset = 0; addClient(userInfoFromBytes(packet.DataBytes, newOffset, out newOffset)); } if (packet.PacketType == NetConstants.PacketTypes.UserLeft) { newOffset = 0; var incomingUser = userInfoFromBytes(packet.DataBytes, newOffset, out newOffset); var user = getUserInfoFromGuid(incomingUser.Guid); removeClient(user); } if (packet.PacketType == NetConstants.PacketTypes.UserAlreadyHere) { var numClients = BitConverter.ToInt32(packet.DataBytes, 0); newOffset = 4; for (int i = 0; i < numClients; ++i) { addClient(userInfoFromBytes(packet.DataBytes, newOffset, out newOffset)); } } if (packet.PacketType == NetConstants.PacketTypes.ServerShutdown) { onDisconnected("Server shut down"); Disconnect(); } if (packet.PacketType == NetConstants.PacketTypes.MemoryData) { var guid = guidFromBytes(packet.DataBytes, 0, out newOffset); var user = getUserInfoFromGuid(guid); //Don't accept memory changed packets from ourselves if (user != null && user.Guid != ClientGuid && user.Approved) { var memorySize = packet.DataSize - newOffset; var memoryBytes = new byte[memorySize]; Array.Copy(packet.DataBytes, newOffset, memoryBytes, 0, memorySize); var memoryData = ChangedDataFactory.BuildChangedData(memoryBytes); onIncomingData(new IncomingMemoryData(user, memoryData)); } } if (packet.PacketType == NetConstants.PacketTypes.ServerToUserChat) { var incomingUser = userInfoFromBytes(packet.DataBytes, 0, out newOffset); //Use the local user if available var user = getUserInfoFromGuid(incomingUser.Guid) ?? incomingUser; var text = PacketHelper.GetStringFromBytes(packet.DataBytes, newOffset, out newOffset); onIncomingData(new ChatData(user, text)); } }
public byte[] GetBytes() { return(PacketHelper.ConcatBytes(UserGuid.ToByteArray(), PacketHelper.GetStringBytes(Username))); }