//send to connection public void SendAsync(Packet.SendPacketHandlers.Packet packet, bool test) { lock (locker) { if (sendDone) { int curBufCount = 0; Packet.SendPacketHandlers.Packet p = null; byte[] sBuf = null; if (sendQueue.Count > 0) { p = sendQueue.Dequeue(); if (packet != null) { sendQueue.Enqueue(packet); } } else { if (packet != null) { p = packet; } } if (p == null) { return; } int packetSize = p.PacketLength + Packet.Encrypt.MAX_ADD_BYTES; if (packetSize > SendSocket.Buffer.Length) { sBuf = encryptPacket(p); sendDone = false; try { sendSocket.AcceptSocket.BeginSend(sBuf, 0, sBuf.Length, 0, new AsyncCallback(SendCallback), this); } catch (Exception e) { Output.WriteLine(e.ToString()); Close(); } } else { sBuf = encryptPacket(p); Buffer.BlockCopy(sBuf, 0, SendSocket.Buffer, SendSocket.Offset, sBuf.Length); curBufCount += sBuf.Length; } } else { if (packet != null) { sendQueue.Enqueue(packet); } } } }
public BroadcastPacket(uint xpos, uint ypos, int range, Packet.SendPacketHandlers.Packet p) { packet = p; xPos = xpos; yPos = ypos; this.range = range; }
public void BroadcastPacket(int mapX, int mapY, uint range, Packet.SendPacketHandlers.Packet packet) { //Output.WriteLine("World::BroadcastPacket From " + mapX.ToString() + "," + mapY.ToString() + "] Range: " + range.ToString()); List <Database.Player> plList = PlayersInSightRange(mapX, mapY, range); //Output.WriteLine("World::BroadcastPacket Broadcast to " + plList.Count.ToString() + " players"); BroadcastPacket(plList, packet); }
public void BroadcastPacket(List <Database.Player> plList, Packet.SendPacketHandlers.Packet packet) { foreach (Database.Player p in plList) { p.Con.SendAsync(new Packet.SendPacketHandlers.CopyPacket(packet)); //if(packet.PacketType == (byte)Packet.SendPacketHandlers.SEND_HEADER.MOB_DESPAWN) Output.WriteLine("World::BroadcastPacket Send packet " + packet.PacketType.ToString() + " to " + p.Con.client.PlayerID.ToString()); //Output.WriteLine("World::BroadcastPacket Send packet " + packet.PacketType.ToString() + " to " + p.Con.client.PlayerID.ToString()); } }
private byte[] encryptPacket(Packet.SendPacketHandlers.Packet p) { if (p == null) { return(null); } int packetLength = 0; byte[] sendBuffer = null; switch (client.EncodeType) { case Client.ENCODE_TYPE.AES: break; case Client.ENCODE_TYPE.BXO: break; case Client.ENCODE_TYPE.XOR: if (Program.DEBUG_send) { Output.WriteLine("Connection::SendPacket Encoded with XOR and key: " + client.PrivateKey[client.SendKeyOffset].ToString()); } sendBuffer = p.Compile(client.PrivateKey, client.SendKeyOffset, Client.ENCODE_TYPE.XOR, out packetLength); client.SendKeyOffset++; if (client.SendKeyOffset >= client.PrivateKey.Length) { client.SendKeyOffset = 0; } break; case Client.ENCODE_TYPE.COD: if (Program.DEBUG_send) { Output.WriteLine("Connection::SendPacket" + "PID: " + client.PlayerID.ToString() + " Encode with COD and key: " + client.sendKeyCOD.ToString()); } byte[] enKey = new byte[1]; enKey[0] = client.sendKeyCOD; sendBuffer = p.Compile(enKey, 0, Client.ENCODE_TYPE.COD, out packetLength); client.sendKeyCOD++; if (client.sendKeyCOD >= 63) { client.sendKeyCOD = 0; } break; } return(sendBuffer); }
//send to connection public void SendAsync(Packet.SendPacketHandlers.Packet packet) { //debug if (packet != null && packet.PacketType == (byte)Packet.SendPacketHandlers.SEND_HEADER.SKILL_ANIM) { Output.WriteLine(ConsoleColor.Green, "Connection::SendAsync Send SKILL_ANIM to: " + RecvSocket.AcceptSocket.RemoteEndPoint.ToString()); } if (packet != null && packet.PacketType == (byte)Packet.SendPacketHandlers.SEND_HEADER.ATTACK_MAG) { int casterID; int targetID; int skill_type; int skill_level; MemoryStream stream = new MemoryStream(packet.PacketData()); BinaryReader br; using (br = new BinaryReader(stream)) { stream.Position = 0;//set strem position to begin of data (beafore is header data) casterID = br.ReadInt32(); targetID = br.ReadInt32(); skill_type = br.ReadInt32(); skill_level = br.ReadInt32(); } Output.WriteLine(ConsoleColor.Green, "Connection::SendAsync Send ATTACK_MAG to: " + RecvSocket.AcceptSocket.RemoteEndPoint.ToString() + "[" + casterID.ToString() + "->" + targetID.ToString() + "]"); } if (sendQueue.Count > MAX_SEND_QUEUE_SIZE) { Output.WriteLine(ConsoleColor.Red, "Connection::SendAsync Send queue is full, closing connection: " + RecvSocket.AcceptSocket.RemoteEndPoint.ToString()); Close(); return; } lock (locker) { if (sendDone) { Packet.SendPacketHandlers.Packet p = null; if (sendQueue.Count > 0) { p = sendQueue.Dequeue(); if (packet != null) { sendQueue.Enqueue(packet); } } else { if (packet != null) { p = packet; } } if (p == null) { return; } int packetLength = 0; byte[] sendBuffer = null; switch (client.EncodeType) { case Client.ENCODE_TYPE.AES: break; case Client.ENCODE_TYPE.BXO: break; case Client.ENCODE_TYPE.XOR: if (Program.DEBUG_send) { Output.WriteLine("Connection::SendPacket Encoded with XOR and key: " + client.PrivateKey[client.SendKeyOffset].ToString()); } sendBuffer = p.Compile(client.PrivateKey, client.SendKeyOffset, Client.ENCODE_TYPE.XOR, out packetLength); client.SendKeyOffset++; if (client.SendKeyOffset >= client.PrivateKey.Length) { client.SendKeyOffset = 0; } break; case Client.ENCODE_TYPE.COD: if (Program.DEBUG_send) { Output.WriteLine("Connection::SendPacket" + "PID: " + client.PlayerID.ToString() + " Encode with COD and key: " + client.sendKeyCOD.ToString()); } byte[] enKey = new byte[1]; enKey[0] = client.sendKeyCOD; sendBuffer = p.Compile(enKey, 0, Client.ENCODE_TYPE.COD, out packetLength); client.sendKeyCOD++; if (client.sendKeyCOD >= 63) { client.sendKeyCOD = 0; } break; } if (Program.DEBUG_send || Program.DEBUG_Login_Send) { string text; if (p.PacketType == Packet.LoginServerSend.SERVER_EXTENDED_PACKET_TYPE) { text = String.Format("Send EXTENDED packet type: 0x{0:X2} Length: {1} and after crypt: {2}", p.PacketExtendType, p.PacketLength, packetLength); } else { text = String.Format("PID: " + client.PlayerID.ToString() + " Send packet type: 0x{0:x2} Length: {1} and after crypt: {2}", p.PacketType, p.PacketLength, packetLength); } Output.WriteLine("Connection::Send " + text); } sendDone = false; try { sendSocket.AcceptSocket.BeginSend(sendBuffer, 0, sendBuffer.Length, 0, new AsyncCallback(SendCallback), this); } catch (Exception e) { Output.WriteLine(e.ToString()); Close(); } } else { if (packet != null) { sendQueue.Enqueue(packet); } } } }
public void SendSync(Packet.SendPacketHandlers.Packet p) { lock (locker) { int packetLength = 0; byte[] sendBuffer = null; switch (client.EncodeType) { case Client.ENCODE_TYPE.AES: break; case Client.ENCODE_TYPE.BXO: break; case Client.ENCODE_TYPE.XOR: if (Program.DEBUG_send) { Output.WriteLine("Connection::SendPacket Encoded with XOR and key: " + client.PrivateKey[client.SendKeyOffset].ToString()); } sendBuffer = p.Compile(client.PrivateKey, client.SendKeyOffset, Client.ENCODE_TYPE.XOR, out packetLength); client.SendKeyOffset++; if (client.SendKeyOffset >= client.PrivateKey.Length) { client.SendKeyOffset = 0; } break; case Client.ENCODE_TYPE.COD: if (Program.DEBUG_send) { Output.WriteLine("Connection::SendPacket" + "PID: " + client.PlayerID.ToString() + " Encode with COD and key: " + client.sendKeyCOD.ToString()); } byte[] enKey = new byte[1]; enKey[0] = client.sendKeyCOD; sendBuffer = p.Compile(enKey, 0, Client.ENCODE_TYPE.COD, out packetLength); client.sendKeyCOD++; if (client.sendKeyCOD >= 63) { client.sendKeyCOD = 0; } break; } if (Program.DEBUG_send || Program.DEBUG_Login_Send) { string text; if (p.PacketType == Packet.LoginServerSend.SERVER_EXTENDED_PACKET_TYPE) { text = String.Format("Send EXTENDED packet type: 0x{0:X2} Length: {1} and after crypt: {2}", p.PacketExtendType, p.PacketLength, packetLength); } else { text = String.Format("PID: " + client.PlayerID.ToString() + " Send packet type: 0x{0:x2} Length: {1} and after crypt: {2}", p.PacketType, p.PacketLength, packetLength); } //Output.WriteLine("Connection::Send " + text); } //sendQueue.Enqueue(sendBuffer); //connectedSocket.BeginSend(sendBuffer, 0, packetLength, 0, new AsyncCallback(SendCallback), connectedSocket); //using blocking send mayby async will be bether?? try { int iResult = sendSocket.AcceptSocket.Send(sendBuffer, 0, packetLength, 0); if (iResult == (int)SocketError.SocketError) { Output.WriteLine("Connection::Send - Send failed with error: " + iResult.ToString()); } } catch (ObjectDisposedException e) { Output.WriteLine("Connection::Send - Send failed with error: " + e.ToString()); } catch (SocketException es) { Output.WriteLine("Connection::Send - Send failed with error: " + es.ToString()); } catch (NullReferenceException en) { Output.WriteLine("Connection::Send - Send failed with error: " + en.ToString()); } } }
public void BroadcastPacket(Database.Player pl, Packet.SendPacketHandlers.Packet packet) { BroadcastPacket(pl.PosX, pl.PosY, World.DEBUG_SIGHT_RANGE, packet); }