Example #1
0
        private void ReadFrom(byte[] src)
        {
            var len = 0;

            if (_pos < PROTOCOL_TYPE_SIZE)
            {
                len = Math.Min(PROTOCOL_TYPE_SIZE - _pos, src.Length);

                if (len > 0)
                {
                    _pTypeBuffer = _pTypeBuffer.Concat(src.Take(len)).ToArray();
                    src = src.Skip(len).ToArray();

                    if (_pTypeBuffer.Length == PROTOCOL_TYPE_SIZE)
                    {
                        _pTypeBuffer = _pTypeBuffer.Reverse().ToArray();

                        var c = BitConverter.ToInt32(_pTypeBuffer, 0);
                    }

                    _pos += len;
                }

            }

            if (_pos >= PROTOCOL_TYPE_SIZE && _pos < PROTOCOL_TYPE_SIZE + HEADER_SIZE)
            {
                len = Math.Min(PROTOCOL_TYPE_SIZE + HEADER_SIZE - _pos, src.Length);
                if (len > 0)
                {
                    _pLenBuffer = _pLenBuffer.Concat(src.Take(len)).ToArray();
                    src = src.Skip(len).ToArray();

                    if (_pLenBuffer.Length == HEADER_SIZE)
                    {
                        _pLenBuffer = _pLenBuffer.Reverse().ToArray();

                        _dataLength = BitConverter.ToInt32(_pLenBuffer, 0);
                    }

                    _pos += len;
                }
            }

            if (_pos >= PROTOCOL_TYPE_SIZE + HEADER_SIZE)
            {
                len = Math.Min(_dataLength + PROTOCOL_TYPE_SIZE + HEADER_SIZE - _pos, src.Length);
                if (len > 0)
                {
                    _pDataBuffer = _pDataBuffer.Concat(src.Take(len)).ToArray();
                    src = src.Skip(len).ToArray();

                    _pos += len;
                }
            }
            if (Remain == 0)
            {
                //var rawStr = Encoding.UTF8.GetString(_pDataBuffer, 0, _pDataBuffer.Length);

                //_testMsg = rawStr;
                //if (OnDataReceived != null)
                //{
                //    OnDataReceived(rawStr);
                //}

                //Sysinfo("Message Received.");

                var a3p = new AMF3Protocol();
                //a3p.SetBytes
                var da = new ByteArray();
                da.WriteBytes(_pDataBuffer, 0, _pDataBuffer.Length);
                a3p.SetBytes(da);

                if (OnAMFMsgReceived != null)
                {
                    OnAMFMsgReceived(a3p);
                }

                Reset();

            }

            if (src.Length > 0)
            {
                ReadFrom(src);
            }
        }
Example #2
0
        public void Send(string act, string cmd, Object data)
        {
            if (!_tcpClient.Connected)
                throw new Exception("Closed, cannot send data.");

            var reqResVO = new ReqResVO();

            reqResVO.accountId = SessionInfo._currentSysUserVO == null ?
                0 : SessionInfo._currentSysUserVO.suAccountVO.saId;
            reqResVO.userId = SessionInfo._currentSysUserVO == null ?
                0 : SessionInfo._currentSysUserVO.suId;

            reqResVO.sessionId = 0;
            reqResVO.action = act;
            reqResVO.command = cmd;
            reqResVO.data = data;

            //var id: String = getIdByVO(reqResVO);

            //if(callback != null) _callbackMap[id] = callback;

            //Logger.debug(SocketService,reqResVO.toString());

            //var protocol: IProtocol = ProtocolBuilder.newProtocol(ProtocolType.AMF3_PROTOCOL);

            var protocol = new AMF3Protocol();

            protocol.SetData(reqResVO);

            var b1 = protocol.GetBytes();

            var b2 = FluorineFx.Util.Convert.ToByteArray(b1);

            var msg = new ByteArray();

            msg.WriteInt(1);
            msg.WriteInt((int)b1.Length);

            var b3 = b2.Take((int)b1.Length).ToArray();

            msg.WriteBytes(b3, 0, b3.Length);

            b3 = FluorineFx.Util.Convert.ToByteArray(msg).Take((int)msg.Length).ToArray();

            Sysinfo("Sending " + act + "#" + cmd);

            _tcpClient.Client.Send(b3);
        }