Example #1
0
        private static void ProccessData_Len(Hashtable param)
        {
            BufferedSocket socket   = (BufferedSocket)param["socket"];
            MemoryStream   strm     = (MemoryStream)param["strm"];
            object         tag      = param["tag"];
            SocketCallBack callBack = (SocketCallBack)param["callBack"];

            long dataAvailable = socket.AvailableInBuffer;

            if (dataAvailable > 0)
            {
                byte[] data = new byte[dataAvailable];
                // Ensure that we don't get more data than needed !!!
                if (dataAvailable > ((long)param["lengthToRead"] - (long)param["readedCount"]))
                {
                    data = new byte[(long)param["lengthToRead"] - (long)param["readedCount"]];
                }
                int countRecieved = socket.ReceiveFromFuffer(data);

                // Increase readed count
                param["readedCount"] = ((long)param["readedCount"] + data.Length);

                if ((long)param["readedCount"] < (long)param["maxLength"])
                {
                    strm.Write(data, 0, data.Length);
                }
                // Message size exceeded, we must junk stream data.
                else if (strm.Length > 0)
                {
                    strm.SetLength(0);
                }
            }

            // We got all data successfully, call EndRecieve call back
            if ((long)param["readedCount"] == (long)param["lengthToRead"])
            {
                callBack(SocketCallBackResult.Ok, (long)param["readedCount"], null, tag);
            }
            else
            {
                // Recieve next bytes
                byte[] buff = new byte[1024];
                param["recieveBuffer"] = buff;
                socket.BeginReceive(buff, 0, buff.Length, 0, new AsyncCallback(OnRecievedData), param);
            }
        }
Example #2
0
        private static void ProccessData_Term(Hashtable param)
        {
            BufferedSocket socket           = (BufferedSocket)param["socket"];
            MemoryStream   strm             = (MemoryStream)param["strm"];
            string         removeFromEnd    = (string)param["removeFromEnd"];
            _FixedStack    stack            = (_FixedStack)param["stack"];
            int            nextReadWriteLen = (int)param["nextReadWriteLen"];
            object         tag      = param["tag"];
            SocketCallBack callBack = (SocketCallBack)param["callBack"];

            while (nextReadWriteLen > 0)
            {
                // We used buffer, request more data
                if (socket.AvailableInBuffer < nextReadWriteLen)
                {
                    // Store nextReadWriteLen for next call of this command
                    param["nextReadWriteLen"] = nextReadWriteLen;

                    // Recieve next bytes
                    byte[] buff = new byte[1024];
                    param["recieveBuffer"] = buff;
                    socket.BeginReceive(buff, 0, buff.Length, 0, new AsyncCallback(OnRecievedData), param);

                    // End this method, if data arrives, this method is called again
                    return;
                }

                //Read byte(s)
                byte[] b             = new byte[nextReadWriteLen];
                int    countRecieved = socket.ReceiveFromFuffer(b);

                // Increase readed count
                param["readedCount"] = ((long)param["readedCount"] + countRecieved);

                // Write byte(s) to buffer, if length isn't exceeded.
                if ((long)param["readedCount"] < (long)param["maxLength"])
                {
                    strm.Write(b, 0, countRecieved);
                }
                // Message size exceeded, we must junk stream data.
                else if (strm.Length > 0)
                {
                    strm.SetLength(0);
                }

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


            // If we reach so far, then we have successfully readed data

            if ((long)param["readedCount"] < (long)param["maxLength"])
            {
                // Remove "removeFromEnd" from end
                if (removeFromEnd.Length > 0 && strm.Length > removeFromEnd.Length)
                {
                    strm.SetLength(strm.Length - removeFromEnd.Length);
                }
                strm.Position = 0;

                // We got all data successfully, call EndRecieve call back
                callBack(SocketCallBackResult.Ok, (long)param["readedCount"], null, tag);
            }
            else
            {
                callBack(SocketCallBackResult.LengthExceeded, (long)param["readedCount"], null, tag);
            }
        }