コード例 #1
0
        private void ConnectionProcess(TcpSocketSaeaSession session)
        {
            if (Interlocked.Exchange(ref _manager_login, 1) == 0)
            {
                this.SendAck(session, SessionWorkType.ManagerSession);

                _manager_session  = session;
                session.AppTokens = new object[]
                {
                    SessionWorkType.ManagerSession,
                    null
                };

                //获取所有主连接
                SendMessageHelper.SendMessage(session, MsgCommand.Msg_Pull_Session);
            }
            else
            {
                this.SendAck(session, SessionWorkType.ManagerWorkSession);
                var sessionBased = new TcpProxySessionBased(session);
                session.AppTokens = new object[]
                {
                    SessionWorkType.ManagerWorkSession,
                    sessionBased
                };

                this._onSessionNotifyProc(SessionCompletedNotify.OnConnected, sessionBased as SessionHandler);
            }
        }
        private void ConnectedHandler(TcpSocketSaeaSession session)
        {
            if (Interlocked.Exchange(ref _managerLoginSign, CHANNEL_LOGIN) == CHANNEL_LOGOUT)
            {
                //表示代理管理连接登陆
                this.SendAck(session, SessionWorkType.ManagerSession);

                _managerSession   = session;
                session.AppTokens = new object[]
                {
                    SessionWorkType.ManagerSession,
                    null
                };

                //获取所有主连接
                MessageHelper.SendMessage(session, MessageHead.Msg_Pull_Session);
            }
            else
            {
                this.SendAck(session, SessionWorkType.ManagerWorkSession);
                var sessionBased = new TcpProxySessionBased(session);
                session.AppTokens = new object[]
                {
                    SessionWorkType.ManagerWorkSession,
                    sessionBased
                };

                this._onSessionNotifyProc(SessionCompletedNotify.OnConnected, sessionBased as SessionProviderContext);
            }
        }
コード例 #3
0
        private void ProcessSessionClose(byte[] data)
        {
            long     id = BitConverter.ToInt64(data, 1);
            GCHandle gc = GCHandle.FromIntPtr(new IntPtr(id));

            TcpProxySessionBased session = gc.Target as TcpProxySessionBased;

            this._onSessionNotifyProc(SessionCompletedNotify.OnClosed, session as SessionHandler);
            this._sessionList.Remove(session);//移除List引用
            session.Buffer.Clear();
            session.Dispose();
            //////////////////////////////check
        }
コード例 #4
0
        private void ProcessPackage(byte[] data)
        {
            long                 id      = BitConverter.ToInt64(data, 1);
            GCHandle             gc      = GCHandle.FromIntPtr(new IntPtr(id));
            TcpProxySessionBased session = gc.Target as TcpProxySessionBased;

            if (session == null)
            {
                this.ConsoleWriteLine("DEBUG ProcessPackage SESSION NULL", ConsoleColor.Red);
                return;
            }

            byte[] bytes = new byte[data.Length - (sizeof(Int64) + 1)];
            Array.Copy(data, sizeof(Int64) + 1, bytes, 0, bytes.Length);

            session._receiveTransferredBytes = bytes.Length;
            this._onSessionNotifyProc(SessionCompletedNotify.OnRecv, session as SessionHandler);
            session.Buffer.AddRange(bytes);
            do
            {
                if (session.Buffer.Count < 4)
                {
                    return;
                }

                byte[] lenBytes   = session.Buffer.GetRange(0, 4).ToArray();
                int    packageLen = BitConverter.ToInt32(lenBytes, 0);

                //缓冲区越界判断
                if (packageLen > (1024 * 1024 * 2) || packageLen < 1)
                {
                    this.ConsoleWriteLine("DEBUG BUFFER OutOfRange !! ProcessPackage DATA", ConsoleColor.Red);
                    this._manager_session.Close(true);
                    return;
                }

                if (packageLen > session.Buffer.Count - 4)
                {
                    return;
                }

                byte[] completeBytes = session.Buffer.GetRange(4, packageLen).ToArray();

                session._completedBuffer = CompressHelper.Decompress(completeBytes);

                this._onSessionNotifyProc(SessionCompletedNotify.OnReceived, session as SessionHandler);

                session.Buffer.RemoveRange(0, packageLen + 4);
            } while (session.Buffer.Count > 4);
        }
コード例 #5
0
        private void CreateSession(byte[] data)
        {
            byte[] body = new byte[data.Length - 1];
            Array.Copy(data, 1, body, 0, body.Length);

            //byte[] sessionIds = new byte[body.Length * 2];
            List <byte> buffer = new List <byte>();

            byte[] sessionId = new byte[sizeof(Int64) * 2];

            List <TcpProxySessionBased> sessionList = new List <TcpProxySessionBased>();

            for (int i = 0; i < body.Length / sizeof(Int64); i++)
            {
                Array.Copy(body, i * sizeof(Int64), sessionId, 0, sizeof(Int64));
                TcpProxySessionBased sessionBased = new TcpProxySessionBased(_manager_session);
                sessionBased.RemoteId = BitConverter.ToInt64(sessionId, 0);

                Console.WriteLine("CreateSession:" + sessionBased.RemoteId + " Id:" + sessionBased.Id);

                sessionList.Add(sessionBased);

                BitConverter.GetBytes(sessionBased.Id).CopyTo(sessionId, sizeof(Int64));
                buffer.AddRange(sessionId);
                //Array.Copy(sessionId, 0, sessionIds, i * (sizeof(Int64) + sizeof(Int64)), sizeof(Int64) + sizeof(Int64));
            }

            SendMessageHelper.SendMessage(_manager_session, MsgCommand.Msg_Set_Session_Id, buffer.ToArray());

            this._sessionList.AddRange(sessionList.ToArray());

            foreach (var session in sessionList)
            {
                this._onSessionNotifyProc?.Invoke(SessionCompletedNotify.OnConnected, session as SessionHandler);
            }

            sessionList.Clear();
        }