/// <exception cref="System.IO.IOException"/> public virtual int PreRead(LittleEndianDataInputStream littleEndianDataInputStream ) { sizeofBlock = BlockSizeMultiplier * littleEndianDataInputStream.ReadUnsignedShort (); return(2); }
/// <summary>read data.</summary> /// <remarks>read data. This assumes that the inputStream is on byte 0 from the start of the block /// </remarks> /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> public virtual void Read(PDXReaderListener pdxReaderListener, Stream inputStream ) { try { records = new List <DBTableRecord>(); /* * read the header */ LittleEndianDataInputStream littleEndianDataInputStream = new LittleEndianDataInputStream (inputStream); ReadHeader(littleEndianDataInputStream); /* * read the records */ for (int i = 0; i < recordsPerBlock; i++) { DBTableRecord pdxTableRecord = new DBTableRecord(); pdxTableRecord.Read(pdxReaderListener, fields, inputStream); records.Add(pdxTableRecord); } } catch (Exception e) { throw new PDXReaderException("Exception in read", e); } }
public bool connect() { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(hostName, port); socket.NoDelay = true; @out = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket))); ExtendedDataInput @in = new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))); string body = "connect\n"; @out.writeBytes("API 0 "); @out.writeBytes(body.Length.ToString()); @out.writeByte('\n'); @out.writeBytes(body); @out.flush(); string line = @in.readLine(); int endPos = line.IndexOf(' '); if (endPos <= 0) { close(); throw new IOException("Invalid ack msg : " + line); //return false; } sessionID = line.Substring(0, endPos); int startPos = endPos + 1; endPos = line.IndexOf(' ', startPos); if (endPos != line.Length - 2) { close(); throw new IOException("Invalid ack msg : " + line); //return false; } if (line[endPos + 1] == '0') { remoteLittleEndian = false; @out = new BigEndianDataOutputStream(new BufferedStream(new NetworkStream(socket))); } else { remoteLittleEndian = true; } if (this.userId.Length > 0 && this.password.Length > 0) { login(); } if (this.initialScript != "") { run(initialScript); } return(true); }
/// <summary>names</summary> /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> public virtual void ReadFieldName(LittleEndianDataInputStream littleEndianDataInputStream ) { try { name = StringUtil.ReadString(littleEndianDataInputStream); } catch (Exception e) { throw new PDXReaderException("Exception in read", e); } }
/// <summary>read header</summary> /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> private void ReadHeader(LittleEndianDataInputStream littleEndianDataInputStream) { try { pxFileBlockHeader = new PXIndexBlockHeader(); pxFileBlockHeader.Read(littleEndianDataInputStream); } catch (Exception e) { throw new PDXReaderException("Exception in readHeader", e); } }
/// <summary>block header, 6 bytes</summary> /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> public virtual void Read(LittleEndianDataInputStream littleEndianDataInputStream) { try { nextBlockNumber = littleEndianDataInputStream.ReadUnsignedShort(); previousBlockNumber = littleEndianDataInputStream.ReadUnsignedShort(); offsetLastRecord = littleEndianDataInputStream.ReadUnsignedShort(); } catch (Exception e) { throw new PDXReaderException("Exception in read", e); } }
/// <summary>read a null terminated string from a LittleEndianDataInputStream</summary> /// <exception cref="System.IO.IOException"/> public static string ReadString(LittleEndianDataInputStream littleEndianDataInputStream ) { StringBuilder stringBuilder = new StringBuilder(); int c = littleEndianDataInputStream.ReadUnsignedByte(); while (c != 0) { stringBuilder.Append((char)c); c = littleEndianDataInputStream.ReadUnsignedByte(); } return(stringBuilder.ToString()); }
/// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> public virtual void Read(LittleEndianDataInputStream littleEndianDataInputStream) { try { blockNumberForKey = littleEndianDataInputStream.ReadInt(); statistics = littleEndianDataInputStream.ReadInt(); unknown = littleEndianDataInputStream.ReadInt(); } catch (Exception e) { throw new PDXReaderException("Exception in read", e); } }
/// <summary>read</summary> /// <exception cref="System.Exception"/> public virtual void Read(File file, PDXReaderListener pdxReaderListener) { try { /* * start */ pdxReaderListener.Start(); /* * set up streams */ BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream (file)); LittleEndianDataInputStream littleEndianDataInputStream = new LittleEndianDataInputStream (bufferedInputStream); try { /* * mark and read the headers */ bufferedInputStream.Mark(MaxHeaderSize); ReadHeaders(littleEndianDataInputStream); /* * call the api */ pdxReaderListener.Header(dbTableHeader); /* * read the block data */ bufferedInputStream.Reset(); ReadBlocks(bufferedInputStream, pdxReaderListener); /* * done */ pdxReaderListener.Finish(); } finally { littleEndianDataInputStream.Close(); bufferedInputStream.Close(); } } catch (Exception e) { throw new Exception("Exception in read", e); } }
/// <summary>read</summary> /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> private void ReadHeaders(LittleEndianDataInputStream littleEndianDataInputStream) { try { /* * read header */ dbTableHeader = new DBTableHeader(); dbTableHeader.Read(littleEndianDataInputStream); } catch (Exception e) { throw new PDXReaderException("Exception in readHeaders", e); } }
/// <summary>read the field descriptions</summary> /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> private void ReadFieldNames(LittleEndianDataInputStream littleEndianDataInputStream ) { try { foreach (DBTableField pdxTableField in fields) { pdxTableField.ReadFieldName(littleEndianDataInputStream); } } catch (Exception e) { throw new PDXReaderException("Exception in readFields", e); } }
public virtual bool tryReconnect() { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(hostName, port); @out = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket))); ExtendedDataInput @in = new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))); string body = "connect\n"; @out.writeBytes("API 0 "); @out.writeBytes(body.Length.ToString()); @out.writeByte('\n'); @out.writeBytes(body); @out.flush(); string line = @in.readLine(); int endPos = line.IndexOf(' '); if (endPos <= 0) { close(); return(false); } sessionID = line.Substring(0, endPos); int startPos = endPos + 1; endPos = line.IndexOf(' ', startPos); if (endPos != line.Length - 2) { close(); return(false); } if (line[endPos + 1] == '0') { remoteLittleEndian = false; @out = new BigEndianDataOutputStream(new BufferedStream(new NetworkStream(socket))); } else { remoteLittleEndian = true; } return(true); }
/// <summary>read the field descriptions</summary> /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> private void ReadFieldTypesAndSizes(LittleEndianDataInputStream littleEndianDataInputStream ) { try { fields = new List <DBTableField>(); for (int i = 0; i < numberFields; i++) { DBTableField pdxTableField = new DBTableField(); if (pdxTableField.ReadFieldTypeAndSize(littleEndianDataInputStream)) { fields.Add(pdxTableField); } } } catch (Exception e) { throw new PDXReaderException("Exception in readFields", e); } }
/// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> public override void Read(LittleEndianDataInputStream littleEndianDataInputStream ) { try { int blockType = littleEndianDataInputStream.ReadByte(); if (blockType != base.recordType.GetValue()) { throw new PDXReaderException("Block type mismatch"); } int sizeofBlock = littleEndianDataInputStream.ReadUnsignedShort(); if ((sizeofBlock * BlockSizeMultiplier) != base.sizeofBlock) { throw new PDXReaderException("Block type mismatch"); } } catch (IOException e) { throw new PDXReaderException("Exception reading inputStream", e); } }
/// <summary>Read all blocks</summary> /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> private void ReadBlocks(File file) { try { foreach (MBTableBlock mbTableBlock in blocks) { /* * set up streams */ BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream (file)); LittleEndianDataInputStream littleEndianDataInputStream = new LittleEndianDataInputStream (bufferedInputStream); try { /* * ffd */ long skipped = littleEndianDataInputStream.Skip(mbTableBlock.GetFileOffset()); if (0 != skipped) { /* * read */ mbTableBlock.Read(littleEndianDataInputStream); } } finally { littleEndianDataInputStream.Close(); bufferedInputStream.Close(); } } } catch (Exception e) { throw new PDXReaderException("Exception in read", e); } }
/// <summary>read</summary> /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> public virtual void Read(File file) { try { /* * set up streams */ BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream (file)); LittleEndianDataInputStream littleEndianDataInputStream = new LittleEndianDataInputStream (bufferedInputStream); try { /* * mark and read the headers */ bufferedInputStream.Mark(MaxHeaderSize); ReadHeaders(littleEndianDataInputStream); /* * read the block data */ bufferedInputStream.Reset(); ReadBlocks(bufferedInputStream); } finally { littleEndianDataInputStream.Close(); bufferedInputStream.Close(); } } catch (Exception e) { throw new PDXReaderException("Exception in read", e); } }
/// <summary>read data.</summary> /// <remarks>read data. This assumes that the inputStream is on byte 0 from the start of the block /// </remarks> /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> public virtual void Read(InputStream inputStream) { try { /* * read the header */ LittleEndianDataInputStream littleEndianDataInputStream = new LittleEndianDataInputStream (inputStream); ReadHeader(littleEndianDataInputStream); /* * read the records */ indexRecords = new List <PXIndexBlockRecord>(); PXIndexBlockRecord pxFileIndexRecord = new PXIndexBlockRecord(); pxFileIndexRecord.Read(littleEndianDataInputStream); indexRecords.Add(pxFileIndexRecord); } catch (Exception e) { throw new PDXReaderException("Exception in read", e); } }
/// <summary>types and sizes, 2 bytes per field</summary> /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> public virtual bool ReadFieldTypeAndSize(LittleEndianDataInputStream littleEndianDataInputStream ) { try { type = littleEndianDataInputStream.ReadUnsignedByte(); length = littleEndianDataInputStream.ReadUnsignedByte(); switch (type) { case unchecked ((int)(0x00)): { return(false); } case unchecked ((int)(0x01)): { fieldType = DBTableField.FieldType.A; break; } case unchecked ((int)(0x02)): { fieldType = DBTableField.FieldType.D; if (length != 4) { throw new Exception("Invalid field length '" + length + "' for type '" + type + "'" ); } break; } case unchecked ((int)(0x03)): { fieldType = DBTableField.FieldType.S; if (length != 2) { throw new Exception("Invalid field length '" + length + "' for type '" + type + "'" ); } break; } case unchecked ((int)(0x04)): { fieldType = DBTableField.FieldType.I; if (length != 4) { throw new Exception("Invalid field length '" + length + "' for type '" + type + "'" ); } break; } case unchecked ((int)(0x05)): { fieldType = DBTableField.FieldType.C; if (length != 8) { throw new Exception("Invalid field length '" + length + "' for type '" + type + "'" ); } break; } case unchecked ((int)(0x06)): { fieldType = DBTableField.FieldType.N; if (length != 8) { throw new Exception("Invalid field length '" + length + "' for type '" + type + "'" ); } break; } case unchecked ((int)(0x09)): { fieldType = DBTableField.FieldType.L; if (length != 1) { throw new Exception("Invalid field length '" + length + "' for type '" + type + "'" ); } break; } case unchecked ((int)(0xc)): { fieldType = DBTableField.FieldType.M; break; } case unchecked ((int)(0xd)): { fieldType = DBTableField.FieldType.B; break; } case unchecked ((int)(0xe)): { fieldType = DBTableField.FieldType.E; break; } case unchecked ((int)(0xf)): { fieldType = DBTableField.FieldType.O; break; } case unchecked ((int)(0x10)): { fieldType = DBTableField.FieldType.G; break; } case unchecked ((int)(0x14)): { fieldType = DBTableField.FieldType.T; if (length != 4) { throw new Exception("Invalid field length '" + length + "' for type '" + type + "'" ); } break; } case unchecked ((int)(0x15)): { fieldType = DBTableField.FieldType.Ts; if (length != 8) { throw new Exception("Invalid field length '" + length + "' for type '" + type + "'" ); } break; } case unchecked ((int)(0x16)): { fieldType = DBTableField.FieldType.Auto; break; } case unchecked ((int)(0x17)): { fieldType = DBTableField.FieldType.Bcd; if (length != 17) { throw new Exception("Invalid field length '" + length + "' for type '" + type + "'" ); } break; } case unchecked ((int)(0x18)): { fieldType = DBTableField.FieldType.Bytes; break; } default: { throw new PDXReaderException("Unknown field type '" + type + "'"); } } return(true); } catch (Exception e) { throw new PDXReaderException("Exception in read", e); } }
/// <summary>read (little endian)</summary> /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> public virtual void Read(LittleEndianDataInputStream littleEndianDataInputStream) { try { /* * The index record length is six greater than the sum of the lengths of the key fields. */ indexRecordLength = littleEndianDataInputStream.ReadUnsignedShort(); headerBlockSize = littleEndianDataInputStream.ReadUnsignedShort(); if (2048 != headerBlockSize) { throw new Exception("headerBlockSize was expected to be 2048, but '" + headerBlockSize + "' was found"); } int fileType = littleEndianDataInputStream.ReadUnsignedByte(); if (1 != fileType) { throw new Exception("PX file type was expected to be 1, but '" + fileType + "' was found" ); } dataBlockSizeCode = littleEndianDataInputStream.ReadUnsignedByte(); if (1 == dataBlockSizeCode) { blockSize = PXFileHeader.BlockSize.oneK; } else { if (2 == dataBlockSizeCode) { blockSize = PXFileHeader.BlockSize.twoK; } else { if (3 == dataBlockSizeCode) { blockSize = PXFileHeader.BlockSize.threeK; } else { if (4 == dataBlockSizeCode) { blockSize = PXFileHeader.BlockSize.fourK; } else { throw new Exception("Unknown block size code '" + dataBlockSizeCode + "'"); } } } } numberRecords = littleEndianDataInputStream.ReadInt(); blocksInUse = littleEndianDataInputStream.ReadUnsignedShort(); totalBlocksInFile = littleEndianDataInputStream.ReadUnsignedShort(); firstDataBlock = littleEndianDataInputStream.ReadUnsignedShort(); if (1 != firstDataBlock) { throw new Exception("firstDataBlock was expected to be 1, but '" + firstDataBlock + "' was found"); } lastDataBlock = littleEndianDataInputStream.ReadUnsignedShort(); indexRootBlock = littleEndianDataInputStream.ReadUnsignedShort(); numberIndexLevels = littleEndianDataInputStream.ReadUnsignedByte(); /* * The number of fields in the index is the same as the number of key fields for the table. */ numberIndexFields = littleEndianDataInputStream.ReadUnsignedByte(); } catch (Exception e) { throw new PDXReaderException("Exception in read", e); } }
/// <summary>read (little endian)</summary> /// <exception cref="System.Exception"/> public virtual void Read(LittleEndianDataInputStream littleEndianDataInputStream) { try { recordBufferSize = littleEndianDataInputStream.ReadUnsignedShort(); headerBlockSize = littleEndianDataInputStream.ReadUnsignedShort(); int tableType = littleEndianDataInputStream.ReadUnsignedByte(); if (0 == tableType) { this.tableType = DBTableHeader.TableType.keyed; } else { if (2 == tableType) { this.tableType = DBTableHeader.TableType.unkeyed; } else { throw new Exception("Unknown table type '" + tableType + "'"); } } dataBlockSizeCode = littleEndianDataInputStream.ReadUnsignedByte(); if (1 == dataBlockSizeCode) { blockSize = DBTableHeader.BlockSize.oneK; } else { if (2 == dataBlockSizeCode) { blockSize = DBTableHeader.BlockSize.twoK; } else { if (3 == dataBlockSizeCode) { blockSize = DBTableHeader.BlockSize.threeK; } else { if (4 == dataBlockSizeCode) { blockSize = DBTableHeader.BlockSize.fourK; } else { throw new Exception("Unknown block size code '" + dataBlockSizeCode + "'"); } } } } numberRecords = littleEndianDataInputStream.ReadInt(); blocksInUse = littleEndianDataInputStream.ReadUnsignedShort(); totalBlocksInFile = littleEndianDataInputStream.ReadUnsignedShort(); firstDataBlock = littleEndianDataInputStream.ReadUnsignedShort(); lastDataBlock = littleEndianDataInputStream.ReadUnsignedShort(); littleEndianDataInputStream.SkipBytes(15); // byte 0x21 numberFields = littleEndianDataInputStream.ReadUnsignedByte(); // byte 0x22 littleEndianDataInputStream.SkipBytes(1); // byte 0x23 numberKeyFields = littleEndianDataInputStream.ReadUnsignedByte(); littleEndianDataInputStream.SkipBytes(unchecked ((int)(0x54))); // byte 0x78 ReadFieldTypesAndSizes(littleEndianDataInputStream); // name littleEndianDataInputStream.SkipBytes(20); embeddedFilename = StringUtil.ReadString(littleEndianDataInputStream); /* * skip forward 250 bytes */ int skipBytes = 250; // dumpBytes(skipBytes, littleEndianDataInputStream); littleEndianDataInputStream.SkipBytes(skipBytes); ReadFieldNames(littleEndianDataInputStream); } catch (Exception e) { throw new Exception("Exception in read", e); } }
/// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> public override void Read(LittleEndianDataInputStream littleEndianDataInputStream ) { }
/// <summary>This method finds all the blocks with their types and offsets</summary> /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> private void PreReadBlocks(File file) { try { /* * set up streams */ BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream (file)); LittleEndianDataInputStream littleEndianDataInputStream = new LittleEndianDataInputStream (bufferedInputStream); try { /* * offset tracker */ int fileOffset = 0; /* * loop */ while (littleEndianDataInputStream.Available() > 1) { /* * read type */ int type = littleEndianDataInputStream.ReadByte(); /* * get an appropriate Block type */ MBTableBlock.RecordType recordType = MBTableBlock.GetRecordType(type); /* * ok? */ if (null != recordType) { MBTableBlock mbTableBlock = MBTableBlockFactory.GetMBTableBlock(recordType); /* * set the offset */ mbTableBlock.SetFileOffset(fileOffset); /* * pre-read */ int bytesPreRead = mbTableBlock.PreRead(littleEndianDataInputStream); /* * add to list */ blocks.Add(mbTableBlock); /* * skip forward to next one */ int bytesToSkip = mbTableBlock.GetSizeofBlock() - (1 + bytesPreRead); long skipped = littleEndianDataInputStream.Skip(bytesToSkip); if (0 != skipped) { /* * update the offset */ fileOffset += mbTableBlock.GetSizeofBlock(); } } } } finally { littleEndianDataInputStream.Close(); bufferedInputStream.Close(); } } catch (Exception e) { throw new PDXReaderException("Exception in read", e); } }
public void run() { ConcurrentDictionary <string, SubscribeInfo> subscribeInfos = dispatcher_.getSubscribeInfos(); Socket socket = this.socket_; try { if (bis_ == null) { bis_ = new BufferedStream(new NetworkStream(socket)); } ExtendedDataInput @in = null; while (!dispatcher_.isClose()) { if (@in == null) { bool isLittle = bis_.ReadByte() != 0; if (isLittle) { @in = new LittleEndianDataInputStream(bis_); } else { @in = new BigEndianDataInputStream(bis_); } } else { @in.readBoolean(); } @in.readLong(); long msgid = @in.readLong(); topics = @in.readString(); short flag = @in.readShort(); IEntityFactory factory = new BasicEntityFactory(); int form = flag >> 8; int type = flag & 0xff; bool extended = type >= 128; if (type >= 128) { type -= 128; } if (form < 0 || form > MAX_FORM_VALUE) { throw new IOException("Invalid form value: " + form); } if (type < 0 || type > MAX_TYPE_VALUE) { throw new IOException("Invalid type value: " + type); } DATA_FORM df = (DATA_FORM)form; DATA_TYPE dt = (DATA_TYPE)type; IEntity body; try { body = factory.createEntity(df, dt, @in, extended); } catch { throw; } if (body.isTable()) { foreach (string HATopic in topics.Split(',')) { string topic = dispatcher_.getTopicForHATopic(HATopic); if (topic == null) { throw new Exception("Subscription with topic " + HATopic + " does not exist. "); } if (!successTopics.Contains(topic)) { SubscribeInfo subscribeInfo = null; //Prevents a situation where streaming data arrives earlier than the subscription succeeds. lock (subscribeInfos) { if (!subscribeInfos.TryGetValue(topic, out subscribeInfo)) { throw new Exception("Subscription with topic " + topic + " does not exist. "); } } lock (subscribeInfo) { if (subscribeInfo.getConnectState() != ConnectState.RECEIVED_SCHEMA) { subscribeInfo.setConnectState(ConnectState.RECEIVED_SCHEMA); } else { throw new Exception("Subscription with topic " + topic + " already has a thread parsing the stream data. "); } } successTopics.Add(topic); } } } else if (body.isVector()) { foreach (string HATopic in topics.Split(',')) { string topic = dispatcher_.getTopicForHATopic(HATopic); if (topic == null) { throw new Exception("Subscription with topic " + HATopic + " does not exist. "); } SubscribeInfo subscribeInfo = null; if (!subscribeInfos.TryGetValue(topic, out subscribeInfo)) { throw new Exception("Subscription with topic " + topic + " does not exist. "); } BasicAnyVector dTable = (BasicAnyVector)body; int colSize = dTable.rows(); int rowSize = dTable.getEntity(0).rows(); if (rowSize == 1) { BasicMessage rec = new BasicMessage(msgid, topic, dTable); dispatcher_.dispatch(rec); } else if (rowSize > 1) { List <IMessage> messages = new List <IMessage>(rowSize); for (int i = 0; i < rowSize; i++) { BasicAnyVector row = new BasicAnyVector(colSize); for (int j = 0; j < colSize; j++) { AbstractVector vector = (AbstractVector)dTable.getEntity(j); IEntity entity = vector.get(i); row.setEntity(j, entity); } BasicMessage rec = new BasicMessage(msgid, topic, row); messages.Add(rec); } dispatcher_.batchDispatch(messages); } lock (subscribeInfo) { subscribeInfo.setMsgId(msgid); } } } else { throw new Exception("message body has an invalid format. Vector or table is expected"); } } } catch (Exception e) { System.Console.Out.WriteLine(e.StackTrace); foreach (string topic in successTopics) { SubscribeInfo subscribeInfo = null; if (!subscribeInfos.TryGetValue(topic, out subscribeInfo)) { System.Console.Out.WriteLine("Subscription with topic " + topic + " doesn't exist. "); } else { lock (subscribeInfo) { subscribeInfo.setConnectState(ConnectState.NO_CONNECT); } } } } finally { try { socket.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.Write(ex.StackTrace); } } Console.WriteLine("MessageParser thread stopped."); }
public virtual void run() { Socket socket = this.socket; try { if (bis == null) { bis = new BufferedInputStream(socket.InputStream); } long offset = -1; ExtendedDataInput @in = null; //isRemoteLittleEndian ? new LittleEndianDataInputStream(bis) : new BigEndianDataInputStream(bis); while (true) { if (@in == null) { bool?b = bis.read() != 0; //true/false : little/big if (b == true) { @in = new LittleEndianDataInputStream(bis); } else { @in = new BigEndianDataInputStream(bis); } } else { @in.readBoolean(); } long msgid = @in.readLong(); if (offset == -1) { offset = msgid; } else { assert(offset == msgid); } string topic = @in.readString(); short flag = @in.readShort(); EntityFactory factory = new BasicEntityFactory(); int form = flag >> 8; int type = flag & 0xff; if (form < 0 || form > MAX_FORM_VALUE) { throw new IOException("Invalid form value: " + form); } if (type < 0 || type > MAX_TYPE_VALUE) { throw new IOException("Invalid type value: " + type); } Entity_DATA_FORM df = Enum.GetValues(typeof(Entity_DATA_FORM))[form]; Entity_DATA_TYPE dt = Enum.GetValues(typeof(Entity_DATA_TYPE))[type]; Entity body; try { body = factory.createEntity(df, dt, @in); } catch (Exception exception) { throw exception; } if (body.Vector) { BasicAnyVector dTable = (BasicAnyVector)body; int colSize = dTable.rows(); int rowSize = dTable.getEntity(0).rows(); if (rowSize >= 1) { if (rowSize == 1) { BasicMessage rec = new BasicMessage(msgid, topic, dTable); dispatcher.dispatch(rec); } else { IList <IMessage> messages = new List <IMessage>(rowSize); for (int i = 0; i < rowSize; i++) { BasicAnyVector row = new BasicAnyVector(colSize); for (int j = 0; j < colSize; j++) { AbstractVector vector = (AbstractVector)dTable.getEntity(j); Entity entity = vector.get(i); row.setEntity(j, entity); } BasicMessage rec = new BasicMessage(msgid, topic, row); messages.Add(rec); msgid++; } dispatcher.batchDispatch(messages); } } offset += rowSize; } else { throw new Exception("message body has an invalid format.vector is expected"); } } } catch (Exception e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } finally { try { socket.close(); } catch (IOException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } } }
public bool connect(string hostName, int port) { lock (threadLock) { try { if (sessionID.Length > 0) { return(true); } this.hostName = hostName; this.port = port; socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(hostName, port); socket.NoDelay = true; @out = new LittleEndianDataOutputStream(new BufferedStream(new NetworkStream(socket))); ExtendedDataInput @in = new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))); string body = "connect\n"; @out.writeBytes("API 0 "); @out.writeBytes(body.Length.ToString()); @out.writeChar('\n'); @out.writeBytes(body); @out.flush(); string line = @in.readLine(); int endPos = line.IndexOf(' '); if (endPos <= 0) { close(); return(false); } sessionID = line.Substring(0, endPos); int startPos = endPos + 1; endPos = line.IndexOf(' ', startPos); if (endPos != line.Length - 2) { close(); return(false); } if (line[endPos + 1] == '0') { remoteLittleEndian = false; @out = new BigEndianDataOutputStream(new BufferedStream(new NetworkStream(socket))); } else { remoteLittleEndian = true; } return(true); } catch (Exception ex) { throw ex; } } }
/// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> public abstract void Read(LittleEndianDataInputStream littleEndianDataInputStream );
public void run() { Socket socket = this.socket; try { long offset = -1; if (bis == null) { bis = new BufferedStream(new NetworkStream(socket)); } ExtendedDataInput @in = null; while (true) { if (@in == null) { bool isLittle = bis.ReadByte() != 0; if (isLittle) { @in = new LittleEndianDataInputStream(bis); } else { @in = new BigEndianDataInputStream(bis); } } else { @in.readBoolean(); } @in.readLong(); long msgid = @in.readLong(); if (offset == -1) { offset = msgid; } topic = @in.readString(); short flag = @in.readShort(); IEntityFactory factory = new BasicEntityFactory(); int form = flag >> 8; int type = flag & 0xff; if (form < 0 || form > MAX_FORM_VALUE) { throw new IOException("Invalid form value: " + form); } if (type < 0 || type > MAX_TYPE_VALUE) { throw new IOException("Invalid type value: " + type); } DATA_FORM df = (DATA_FORM)form; DATA_TYPE dt = (DATA_TYPE)type; IEntity body; try { body = factory.createEntity(df, dt, @in); } catch { throw; } if (body.isTable()) { if (body.rows() != 0) { throw new Exception("When message is table, it should be empty."); } } else if (body.isVector()) { dispatcher.setMsgId(topic, msgid); BasicAnyVector dTable = (BasicAnyVector)body; int colSize = dTable.rows(); int rowSize = dTable.getEntity(0).rows(); if (rowSize == 1) { BasicMessage rec = new BasicMessage(msgid, topic, dTable); dispatcher.dispatch(rec); } else if (rowSize > 1) { List <IMessage> messages = new List <IMessage>(rowSize); for (int i = 0; i < rowSize; i++) { BasicAnyVector row = new BasicAnyVector(colSize); for (int j = 0; j < colSize; j++) { AbstractVector vector = (AbstractVector)dTable.getEntity(j); IEntity entity = vector.get(i); row.setEntity(j, entity); } BasicMessage rec = new BasicMessage(msgid, topic, row); messages.Add(rec); msgid++; } dispatcher.batchDispatch(messages); } offset += rowSize; } else { throw new Exception("message body has an invalid format. Vector or table is expected"); } } } catch (Exception) { if (dispatcher.isClosed(topic)) { return; } else { dispatcher.tryReconnect(topic); } } finally { try { socket.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.Write(ex.StackTrace); } } }