Example #1
0
        //void SendConnectAck(NetworkStream stream) {
        //    stream.Write(new byte[] { 0x0 }, 0, 1);
        //}

        void Read(object streamObj)
        {
            var stream = streamObj as NetworkStream;

            while (_connected)
            {
                if (stream.CanRead && stream.DataAvailable)
                {
                    try {
                        var recievedBytes = _packetIO.Recieve();
                        OnRecieved(recievedBytes);
                        //_recieveBuffer.Clear();
                        //int incomingBytes = stream.ReadByte();
                        //do {

                        //    byte[] buffer = new byte[incomingBytes];
                        //    var message = stream.BeginRead(
                        //        buffer, 0, incomingBytes,
                        //        new AsyncCallback(Recieved),
                        //        new Result {
                        //            Buffer = buffer,
                        //            Stream = stream
                        //        });
                        //    _recieveLock.WaitOne();

                        //    incomingBytes = stream.ReadByte();

                        //} while (incomingBytes == CHUNK_SIZE);

                        //var recievedBytes = _recieveBuffer
                        //    .SelectMany(chunk => chunk)
                        //    .ToArray();
                        //OnRecieved(recievedBytes);
                    } catch (IOException) {
                        _connected = false;
                    }
                }
            }

            OnDisconnected?.Invoke();
        }