static void AgentRemoteThread() { try { while (true) { if (ag_remote_stream.DataAvailable) { ag_remote_recv_buffer.Offset = 0; ag_remote_recv_buffer.Size = ag_remote_stream.Read(ag_remote_recv_buffer.Buffer, 0, ag_remote_recv_buffer.Buffer.Length); ag_remote_security.Recv(ag_remote_recv_buffer); } ag_remote_recv_packets = ag_remote_security.TransferIncoming(); if (ag_remote_recv_packets != null) { foreach (Packet packet in ag_remote_recv_packets) { byte[] packet_bytes = packet.GetBytes(); // Console.WriteLine("[S->P][{0:X4}][{1} bytes]{2}{3}{4}{5}{6}", packet.Opcode, packet_bytes.Length, packet.Encrypted ? "[Encrypted]" : "", packet.Massive ? "[Massive]" : "", Environment.NewLine, Utility.HexDump(packet_bytes), Environment.NewLine); AgentHandler.RemotePacketHandler(packet); } } ag_remote_send_buffers = ag_remote_security.TransferOutgoing(); if (ag_remote_send_buffers != null) { foreach (var kvp in ag_remote_send_buffers) { Packet packet = kvp.Value; TransferBuffer buffer = kvp.Key; byte[] packet_bytes = packet.GetBytes(); //Console.WriteLine("[P->S][{0:X4}][{1} bytes]{2}{3}{4}{5}{6}", packet.Opcode, packet_bytes.Length, packet.Encrypted ? "[Encrypted]" : "", packet.Massive ? "[Massive]" : "", Environment.NewLine, Utility.HexDump(packet_bytes), Environment.NewLine); ag_remote_stream.Write(buffer.Buffer, 0, buffer.Size); } } Thread.Sleep(1); } } catch { //Console.WriteLine("[AgentRemoteThread] Exception: {0}", ex); } }
public void Agent_thread() { while (true) { if (!ag_socket.IsConnected()) { Globals.UpdateLogs("Disconnected from the server."); break; } SocketError err; ag_recv_buffer.Size = ag_socket.Receive(ag_recv_buffer.Buffer, 0, ag_recv_buffer.Buffer.Length, SocketFlags.None, out err); if (err != SocketError.Success) { if (err != SocketError.WouldBlock) { break; } } else { if (ag_recv_buffer.Size > 0) { ag_security.Recv(ag_recv_buffer); } else { break; } } // Obtain all queued packets and add them to our own queue to process later. List <Packet> tmp_packets = ag_security.TransferIncoming(); if (tmp_packets != null) { ag_packets.AddRange(tmp_packets); } if (ag_packets.Count > 0) { foreach (Packet packet in ag_packets) { if (packet.Opcode == 0x5000 || packet.Opcode == 0x9000) { continue; } if (packet.Opcode == 0x2001) { if (packet.ReadSTRING("ascii") == "GatewayServer") { Globals.Server = Globals.ServerEnum.Gateway; Packet response = new Packet(0x6100, true, false); response.AddBYTE((byte)locale); response.AddSTRING("SR_Client", "ascii"); response.AddDWORD(version); ag_security.Send(response); } else { Globals.Server = Globals.ServerEnum.Agent; Packet p = new Packet(0x6103); p.AddDWORD(loginID); p.WriteAscii(username); p.WriteAscii(password); p.AddBYTE(22); p.AddDWORD(0); p.AddWORD(0); ag_security.Send(p); } } else if (packet.Opcode == 0xA103) { byte flag = packet.ReadBYTE(); if (flag == 1) { Packet response = new Packet(0x7007); response.AddBYTE(2); ag_security.Send(response); } else if (flag == 2) { if (packet.ReadBYTE() == 4) { Globals.UpdateLogs("Server is full"); Globals.Main.btnLogin.Enabled = true; } } } else if (packet.Opcode == 0xB007) { Login.HandleCharList(packet); } else if (packet.Opcode == 0x3020) { Packet p = new Packet(0x3012); Send(p); } AgentHandler.RemotePacketHandler(packet); } ag_packets.Clear(); } List <KeyValuePair <TransferBuffer, Packet> > tmp_buffers = ag_security.TransferOutgoing(); if (tmp_buffers != null) { foreach (var kvp in tmp_buffers) { TransferBuffer buffer = kvp.Key; Packet packet = kvp.Value; err = SocketError.Success; while (buffer.Offset != buffer.Size) { int sent = ag_socket.Send(buffer.Buffer, buffer.Offset, buffer.Size - buffer.Offset, SocketFlags.None, out err); Analyzer.ClientAnalyze(packet); if (err != SocketError.Success) { if (err != SocketError.WouldBlock) { break; } } buffer.Offset += sent; Thread.Sleep(1); } if (err != SocketError.Success) { break; } } if (err != SocketError.Success) { break; } } Thread.Sleep(1); } }