コード例 #1
0
ファイル: Link.cs プロジェクト: ouchzsc/RazorFrameWork
        //0: ok, 1: NetUnconnected, 2: OutputBufferExceed
        public int SendProtocol(int type, OctetsStream octetsStream)
        {
            if (ConnectState != ConnectState.Connected)
            {
                return(1);
            }

            //此时连接并没有断开,为了保证协议不丢失,在ProcessOutput中再断开连接(如果在此处就断开连接,可能会导致逻辑不一致吧???)
            if (_outputBuf.Count >= OutputBufferSize)
            {
                return(2);
            }

            try
            {
                _sendos.MarshalSize(type).MarshalOctets(octetsStream.Data);
                _outputBuf.Append(_outputSecurity.Update(_sendos.Data));
            }
            finally
            {
                _sendos.Reset();
                //_sendos.ResetWithSize(ReserveOutputBufSize);
            }

            return(0);
        }
コード例 #2
0
ファイル: Link.cs プロジェクト: ouchzsc/RazorFrameWork
        private void ProcessInput()
        {
            if (!_socket.Poll(0, SelectMode.SelectRead))
            {
                return;
            }

            int received = _socket.Receive(_input, _input.Length, SocketFlags.None);

            if (received <= 0)
            {
                throw new Exception("Socket.Receive received = " + received);
            }
            _inputBuf.Append(_inputSecurity.Update(_recvo.WrapBytes(_input, received)));
            var os = _recvos.WrapOctets(_inputBuf);

            while (os.Remaining > 0)
            {
                int tranpos = os.Begin();
                try
                {
                    int type = os.UnmarshalSize();
                    int size = os.UnmarshalSize();
                    if (size > os.Remaining)
                    {
                        os.Rollback(tranpos);
                        break; // not enough
                    }
                    _protocolStrs.Add(new ProtocolStruct(type, os.UnmarshalFixedSizeBytes(size)));

                    //因为回调里,可能Close了Link,则os底层的_inputBuf也已经clear了。
                    if (ConnectState != ConnectState.Connected)
                    {
                        return;
                    }
                }
                catch (MarshalException)
                {
                    os.Rollback(tranpos);
                    break;
                }
            }

            if (os.Position != 0)
            {
                _inputBuf.EraseAndCompact(os.Position, ReserveInputBufSize);
            }
        }
コード例 #3
0
        //0: ok, 1: NetUnconnected, 2: OutputBufferExceed
        public int SendProtocol(int type, byte[] data)
        {
            if (!Connected)
            {
                return(1);
            }

            if (_outputBuf.Count >= OutputBufferSize)
            {
                return(2);
            }

            var os = new OctetsStream();

            os.MarshalSize(type).Marshal(data);
            var emptyBeforeAdd = (_outputBuf.Count == 0);

            _outputBuf.Append(_outputSecurity.Update(os.Data));
            if (emptyBeforeAdd)
            {
                BeginSend();
            }
            return(0);
        }
コード例 #4
0
        private void BeginReceive()
        {
            Socket sock = _socket; //closure 的问题,需要这句

            try
            {
                sock.BeginReceive(_input, 0, InputSize, SocketFlags.None, ar =>
                {
                    _actions.Enqueue(() =>
                    {
                        try
                        {
                            int received = sock.EndReceive(ar);
                            if (received > 0)
                            {
                                _inputBuf.Append(_inputSecurity.Update(Octets.Wrap(_input, received)));

                                var os = OctetsStream.Wrap(_inputBuf);
                                while (os.Remaining > 0)
                                {
                                    int tranpos = os.Begin();
                                    try
                                    {
                                        int type = os.UnmarshalSize();
                                        int size = os.UnmarshalSize();
                                        if (size > os.Remaining)
                                        {
                                            os.Rollback(tranpos);
                                            break; // not enough
                                        }
                                        _protocols.Enqueue(new Protocol(type, os.UnmarshalFixedSizeBytes(size)));
                                    }
                                    catch (MarshalException)
                                    {
                                        os.Rollback(tranpos);
                                        break;
                                    }
                                }

                                if (os.Position != 0)
                                {
                                    _inputBuf.EraseAndCompact(os.Position, ReserveInputBufSize);
                                }
                                BeginReceive();
                            }
                            else
                            {
                                Close(sock, new Exception("the socket channel has reached end-of-stream"));
                            }
                        }
                        catch (Exception e)
                        {
                            Close(sock, e);
                        }
                    });
                }, null);
            }
            catch (Exception e)
            {
                Close(sock, e);
            }
        }
コード例 #5
0
        public Octets Update(Octets oin)
        {
            _legacyIn.Append(oin);
            _blenTotol = (uint)(_legacyIn.Count * 8 - _l);
            _legacyIn.Reserve(_legacyIn.Count + 3);

            _rptr = 0;
            _blen = 7;
            Octets oout = oin;

            oout.Clear();
            uint histhead = _histptr;

            while (_blenTotol > _blen)
            {
                _adjustL    = _l;
                _adjustRptr = _rptr;
                uint val = Fetch();

                if (val < 0x80000000)
                {
                    if (!Passbits(8))
                    {
                        break;
                    }
                    _history[_histptr++] = (byte)(val >> 24);
                    continue;
                }
                if (val < 0xc0000000)
                {
                    if (!Passbits(9))
                    {
                        break;
                    }
                    _history[_histptr++] = (byte)(((val >> 23) | 0x80) & 0xff);
                    continue;
                }

                uint len;
                uint off = 0;
                if (val >= 0xf0000000)
                {
                    if (!Passbits(10))
                    {
                        break;
                    }
                    off = (val >> 22) & 0x3f;
                    if (off == (uint)Mppc.CtrlOffEob)
                    {
                        uint advance = 8 - (_l & 7);
                        if (advance < 8)
                        {
                            if (!Passbits(advance))
                            {
                                break;
                            }
                        }
                        oout.Append(_history, (int)histhead, (int)(_histptr - histhead));
                        if (_histptr == (uint)Mppc.MppcHistLen)
                        {
                            _histptr = 0;
                        }
                        histhead = _histptr;
                        continue;
                    }
                }
                else if (val >= 0xe0000000)
                {
                    if (!Passbits(12))
                    {
                        break;
                    }
                    off = ((val >> 20) & 0xff) + 64;
                }
                else if (val >= 0xc0000000)
                {
                    if (!Passbits(16))
                    {
                        break;
                    }
                    off = ((val >> 16) & 0x1fff) + 320;
                }


                val = Fetch();
                if (val < 0x80000000)
                {
                    if (!Passbits(1))
                    {
                        break;
                    }
                    len = 3;
                }
                else if (val < 0xc0000000)
                {
                    if (!Passbits(4))
                    {
                        break;
                    }
                    len = 4 | ((val >> 28) & 3);
                }
                else if (val < 0xe0000000)
                {
                    if (!Passbits(6))
                    {
                        break;
                    }
                    len = 8 | ((val >> 26) & 7);
                }
                else if (val < 0xf0000000)
                {
                    if (!Passbits(8))
                    {
                        break;
                    }
                    len = 16 | ((val >> 24) & 15);
                }
                else if (val < 0xf8000000)
                {
                    if (!Passbits(10))
                    {
                        break;
                    }
                    len = 32 | ((val >> 22) & 0x1f);
                }
                else if (val < 0xfc000000)
                {
                    if (!Passbits(12))
                    {
                        break;
                    }
                    len = 64 | ((val >> 20) & 0x3f);
                }
                else if (val < 0xfe000000)
                {
                    if (!Passbits(14))
                    {
                        break;
                    }
                    len = 128 | ((val >> 18) & 0x7f);
                }
                else if (val < 0xff000000)
                {
                    if (!Passbits(16))
                    {
                        break;
                    }
                    len = 256 | ((val >> 16) & 0xff);
                }
                else if (val < 0xff800000)
                {
                    if (!Passbits(18))
                    {
                        break;
                    }
                    len = 0x200 | ((val >> 14) & 0x1ff);
                }
                else if (val < 0xffc00000)
                {
                    if (!Passbits(20))
                    {
                        break;
                    }
                    len = 0x400 | ((val >> 12) & 0x3ff);
                }
                else if (val < 0xffe00000)
                {
                    if (!Passbits(22))
                    {
                        break;
                    }
                    len = 0x800 | ((val >> 10) & 0x7ff);
                }
                else if (val < 0xfff00000)
                {
                    if (!Passbits(24))
                    {
                        break;
                    }
                    len = 0x1000 | ((val >> 8) & 0xfff);
                }
                else
                {
                    _l    = _adjustL;
                    _rptr = _adjustRptr;
                    break;
                }

                if (_histptr < off || _histptr + len > (uint)Mppc.MppcHistLen)
                {
                    break;
                }
                // Array.Copy(history, histptr - off, history, histptr, histptr - histhead);
                LameCopy(_history, (int)_histptr, (int)(_histptr - off), (int)len);
                // Array.Copy(history, histptr - off, history, histptr, len);
                _histptr += len;
            }

            oout.Append(_history, (int)histhead, (int)(_histptr - histhead));
            _legacyIn.Erase(0, (int)_rptr);
            return(oout);
        }