static void TxCallback(IAsyncResult ar) { amuleRemote o = (amuleRemote)ar.AsyncState; Console.WriteLine("TxCallback signalled, calling EndWrite"); o.m_s.EndSend(ar); }
public amuleLogicHandler(amuleRemote o) { m_owner = o; }
static void RxCallback(IAsyncResult ar) { amuleRemote o = (amuleRemote)ar.AsyncState; Console.WriteLine("RxCallback signalled, calling EndReceive"); int bytesRead = o.m_s.EndReceive(ar); if (bytesRead == 0) { // remote side closed connection. // indicate error to caller o.m_rx_byte_count = -1; return; } o.m_rx_remaining_count -= bytesRead; Console.WriteLine("RxCallback: got {0} bytes, waiting for {1}", bytesRead, o.m_rx_remaining_count); // are we still waiting for flags and size? if (o.m_rx_byte_count < 8) { if ((o.m_rx_byte_count + bytesRead) >= 8) { // got flags and packet size - may proceed. Int32 flags = o.m_sock_reader.ReadInt32(); Int32 val32 = o.m_sock_reader.ReadInt32(); o.m_rx_remaining_count = (int)IPAddress.NetworkToHostOrder(val32) - (bytesRead - 8); Console.WriteLine("RxCallback: expecting packet size={0}", o.m_rx_remaining_count); if (o.m_rx_buffer.Length <= (o.m_rx_remaining_count + o.m_rx_byte_count)) { byte [] new_buffer = new byte[o.m_rx_remaining_count + o.m_rx_buffer.Length + 1]; o.m_rx_buffer.CopyTo(new_buffer, 0); o.m_rx_buffer = new_buffer; // // update stream reader with new buffer // o.m_rx_mem_stream = new MemoryStream(o.m_rx_buffer); o.m_sock_reader = new BinaryReader(o.m_rx_mem_stream); } } } else { if (o.m_rx_remaining_count == 0) { // // Packet received - call handler // if (o.m_handler != null) { o.m_rx_mem_stream.Seek(0, SeekOrigin.Begin); Console.WriteLine("Packet received - call handler"); ecProto.ecPacket p = new ecProto.ecPacket(o.m_sock_reader); //m_packet_op_Done.Set(); o.m_handler.HandlePacket(p); Console.WriteLine("Handler done"); } Console.WriteLine("Signalling event"); //m_packet_op_Done.Set(); // // Keep waiting for more packets // o.StartReceive(); return; } } o.m_rx_byte_count += bytesRead; // not just yet - keep waiting o.m_s.BeginReceive(o.m_rx_buffer, o.m_rx_byte_count, o.m_rx_remaining_count, SocketFlags.None, new AsyncCallback(RxCallback), o); }