LumiSoft.Net._FixedStack.Push C# (CSharp) Method

Push() public method

Pushes new bytes to stack.(Last in, first out).
public Push ( byte bytes, int count ) : int
bytes byte
count int Count to push from bytes parameter
return int
        public int Push(byte[] bytes,int count)
        {
            int termLen = m_TerminaTor.Length;

            if(bytes.Length > termLen){
                throw new Exception("bytes.Length is too big, can't be more than terminator.length !");
            }
            if(count > termLen){
                throw new Exception("count is too big, can't be more than terminator.length !");
            }

            // Move stack bytes which will stay and append new ones
            //	Array.Copy(m_SackList,count,m_SackList,0,m_SackList.Length - count);
            //	Array.Copy(bytes,0,m_SackList,m_SackList.Length - count,count);
            // Code above is slower than code below, when message size > 5 mb

            // Append new bytes to end and remove first bytes
            if(count != termLen){
                byte[] newStack = new byte[termLen];
                for(int i=0;i<termLen;i++){
                    // Write old bytes
                    if(termLen - count > i){
                        newStack[i] = m_SackList[count + i];
                    }
                    // Write new bytes
                    else{
                        newStack[i] = bytes[i - (termLen - count)];
                    }
                }
                m_SackList = newStack;
            }
            // Push count is equal to stack, just set is as new stack
            else{
                m_SackList = bytes;
            }

            //	int index = Array.IndexOf(m_SackList,m_TerminaTor[0]);
            // Code above is slower than code below, when message size > 5 mb

            int index = -1;
            for(int i=0;i<termLen;i++){
                if(m_SackList[i] == m_TerminaTor[0]){
                    index = i;
                    break;
                }
            }

            if(index > -1){
                if(index == 0){
                    // Check if contains full terminator
                    for(int i=0;i<m_SackList.Length;i++){
                        if(m_SackList[i] != m_TerminaTor[i]){
                            return 1;
                        }
                    }
                    return 0; // If reaches so far, contains terminator
                }

                return 1;
            }
            else{
                return termLen;
            }
        }

Usage Example

        /// <summary>
        /// Reads reply from socket.
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="replyData">Data that has been readen from socket.</param>
        /// <param name="addData">Data that has will be written at the beginning of read data. This param may be null.</param>
        /// <param name="maxLength">Maximum Length of data which may read.</param>
        /// <param name="cmdIdleTimeOut">Command idle time out in milliseconds.</param>
        /// <param name="terminator">Terminator string which terminates reading. eg '\r\n'.</param>
        /// <param name="removeFromEnd">Removes following string from reply.NOTE: removes only if ReadReplyCode is Ok.</param>		
        /// <returns>Return reply code.</returns>
        public static ReadReplyCode ReadData(Socket socket,out MemoryStream replyData,byte[] addData,int maxLength,int cmdIdleTimeOut,string terminator,string removeFromEnd)
        {
            ReadReplyCode replyCode = ReadReplyCode.Ok;
            replyData = null;

            try
            {
                replyData = new MemoryStream();
                _FixedStack stack = new _FixedStack(terminator);
                int nextReadWriteLen = 1;

                long lastDataTime = DateTime.Now.Ticks;
                while(nextReadWriteLen > 0){
                    if(socket.Available >= nextReadWriteLen){
                        //Read byte(s)
                        byte[] b = new byte[nextReadWriteLen];
                        int countRecieved = socket.Receive(b);

                        // Write byte(s) to buffer, if length isn't exceeded.
                        if(replyCode != ReadReplyCode.LengthExceeded){
                            replyData.Write(b,0,countRecieved);
                        }

                        // Write to stack(terminator checker)
                        nextReadWriteLen = stack.Push(b,countRecieved);

                        //---- Check if maximum length is exceeded ---------------------------------//
                        if(replyCode != ReadReplyCode.LengthExceeded && replyData.Length > maxLength){
                            replyCode = ReadReplyCode.LengthExceeded;
                        }
                        //--------------------------------------------------------------------------//

                        // reset last data time
                        lastDataTime = DateTime.Now.Ticks;
                    }
                    else{
                        //---- Idle and time out stuff ----------------------------------------//
                        if(DateTime.Now.Ticks > lastDataTime + ((long)(cmdIdleTimeOut)) * 10000){
                            replyCode = ReadReplyCode.TimeOut;
                            break;
                        }
                        System.Threading.Thread.Sleep(50);
                        //---------------------------------------------------------------------//
                    }
                }

                // If reply is ok then remove chars if any specified by 'removeFromEnd'.
                if(replyCode == ReadReplyCode.Ok && removeFromEnd.Length > 0){
                    replyData.SetLength(replyData.Length - removeFromEnd.Length);
                }
            }
            catch{
                replyCode = ReadReplyCode.UnKnownError;
            }

            return replyCode;
        }
All Usage Examples Of LumiSoft.Net._FixedStack::Push