// File: Unreality3D[P]Unreality3D/indie-studio/source/Network/Broker.cpp
// Function: network::Broker::onPacketMessage(int, IndieProtocol::Message)
void	Broker::onPacketMessage(int fd, IndieProtocol::Message msg)
  {
    std::cout << "FD : " << fd << " : MESSAGE" << std::endl;
    std::cout << msg.target << std::endl;
    std::cout << msg.topic << std::endl;
    std::cout << msg.event << std::endl;

    auto from = _clients.find(fd);
    if (from == _clients.end() || !(*from).second.connected)
      {
	std::cout << "FROM client not connected. Not forwarding the message" << std::endl;
	return ;
      }

    IndieProtocol::Serialized serialed = _serializer(msg);

    // broadcast
    if (msg.target == "")
      {
	IndieProtocol::Serialized tmpSerialed;
	for (auto c = _clients.begin(); c != _clients.end(); ++c)
	  {
	    if ((*c).second.connected && (*c).second.fd != fd)
	      {
		std::cout << "Forwarding to " << (*c).second.name << std::endl;
		tmpSerialed = serialed;
		(*c).second.writeBuffer.push_back(tmpSerialed);
	      }
	  }
      }
    else
      {
	auto c = std::find_if(_clients.begin(), _clients.end(),
			      [&msg](std::pair<SOCKET, Client> const &elem)
			      {
				return (elem.second.name == msg.target && elem.second.connected);
			      });
	if (c != _clients.end())
	  {
	    std::cout << "Forwarding to " << (*c).second.name << std::endl;
	    (*c).second.writeBuffer.push_back(serialed);
	  }
      }
  }

// Assembly:
subq $0xe8, %rsp
movq %rdx, 0x40(%rsp)
movq %rdi, 0xe0(%rsp)
movl %esi, 0xdc(%rsp)
movq %rdx, 0xd0(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x1b469(%rip), %rdi     # 0x52fb0
leaq 0xe924(%rip), %rsi      # 0x46472
callq 0x2a030
movq %rax, %rdi
movl 0xdc(%rsp), %esi
callq 0x2b5c0
movq %rax, %rdi
leaq 0xe91f(%rip), %rsi      # 0x4648b
callq 0x2a030
movq %rax, %rdi
movq 0x1b3e5(%rip), %rsi     # 0x52f60
callq 0x2a150
movq 0x40(%rsp), %rsi
movq 0x1b424(%rip), %rdi     # 0x52fb0
callq 0x29c50
movq %rax, %rdi
movq 0x1b3c5(%rip), %rsi     # 0x52f60
callq 0x2a150
movq 0x40(%rsp), %rsi
addq $0x20, %rsi
movq 0x1b400(%rip), %rdi     # 0x52fb0
callq 0x29c50
movq %rax, %rdi
movq 0x1b3a1(%rip), %rsi     # 0x52f60
callq 0x2a150
movq 0x40(%rsp), %rsi
addq $0x40, %rsi
movq 0x1b3dc(%rip), %rdi     # 0x52fb0
callq 0x29c50
movq %rax, %rdi
movq 0x1b37d(%rip), %rsi     # 0x52f60
callq 0x2a150
movq 0x48(%rsp), %rdi
addq $0x180, %rdi            # imm = 0x180
leaq 0xdc(%rsp), %rsi
callq 0x2ad40
movq 0x48(%rsp), %rdi
movq %rax, 0xc8(%rsp)
addq $0x180, %rdi            # imm = 0x180
callq 0x2b860
movq %rax, 0xc0(%rsp)
leaq 0xc8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x2b6b0
movb %al, %cl
movb $0x1, %al
testb $0x1, %cl
movb %al, 0x57(%rsp)
jne 0x37c5a
leaq 0xc8(%rsp), %rdi
callq 0x2b060
movb 0x8(%rax), %al
xorb $-0x1, %al
movb %al, 0x57(%rsp)
movb 0x57(%rsp), %al
testb $0x1, %al
jne 0x37c64
jmp 0x37c8b
movq 0x1b345(%rip), %rdi     # 0x52fb0
leaq 0xe824(%rip), %rsi      # 0x46496
callq 0x2a030
movq %rax, %rdi
movq 0x1b2df(%rip), %rsi     # 0x52f60
callq 0x2a150
jmp 0x37f5d
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rsi
leaq 0xb0(%rsp), %rdi
callq 0x2bab0
movq 0x40(%rsp), %rdi
leaq 0xe7c9(%rip), %rsi      # 0x46477
callq 0x2be70
movb %al, 0x3f(%rsp)
jmp 0x37cb9
movb 0x3f(%rsp), %al
testb $0x1, %al
jne 0x37cc6
jmp 0x37e53
xorl %esi, %esi
movl %esi, %edx
leaq 0x90(%rsp), %rdi
callq 0x2a480
jmp 0x37cd9
movq 0x48(%rsp), %rdi
addq $0x180, %rdi            # imm = 0x180
callq 0x294f0
movq %rax, 0x88(%rsp)
movq 0x48(%rsp), %rdi
addq $0x180, %rdi            # imm = 0x180
callq 0x2b860
movq %rax, 0x80(%rsp)
leaq 0x88(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x28750
testb $0x1, %al
jne 0x37d29
jmp 0x37e41
leaq 0x88(%rsp), %rdi
callq 0x2b060
testb $0x1, 0x8(%rax)
je 0x37e2d
leaq 0x88(%rsp), %rdi
callq 0x2b060
movl 0xc(%rax), %eax
cmpl 0xdc(%rsp), %eax
je 0x37e2d
movq 0x1b24c(%rip), %rdi     # 0x52fb0
leaq 0xe761(%rip), %rsi      # 0x464cc
callq 0x2a030
movq %rax, 0x30(%rsp)
jmp 0x37d77
leaq 0x88(%rsp), %rdi
callq 0x2b060
movq 0x30(%rsp), %rdi
movq %rax, %rsi
addq $0x10, %rsi
callq 0x29c50
movq %rax, 0x28(%rsp)
jmp 0x37d9c
movq 0x28(%rsp), %rdi
movq 0x1b1b8(%rip), %rsi     # 0x52f60
callq 0x2a150
jmp 0x37daf
leaq 0x90(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x289e0
jmp 0x37dc6
leaq 0x88(%rsp), %rdi
callq 0x2b060
movq %rax, %rdi
addq $0xc0, %rdi
leaq 0x90(%rsp), %rsi
callq 0x28fa0
jmp 0x37dec
jmp 0x37e2d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa8(%rsp)
movl %eax, 0xa4(%rsp)
jmp 0x37f65
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa8(%rsp)
movl %eax, 0xa4(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x295d0
jmp 0x37f65
jmp 0x37e2f
leaq 0x88(%rsp), %rdi
callq 0x29ea0
jmp 0x37cf2
leaq 0x90(%rsp), %rdi
callq 0x295d0
jmp 0x37f50
movq 0x48(%rsp), %rdi
addq $0x180, %rdi            # imm = 0x180
movq %rdi, 0x18(%rsp)
callq 0x294f0
movq 0x18(%rsp), %rdi
movq %rax, 0x70(%rsp)
callq 0x2b860
movq %rax, %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0x68(%rsp)
movq %rax, 0x60(%rsp)
movq 0x70(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x60(%rsp), %rdx
callq 0x37f80
movq %rax, 0x20(%rsp)
jmp 0x37ea5
movq 0x48(%rsp), %rdi
movq 0x20(%rsp), %rax
movq %rax, 0x78(%rsp)
addq $0x180, %rdi            # imm = 0x180
callq 0x2b860
movq %rax, 0x58(%rsp)
leaq 0x78(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x28750
testb $0x1, %al
jne 0x37eda
jmp 0x37f4e
movq 0x1b0cf(%rip), %rdi     # 0x52fb0
leaq 0xe5e4(%rip), %rsi      # 0x464cc
callq 0x2a030
movq %rax, 0x10(%rsp)
jmp 0x37ef4
leaq 0x78(%rsp), %rdi
callq 0x2b060
movq 0x10(%rsp), %rdi
movq %rax, %rsi
addq $0x10, %rsi
callq 0x29c50
movq %rax, 0x8(%rsp)
jmp 0x37f16
movq 0x8(%rsp), %rdi
movq 0x1b03e(%rip), %rsi     # 0x52f60
callq 0x2a150
jmp 0x37f29
leaq 0x78(%rsp), %rdi
callq 0x2b060
movq %rax, %rdi
addq $0xc0, %rdi
leaq 0xb0(%rsp), %rsi
callq 0x28fa0
jmp 0x37f4c
jmp 0x37f4e
jmp 0x37f50
leaq 0xb0(%rsp), %rdi
callq 0x295d0
addq $0xe8, %rsp
retq
leaq 0xb0(%rsp), %rdi
callq 0x295d0
movq 0xa8(%rsp), %rdi
callq 0x2b6f0
nop