Пример #1
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());
        }
Пример #2
0
        /*
         * //REMOVE, obsolete
         * private byte[] core_create_filechannel(byte[] args)
         * {
         *  List<byte> request = new List<byte>(args);
         *  String local_filename = Struct.extractNullTerminatedString(request);
         *  String local_file_access_mode = Struct.extractNullTerminatedString(request);
         *  Byte local_file_target = Struct.extractByte(request); //0 = disc, 1 = in memory
         *  Byte local_file_force = Struct.extractByte(request); //0 = don't force, 1= force (if force is set, non existing file get overwritten on WRITE, and non existing files are created on READWRITE
         *
         *
         *  //create the file channel, on error an exception will be thrown and handled by the caller
         *  FileChannel fc = new FileChannel(this.tl.setOutputProcessingNeeded, local_filename, local_file_access_mode, local_file_target, (local_file_force == 1));
         *
         *  //String response = String.Format("Created file channel for '{0}', access '{1}', mode: {2}, force {3}", local_filename, local_file_access_mode, local_file_target, local_file_force);
         *
         *  //if we are here, channel creation succeded and we return the channel ID as response
         *  List<byte> response = Struct.packUInt32(fc.ID);
         *  return response.ToArray();
         * }
         */

        private byte[] core_fs_open_file(byte[] args)
        {
            List <byte> request  = new List <byte>(args);
            String      filename = Struct.extractNullTerminatedString(request);
            FileMode    fm       = (FileMode)Struct.extractByte(request);
            FileAccess  fa       = (FileAccess)Struct.extractByte(request);

            FileStream fs = File.Open(filename, fm, fa);

            this.addStream(fs);            //store this in global list of opened streams
            int result = fs.GetHashCode(); //return stream id

            //if we are here, channel creation succeded and we return the channel ID as response
            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;
            }
        }