Example #1
0
        //Begins an Asynchronous read from a specific client's network stream.  This function handles
        //reading in header information that defines the size of the object, and then invokes a callback
        //which will handle reading and deserializing the actual object.
        public void BeginRead(onReadEventHandler callback, int clientNumber)
        {
            if (m_listener != null)
            {
                try
                {
                    //Set a callback to notify the user of a succsesful read, once we've finished with
                    //our part.
                    onReadUserCallback = callback;

                    //ServerReadInfo will hold the id of the client who we're reading from, and an empty
                    //array to read into.
                    ServerReadInfo header = new ServerReadInfo();
                    header.Id   = clientNumber;
                    header.Data = new Byte[4];

                    //Set reading to true.  The caller has to use an if to check if we're reading before
                    //calling this function. Note:  Maybe the if can be done in this class?  I wasn't sure
                    //it as appropriate to take away that control.
                    m_clients[clientNumber].Reading = true;

                    //Read the data into our header object and send the instance of that object to the
                    //onRead header local callback.
                    m_clients[clientNumber].Stream.BeginRead(header.Data, 0, 4, onReadHeaderLocalCallback, header);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }
Example #2
0
        public void BeginRead(onReadEventHandler callback)
        {
            if (m_client.Connected)
            {
                try
                {
                    onReadUserCallback = callback;

                    ClientReadInfo header = new ClientReadInfo();
                    header.Data = new Byte[4];

                    m_reading = true;
                    m_stream.BeginRead(header.Data, 0, 4, onReadHeaderLocalCallback, header);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }