public void OutEvent() { // If write buffer is empty, try to read new data from the encoder. if (m_outsize == 0) { m_outpos = null; m_encoder.GetData(ref m_outpos, ref m_outsize); // If there is no data to send, stop polling for output. if (m_outsize == 0) { m_ioObject.ResetPollout(m_handle); return; } } // If there are any data to write in write buffer, write as much as // possible to the socket. Note that amount of data to write can be // arbitratily large. However, we assume that underlying TCP layer has // limited transmission buffer and thus the actual number of bytes // written should be reasonably modest. int nbytes = Write(m_outpos, m_outsize); // IO error has occurred. We stop waiting for output events. // The engine is not terminated until we detect input error; // this is necessary to prevent losing incomming messages. if (nbytes == -1) { m_ioObject.ResetPollout(m_handle); return; } m_outpos.AdvanceOffset(nbytes); m_outsize -= nbytes; // If we are still handshaking and there are no data // to send, stop polling for output. if (m_handshaking) { if (m_outsize == 0) { m_ioObject.ResetPollout(m_handle); } } }