Thread: [csmaild-cvs] csmaild/src/Imap/NetworkManager Connection.cs,1.3,1.4 Listener.cs,1.2,1.3
Brought to you by:
tamc
From: <ta...@us...> - 2003-07-24 04:32:27
|
Update of /cvsroot/csmaild/csmaild/src/Imap/NetworkManager In directory sc8-pr-cvs1:/tmp/cvs-serv32149/src/Imap/NetworkManager Modified Files: Connection.cs Listener.cs Log Message: Long time, no commit. Upgraded to VS.NET 2003 Lots of code changes, still pre-pre-pre-alpha, so who's keeping track Index: Connection.cs =================================================================== RCS file: /cvsroot/csmaild/csmaild/src/Imap/NetworkManager/Connection.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Connection.cs 12 Apr 2003 18:10:15 -0000 1.3 --- Connection.cs 24 Jul 2003 04:32:14 -0000 1.4 *************** *** 19,29 **** #region Variables private Socket mClient; // the communication socket ! private NetworkStream mSocketStream; // a stream wrapping the com socket ! private byte[] mReceiveBuffer; // a storage place for receive calls ! private MemoryStream mBlockStream; // stores the data when reading a block ! private string mLineString; // stores the data when reading a line ! private bool mReadingLine; // true if we're reading a line ! private int mReadingBlockSize; // the size of the block we are reading (if we're reading a block) private Listener mListener; // the listener that created us --- 19,30 ---- #region Variables private Socket mClient; // the communication socket ! private NetworkStream mSocketStream; // a stream wrapping the communication socket ! ! private byte[] mReceiveLineBuffer; // a small storage place for receiving bytes for the line ! private string mLineString; // used to store the line the client sends ! private byte[] mReceiveBlockBuffer; // used to receive the buffer requested ! private int mReceiveBlockOffset; // the current offset into the receive block buffer ! private int mReceiveBlockSize; // the number of bytes left to read into the receive block buffer private Listener mListener; // the listener that created us *************** *** 31,53 **** #region Events /// <summary> ! /// Fires whenever a line is received from the client /// </summary> ! public event ReceivedLineDelegate ReceivedLineEvent; /// <summary> ! /// Fires whenever the amount of data requested is received /// </summary> ! public event ReceivedBlockDelegate ReceivedBlockEvent; /// <summary> /// Fires when sending is complete /// </summary> ! public event SentDataDelegate SentDataEvent; /// <summary> /// Fires when the connection is closed remotely /// </summary> ! public event ConnectionClosedDelegate ConnectionClosedEvent; #endregion --- 32,99 ---- #region Events + private event ReceivedLineDelegate mReceivedLineEvent; + private event ReceivedBlockDelegate mReceivedBlockEvent; + private event SentDataDelegate mSentDataEvent; + private event ConnectionClosedDelegate mConnectionClosedEvent; + /// <summary> ! /// Fires when the requested line has been received from the client /// </summary> ! public event ReceivedLineDelegate ReceivedLineEvent ! { ! add ! { ! mReceivedLineEvent += value; ! } ! remove ! { ! mReceivedLineEvent -= value; ! } ! } /// <summary> ! /// Fires when the requested block has been received from the client /// </summary> ! public event ReceivedBlockDelegate ReceivedBlockEvent ! { ! add ! { ! mReceivedBlockEvent += value; ! } ! remove ! { ! mReceivedBlockEvent -= value; ! } ! } /// <summary> /// Fires when sending is complete /// </summary> ! public event SentDataDelegate SentDataEvent ! { ! add ! { ! mSentDataEvent += value; ! } ! remove ! { ! mSentDataEvent -= value; ! } ! } /// <summary> /// Fires when the connection is closed remotely /// </summary> ! public event ConnectionClosedDelegate ConnectionClosedEvent ! { ! add ! { ! mConnectionClosedEvent += value; ! } ! remove ! { ! mConnectionClosedEvent -= value; ! } ! } #endregion *************** *** 64,73 **** mSocketStream = new NetworkStream(mClient, true); ! mReceiveBuffer = new byte[4096]; ! mBlockStream = new MemoryStream(); } #endregion #region Public methods /// <summary> /// Writes the message to the socket asynchronosously --- 110,124 ---- mSocketStream = new NetworkStream(mClient, true); ! mReceiveLineBuffer = new byte[8192]; } #endregion #region Public methods + public void Write(string msg) + { + byte[] b = Encoding.ASCII.GetBytes(msg); + mSocketStream.BeginWrite(b, 0, b.Length, new AsyncCallback(SendCallback), msg); + } + /// <summary> /// Writes the message to the socket asynchronosously *************** *** 76,81 **** public void WriteLine(string msg) { ! byte[] b = Encoding.ASCII.GetBytes(msg + "\r\n"); ! mSocketStream.BeginWrite(b, 0, b.Length, new AsyncCallback(SendCallback), msg); } --- 127,131 ---- public void WriteLine(string msg) { ! Write(msg + "\r\n"); } *************** *** 85,134 **** public void ReadLine() { ! mReadingLine = true; ! mSocketStream.BeginRead(mReceiveBuffer, 0, mReceiveBuffer.Length, new AsyncCallback(ReceiveCallback), null); } /// <summary> ! /// Reads a block of data from the socket asynchronously /// </summary> ! /// <param name="size">The size of the block to read</param> ! public void ReadBlock(int size) { ! mReadingLine = false; ! mReadingBlockSize = size; ! // if we have some left over from before ! if(mLineString.Length != 0) ! { ! byte[] old = Encoding.ASCII.GetBytes(mLineString); ! mBlockStream.Write(old, 0, old.Length); ! mLineString = string.Empty; ! } ! // read some more bytes (the lesser of the number we need left or the buffer size) ! mSocketStream.BeginRead(mReceiveBuffer, 0, (int)Math.Min(mReceiveBuffer.Length, mReadingBlockSize-mBlockStream.Position), new AsyncCallback(ReceiveCallback), null); } #endregion #region Protected methods ! /// <summary> ! /// Fires the related event, called when a line has been read ! /// </summary> ! /// <param name="line">The line that was read</param> ! protected virtual void OnReadLine(string line) { ! if(ReceivedLineEvent != null) ! ReceivedLineEvent(this, line); } ! /// <summary> ! /// Fires the related event, called when a block has been read ! /// </summary> ! /// <param name="block">The block that was read</param> ! protected virtual void OnReadBlock(byte[] block) { ! if(ReceivedBlockEvent != null) ! ReceivedBlockEvent(this, block); } --- 135,185 ---- public void ReadLine() { ! ReadLine(mReceivedLineEvent); ! } ! public void ReadLine(ReceivedLineDelegate callback) ! { ! mSocketStream.BeginRead(mReceiveLineBuffer, 0, mReceiveLineBuffer.Length, new AsyncCallback(ReceiveLineCallback), callback); } /// <summary> ! /// Reads a block from the socket asynchronously /// </summary> ! /// <param name="block">The buffer to place the bytes read</param> ! /// <param name="offset">The offset into this buffer</param> ! /// <param name="size">The number of bytes to read into the buffer</param> ! public void ReadBlock(byte[] block, int offset, int length) { ! ReadBlock(block, offset, length, mReceivedBlockEvent); ! } ! /// <summary> ! /// Reads a block from the socket asynchronously calling the passed in delegate instead of the subscribed handler ! /// </summary> ! /// <param name="block">The buffer to place the bytes read</param> ! /// <param name="offset">The offset into this buffer</param> ! /// <param name="size">The number of bytes to read into the buffer</param> ! /// <param name="callback">The callback function to call after done reading</param> ! public void ReadBlock(byte[] block, int offset, int length, ReceivedBlockDelegate callback) ! { ! mReceiveBlockBuffer = block; ! mReceiveBlockOffset = offset; ! mReceiveBlockSize = length; ! ReadBlock(callback); } #endregion #region Protected methods ! private void FireReadLineDelegate(string line, ReceivedLineDelegate func) { ! if(func != null) ! func(this, line); } ! private void FireReadBlockDelegate(byte[] block, ReceivedBlockDelegate func) { ! if(func != null) ! func(this, block); } *************** *** 138,143 **** protected virtual void OnSent() { ! if(SentDataEvent != null) ! SentDataEvent(this); } --- 189,194 ---- protected virtual void OnSent() { ! if(mSentDataEvent != null) ! mSentDataEvent(this); } *************** *** 147,164 **** protected virtual void OnClosed() { ! if(ConnectionClosedEvent != null) ! ConnectionClosedEvent(this); } #endregion #region Private methods private void SendCallback(IAsyncResult result) { mSocketStream.EndWrite(result); // spill event OnSent(); } ! private void ReceiveCallback(IAsyncResult result) { int read = mSocketStream.EndRead(result); --- 198,223 ---- protected virtual void OnClosed() { ! if(mConnectionClosedEvent != null) ! mConnectionClosedEvent(this); } #endregion #region Private methods + private void ReadBlock(ReceivedBlockDelegate callback) + { + mSocketStream.BeginRead(mReceiveBlockBuffer, mReceiveBlockOffset, mReceiveBlockSize, new AsyncCallback(ReceiveBlockCallback), callback); + } + private void SendCallback(IAsyncResult result) { mSocketStream.EndWrite(result); + + System.Diagnostics.Trace.Write(ToString() + " <- " + (string)result.AsyncState); + // spill event OnSent(); } ! private void ReceiveLineCallback(IAsyncResult result) { int read = mSocketStream.EndRead(result); *************** *** 171,220 **** { // if we're reading a line ! if(mReadingLine) { ! mLineString += Encoding.ASCII.GetString(mReceiveBuffer, 0, read); ! // we're reading a line and we found the eof!! ! int idx = mLineString.IndexOf("\r\n"); ! if(idx != -1) ! { ! // we quite possibly got too much data for this line ! // stuff everything before the <CRLF> in this ! string line = mLineString.Substring(0, idx); ! // and everything after back here ! mLineString = mLineString.Substring(idx+2); ! // spill event ! OnReadLine(line); ! } ! else ! { ! // read some more bytes ! ReadLine(); ! } } ! else // or we're reading a block of bytes { ! // add to our current stash of input ! mBlockStream.Write(mReceiveBuffer, 0, read); ! // we're reading a block and got all of our data!! ! if(mBlockStream.Position == mReadingBlockSize) ! { ! byte[] block = mBlockStream.ToArray(); ! ! // reset the stream ! mBlockStream.Close(); ! mBlockStream = new MemoryStream(); ! // spill event ! OnReadBlock(block); ! } ! else ! { ! // read more ! ReadBlock(mReadingBlockSize); ! } ! } } } --- 230,277 ---- { // if we're reading a line ! mLineString += Encoding.ASCII.GetString(mReceiveLineBuffer, 0, read); ! ! // we're reading a line and we found the eof!! ! int idx = mLineString.IndexOf("\r\n"); ! if(idx != -1) { ! // we quite possibly got too much data for this line ! // stuff everything before the <CRLF> in this ! string line = mLineString.Substring(0, idx); ! // and everything after back here ! mLineString = mLineString.Substring(idx+2); ! System.Diagnostics.Trace.WriteLine(ToString() + " -> " + (string)line); ! // spill event ! FireReadLineDelegate(line, (ReceivedLineDelegate)result.AsyncState); } ! else { ! // read some more bytes ! ReadLine(); ! } ! } ! } ! private void ReceiveBlockCallback(IAsyncResult result) ! { ! int read = mSocketStream.EndRead(result); ! if(read == 0) // connection was closed ! { ! // spill event ! OnClosed(); ! } ! else ! { ! // move offset by the amount we read ! mReceiveBlockOffset += read; ! // decrease the amount we have left to read by the amount we read ! mReceiveBlockSize -= read; ! if(mReceiveBlockSize == 0) ! FireReadBlockDelegate(mReceiveBlockBuffer, (ReceivedBlockDelegate)result.AsyncState); ! else ! ReadBlock((ReceivedBlockDelegate)result.AsyncState); } } Index: Listener.cs =================================================================== RCS file: /cvsroot/csmaild/csmaild/src/Imap/NetworkManager/Listener.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Listener.cs 11 Apr 2003 01:29:26 -0000 1.2 --- Listener.cs 24 Jul 2003 04:32:14 -0000 1.3 *************** *** 14,18 **** private Socket mListenSocket; ! public event AcceptDelegate AcceptedConnection; public Listener(int port) --- 14,29 ---- private Socket mListenSocket; ! private event AcceptDelegate mAcceptedConnection; ! public event AcceptDelegate AcceptedConnection ! { ! add ! { ! mAcceptedConnection += value; ! } ! remove ! { ! mAcceptedConnection -= value; ! } ! } public Listener(int port) *************** *** 37,42 **** // tell the observers about our new connection ! if(AcceptedConnection != null) ! AcceptedConnection(con); } } --- 48,53 ---- // tell the observers about our new connection ! if(mAcceptedConnection != null) ! mAcceptedConnection(con); } } |