public int SendDataPacket(MemoryStream buffer) { int end = 0; int start = 0; byte[] packet; while (end < buffer.Length - 1) { end = start + 1300; if (end >= buffer.Length) { end = (int)buffer.Length - 1; } packet = new Byte[end - start + 1]; Buffer.BlockCopy(buffer.GetBuffer(), start, packet, 0, end - start + 1); start = end + 1; stSendingPacket stPacket = new stSendingPacket(); stPacket.Packet = packet; stPacket.Freed = false; m_SendPacketList.Add(stPacket); } buffer.Close(); buffer = null; return(0); }
private void m_OnSentPacket(IAsyncResult ar) { try { //Debug.WriteLine("->Enviado"); int sentBytes; sentBytes = m_socket.EndSend(ar); m_TotalSent += sentBytes; m_SendBytesPending -= sentBytes; m_ResetTimeOut(); if (m_SendBytesPending > 0) { m_socket.BeginSend(m_SendPacket, sentBytes, m_SendBytesPending, SocketFlags.None, new AsyncCallback(m_OnSentPacket), null); } else { //this packet has finalized,look for the next packet in the list m_SendPacket = null; if ((m_SendPacketList.Count > 0) && (((stSendingPacket)m_SendPacketList[0]).Freed)) { stSendingPacket stPacket = (stSendingPacket)m_SendPacketList[0]; byte[] packetToSend = stPacket.Packet; m_SendPacketList.RemoveAt(0); SendPacket(packetToSend, packetToSend.Length); } } } catch { OnConnectionFail((byte)Protocol.ConnectionReason.ClosedConnection); } }
public int FreeDataBlock() { if (m_SendPacketList.Count == 0) { return(0); } stSendingPacket stPaquete = (stSendingPacket)m_SendPacketList[0]; if ((!stPaquete.Freed) && (m_socket.Connected) && (m_SendPacket == null)) { stPaquete.Freed = true; m_SendPacketList.RemoveAt(0); SendPacket(stPaquete.Packet, stPaquete.Packet.Length); return(stPaquete.Packet.Length); } else { int i = 0; lock (m_SendPacketList.SyncRoot) { while (i < m_SendPacketList.Count) { stPaquete = (stSendingPacket)m_SendPacketList[i]; if (!stPaquete.Freed) { stPaquete.Freed = true; return(stPaquete.Packet.Length); } i++; } } } return(0); }
/// <summary> /// Connection to client established. /// </summary> /// <param name="ar">State of connection.</param> protected virtual void OnConnected(IAsyncResult ar) { try { m_socket.EndConnect(ar); CKernel.ConnectionsList.Add(this); if (m_Client != null) { m_Client.OnConnected(); } m_ResetTimeOut(); ReceivePacket(); if (m_SendPacketList.Count > 0) { stSendingPacket stPaquete = (stSendingPacket)m_SendPacketList[0]; byte[] paqueteaEnviar = stPaquete.Packet; m_SendPacketList.RemoveAt(0); SendPacket(paqueteaEnviar, paqueteaEnviar.Length); } } catch { OnConnectionFail((byte)Protocol.ConnectionReason.CannotConnect); } }
public void SendPacket(MemoryStream buffer) { if ((m_Client != null) && (buffer.GetBuffer()[0] == (byte)Protocol.ProtocolType.eMule) && (m_Client.EmuleProtocol) && (buffer.Length > 1400)) { CCompressedPacket CompressedPacket = new CCompressedPacket(ref buffer); } byte[] packet = new byte[buffer.Length]; Buffer.BlockCopy(buffer.GetBuffer(), 0, packet, 0, packet.Length); stSendingPacket stPacket = new stSendingPacket(); stPacket.Packet = packet; stPacket.Freed = true; if ((m_SendPacketList.Count == 0) && (m_socket.Connected) && (m_SendPacket == null)) { SendPacket(packet, (int)buffer.Length); } else { m_SendPacketList.Add(stPacket); } buffer.Close(); buffer = null; }
public void SendPacket(MemoryStream buffer) { if ((m_Client!=null)&&(buffer.GetBuffer()[0]== (byte)Protocol.ProtocolType.eMule)&&(m_Client.EmuleProtocol)&&(buffer.Length>1400)) { CCompressedPacket CompressedPacket=new CCompressedPacket(ref buffer); } byte[] packet=new byte[buffer.Length]; Buffer.BlockCopy(buffer.GetBuffer(),0,packet,0,packet.Length); stSendingPacket stPacket=new stSendingPacket(); stPacket.Packet=packet; stPacket.Freed=true; if ((m_SendPacketList.Count==0)&&(m_socket.Connected)&&(m_SendPacket==null)) SendPacket(packet,(int)buffer.Length); else { m_SendPacketList.Add(stPacket); } buffer.Close(); buffer=null; }
public int SendDataPacket(MemoryStream buffer) { int end=0; int start=0; byte[] packet; while (end<buffer.Length-1) { end=start+1300; if (end>=buffer.Length) end=(int)buffer.Length-1; packet=new Byte[end-start+1]; Buffer.BlockCopy(buffer.GetBuffer(),start,packet,0,end-start+1); start=end+1; stSendingPacket stPacket=new stSendingPacket(); stPacket.Packet=packet; stPacket.Freed=false; m_SendPacketList.Add(stPacket); } buffer.Close(); buffer=null; return 0; }