예제 #1
0
        protected override void beginRead(incommingReadDelegate callback)
        {
            byte[]  buffer = new byte[CHUNK_SIZE];
            ReadAcc acc    = new ReadAcc(0, false, false, new LinkedList <char>());

            SocketExceptionContainer(() =>
            {
                connection.BeginReceive(buffer, 0, CHUNK_SIZE, SocketFlags.None,
                                        (IAsyncResult ar) => { onReadOp(buffer, acc, callback); }, null);
            });
        }
예제 #2
0
        private void onReadOp(byte[] buffer, ReadAcc a, incommingReadDelegate callback)
        {
            a.wasInBody = a.wasInBody || a.inBody;

            foreach (byte b in buffer)
            {
                if (a.inBody)
                {
                    a.msg.AddLast(Convert.ToChar(b));

                    a.fLoc   = b == FLANK[a.fLoc - 1] ? a.fLoc - 1 : CHUNK_SIZE;
                    a.inBody = a.fLoc != 0;
                }
                else
                {
                    a.fLoc   = b == FLANK[a.fLoc] ? a.fLoc + 1 : 0;
                    a.inBody = a.fLoc == CHUNK_SIZE;
                }
            }

            if (!a.wasInBody || a.inBody)
            {
                SocketExceptionContainer(() =>
                {
                    connection.BeginReceive(buffer, 0, CHUNK_SIZE, SocketFlags.None,
                                            (IAsyncResult ar) => { onReadOp(buffer, a, callback); }, null);
                });
            }
            else
            {
                for (int i = 0; i < CHUNK_SIZE; i++)
                {
                    a.msg.RemoveLast();
                }
                string ret = new string(a.msg.ToArray()).Trim();

                callback(ret);
            }
        }