Exemplo n.º 1
0
        private byte[] core_open_stream_channel(byte[] args)
        {
            List <byte> request          = new List <byte>(args);
            int         stream_id        = Struct.extractInt32(request);
            byte        passthrough_byte = Struct.extractByte(request);
            bool        passthrough      = false;

            if (passthrough_byte == 1)
            {
                passthrough = true;
            }

            // check if stream is present
            bool exists = this.hasStream(stream_id);

            if (!exists)
            {
                throw new Exception(String.Format("Stream with ID '{0}' doesn't exist", stream_id));
            }

            // create stream channel
            Stream        stream = this.getStream(stream_id);
            StreamChannel sc     = new StreamChannel(stream, this.callbackChannelOutputPresent, this.triggerProcessingNeeded, passthrough);

            //add stream channel to transport layer channels
            this.AddChannel(sc);

            //return stream id
            UInt32      result   = sc.ID;
            List <byte> response = Struct.packUInt32(result);

            return(response.ToArray());
        }
Exemplo n.º 2
0
        private byte[] core_fs_close_stream(byte[] args)
        {
            List <byte> request   = new List <byte>(args);
            int         stream_id = Struct.extractInt32(request);

            // check if stream is present
            bool exists = this.hasStream(stream_id);

            if (!exists)
            {
                throw new Exception(String.Format("Stream with ID '{0}' doesn't exist", stream_id));
            }

            // close stream channel
            Stream stream = this.getStream(stream_id);

            stream.Dispose();
            stream.Close();
            Console.WriteLine(String.Format("Stream object with ID {0} closed.", stream_id));
            this.removeStream(stream_id);

            //return stream id
            Int32       result   = stream_id;
            List <byte> response = Struct.packInt32(result);

            return(response.ToArray());
        }
        private void dispatchControlMessage(List <byte> msg)
        {
            /*
             * This method is called from the input thread (not the processing thread), so time consuming or
             * blocking tasks mustn't be run here
             */
            CH_MSG_TYPE ch_msg_type = (CH_MSG_TYPE)Struct.extractUInt32(msg);

            switch (ch_msg_type)
            {
            case CH_MSG_TYPE.CHANNEL_CONTROL_REQUEST_STATE:
                Console.WriteLine(String.Format("Received STATE request for StreamChannel {0}", this.ID));
                break;

            case CH_MSG_TYPE.CHANNEL_CONTROL_REQUEST_FLUSH:
                Console.WriteLine(String.Format("Received FLUSH request for StreamChannel {0}", this.ID));
                stream.Flush();

                //return message, stating that everything has been written (should be handed by output thread)
                List <byte> flush_response = Struct.packUInt32((UInt32)StreamChannel.CH_MSG_TYPE.CHANNEL_CONTROL_INFORM_FLUSH_SUCCEEDED);
                this.writeControlMessage(flush_response);

                break;

            case CH_MSG_TYPE.CHANNEL_CONTROL_REQUEST_CLOSE:
                Console.WriteLine(String.Format("Received CLOSE request for StreamChannel {0}", this.ID));
                this.shouldBeClosed = true;     //no processing overhead
                break;

            case CH_MSG_TYPE.CHANNEL_CONTROL_REQUEST_POSITION:
                Console.WriteLine(String.Format("Received POSITION request for StreamChannel {0}", this.ID));
                break;

            case CH_MSG_TYPE.CHANNEL_CONTROL_REQUEST_LENGTH:
                Console.WriteLine(String.Format("Received LENGTH request for StreamChannel {0}", this.ID));
                break;

            case CH_MSG_TYPE.CHANNEL_CONTROL_REQUEST_READ_TIMEOUT:
                Console.WriteLine(String.Format("Received READ_TIMEOUT request for StreamChannel {0}", this.ID));
                break;

            case CH_MSG_TYPE.CHANNEL_CONTROL_REQUEST_WRITE_TIMEOUT:
                Console.WriteLine(String.Format("Received WRITE_TIMEOUT request for StreamChannel {0}", this.ID));
                break;

            case CH_MSG_TYPE.CHANNEL_CONTROL_REQUEST_SEEK:
                int offset = Struct.extractInt32(msg);
                int origin = Struct.extractInt32(msg);
                Console.WriteLine(String.Format("Received SEEK request for StreamChannel {0}, count {1}, timeout {2}", this.ID, offset, origin));
                break;

            case CH_MSG_TYPE.CHANNEL_CONTROL_REQUEST_WRITE:
                int    size          = Struct.extractInt32(msg);
                byte[] data_to_write = msg.ToArray();

                //the data shouldn't be written from this thread, so the operation has to be moved to processing thread
                this.stream.Write(data_to_write, 0, data_to_write.Length);

                //return message, stating that everything has been written (should be handed by output thread)
                List <byte> write_response = Struct.packUInt32((UInt32)StreamChannel.CH_MSG_TYPE.CHANNEL_CONTROL_INFORM_WRITE_SUCCEEDED);
                write_response = Struct.packInt32(data_to_write.Length, write_response);
                this.writeControlMessage(write_response);

                break;

            case CH_MSG_TYPE.CHANNEL_CONTROL_REQUEST_READ:
                int count   = Struct.extractInt32(msg);
                int timeout = Struct.extractInt32(msg);

                Console.WriteLine(String.Format("Received READ request for StreamChannel {0}, count {1}, timeout {2}", this.ID, count, timeout));


                //the data shouldn't be readen from this thread, blocking would stop the input thread (has to be done from processing thread
                if (this.readbuf.Length < count)
                {
                    this.readbuf = new byte[count];                                  //resize read buffer if needed
                }
                int         read_size = this.stream.Read(this.readbuf, 0, count);
                List <Byte> read_data = new List <byte>(this.readbuf);
                read_data.RemoveRange(read_size, this.readbuf.Length - read_size);

                //return message, stating legngth of read data and the data itself (should be handed by output thread)
                List <byte> read_response = Struct.packUInt32((UInt32)StreamChannel.CH_MSG_TYPE.CHANNEL_CONTROL_INFORM_READ_SUCCEEDED);
                read_response = Struct.packInt32(read_size, read_response);
                read_response.AddRange(read_data);
                this.writeControlMessage(read_response);

                break;

            default:
                Console.WriteLine(String.Format("Received unknown channel message for StreamChannel {0}", this.ID));
                break;
            }
        }