From: <ta...@us...> - 2003-04-11 01:29:29
|
Update of /cvsroot/csmaild/csmaild/src/Imap/NetworkManager In directory sc8-pr-cvs1:/tmp/cvs-serv31710/src/Imap/NetworkManager Modified Files: Connection.cs Listener.cs NetworkManager.cs Log Message: Just for historical purposes. Index: Connection.cs =================================================================== RCS file: /cvsroot/csmaild/csmaild/src/Imap/NetworkManager/Connection.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Connection.cs 9 Apr 2003 05:00:32 -0000 1.1 --- Connection.cs 11 Apr 2003 01:29:26 -0000 1.2 *************** *** 17,20 **** --- 17,21 ---- public class Connection { + #region Variables private Socket mClient; // the communication socket private NetworkStream mSocketStream; // a stream wrapping the com socket *************** *** 26,29 **** --- 27,34 ---- 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 + #endregion + + #region Events /// <summary> /// Fires whnever a line is received from the client *************** *** 45,64 **** /// </summary> public event ConnectionClosedDelegate ConnectionClosedEvent; /// <summary> /// Creates a new Connection object /// </summary> /// <param name="client">The Socket for this Connection</param> ! internal Connection(Socket client) { mClient = client; ! mSocketStream = new NetworkStream(mClient, true); mReceiveBuffer = new byte[4096]; mBlockStream = new MemoryStream(); } /// <summary> ! /// Writes the message to the socket /// </summary> /// <param name="msg">The message to write</param> --- 50,75 ---- /// </summary> public event ConnectionClosedDelegate ConnectionClosedEvent; + #endregion + #region Constructor /// <summary> /// Creates a new Connection object /// </summary> /// <param name="client">The Socket for this Connection</param> ! /// <param name="listener">The Listener that created us</param> ! internal Connection(Socket client, Listener listener) { mClient = client; ! mListener = listener; + mSocketStream = new NetworkStream(mClient, true); mReceiveBuffer = new byte[4096]; mBlockStream = new MemoryStream(); } + #endregion + #region Public methods /// <summary> ! /// Writes the message to the socket asynchronosously /// </summary> /// <param name="msg">The message to write</param> *************** *** 69,72 **** --- 80,86 ---- } + /// <summary> + /// Reads a line from the socket asynchronously + /// </summary> public void ReadLine() { *************** *** 76,79 **** --- 90,97 ---- } + /// <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) { *************** *** 92,97 **** mSocketStream.BeginRead(mReceiveBuffer, 0, (int)Math.Min(mReceiveBuffer.Length, mReadingBlockSize-mBlockStream.Position), new AsyncCallback(ReceiveCallback), null); } ! public virtual void OnReadLine(string line) { if(ReceivedLineEvent != null) --- 110,121 ---- 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) *************** *** 99,103 **** } ! public virtual void OnReadBlock(byte[] block) { if(ReceivedBlockEvent != null) --- 123,131 ---- } ! /// <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) *************** *** 105,109 **** } ! public virtual void OnSent() { if(SentDataEvent != null) --- 133,140 ---- } ! /// <summary> ! /// Fires the related event, called called when the data has been sent ! /// </summary> ! protected virtual void OnSent() { if(SentDataEvent != null) *************** *** 111,120 **** } ! public virtual void OnClosed() { if(ConnectionClosedEvent != null) ConnectionClosedEvent(this); } private void SendCallback(IAsyncResult result) { --- 142,156 ---- } ! /// <summary> ! /// Fires when the socket is closed ! /// </summary> ! protected virtual void OnClosed() { if(ConnectionClosedEvent != null) ConnectionClosedEvent(this); } + #endregion + #region Private methods private void SendCallback(IAsyncResult result) { *************** *** 183,191 **** --- 219,230 ---- } } + #endregion + #region Overridden methods public override string ToString() { return mClient.RemoteEndPoint.ToString(); } + #endregion } } Index: Listener.cs =================================================================== RCS file: /cvsroot/csmaild/csmaild/src/Imap/NetworkManager/Listener.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Listener.cs 9 Apr 2003 05:00:32 -0000 1.1 --- Listener.cs 11 Apr 2003 01:29:26 -0000 1.2 *************** *** 29,34 **** private void AcceptCallback(IAsyncResult result) { // do the final accpet and wait for another ! Connection con = new Connection(mListenSocket.EndAccept(result)); mListenSocket.BeginAccept(new AsyncCallback(AcceptCallback), null); --- 29,37 ---- private void AcceptCallback(IAsyncResult result) { + if(mListenSocket == null) + return; + // do the final accpet and wait for another ! Connection con = new Connection(mListenSocket.EndAccept(result), this); mListenSocket.BeginAccept(new AsyncCallback(AcceptCallback), null); Index: NetworkManager.cs =================================================================== RCS file: /cvsroot/csmaild/csmaild/src/Imap/NetworkManager/NetworkManager.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** NetworkManager.cs 9 Apr 2003 05:00:32 -0000 1.1 --- NetworkManager.cs 11 Apr 2003 01:29:26 -0000 1.2 *************** *** 43,79 **** public void Listener_AcceptedConnection(Connection con) { - System.Console.WriteLine(con + " Accepted"); - con.ConnectionClosedEvent += new ConnectionClosedDelegate(Connection_Closed); - con.ReceivedLineEvent += new ReceivedLineDelegate(Connection_ReceivedLine); - con.ReceivedBlockEvent += new ReceivedBlockDelegate(Connection_ReceivedBlock); - con.SentDataEvent += new SentDataDelegate(Connection_SentData); - - con.WriteLine("* OK Welcome!!"); - System.Console.WriteLine(con + " Sent: * OK Welcome!!"); - - con.ReadLine(); - mConnections.Add(con); } - public void Connection_ReceivedLine(Connection con, string line) - { - System.Console.WriteLine(con + " Line(" + line.Length + "): " + line); - con.ReadBlock(100); - } - - public void Connection_ReceivedBlock(Connection con, byte[] block) - { - System.Console.WriteLine(con + " Block(" + block.Length + "): " + block); - } - - public void Connection_SentData(Connection con) - { - } - public void Connection_Closed(Connection con) { ! System.Console.WriteLine(con); } } --- 43,53 ---- public void Listener_AcceptedConnection(Connection con) { con.ConnectionClosedEvent += new ConnectionClosedDelegate(Connection_Closed); mConnections.Add(con); } public void Connection_Closed(Connection con) { ! mConnections.Remove(con); } } |