Esempio n. 1
0
        /// <summary>
        /// 客户离开了
        /// </summary>
        /// <param name="dwConnId"></param>
        /// <returns></returns>
        protected virtual HandleResult OnServerClose(IntPtr dwConnId)
        {
            // 获取附加数据
            IntPtr extraPtr = IntPtr.Zero;

            if (server.GetConnectionExtra(dwConnId, ref extraPtr) == false)
            {
                return(HandleResult.Error);
            }

            // extra 就是accept里传入的附加数据了
            ConnExtraData extra = (ConnExtraData)Marshal.PtrToStructure(extraPtr, typeof(ConnExtraData));

            if (extra.FreeType == 0)
            {
                // 由client(N)断开连接,释放agent数据
                agent.Disconnect(extra.ConnIdForClient);
                agent.SetConnectionExtra(extra.ConnIdForClient, null);
            }

            server.SetConnectionExtra(dwConnId, null);

            AddMsg(string.Format(" > [{0},OnServerClose]", dwConnId));
            return(HandleResult.Ok);
        }
Esempio n. 2
0
        /// <summary>
        /// 连接关闭了
        /// </summary>
        /// <param name="dwConnId"></param>
        /// <returns></returns>
        protected virtual HandleResult OnAgentClose(IntPtr dwConnId)
        {
            AddMsg(string.Format(" > [{0},OnAgentClose]", dwConnId));

            // 获取附加数据
            IntPtr extraPtr = IntPtr.Zero;

            if (agent.GetConnectionExtra(dwConnId, ref extraPtr) == false)
            {
                return(HandleResult.Error);
            }

            ConnExtraData extra = (ConnExtraData)Marshal.PtrToStructure(extraPtr, typeof(ConnExtraData));

            agent.SetConnectionExtra(dwConnId, null);

            if (extra.FreeType == 0)
            {
                // 由Target断开连接,释放server连接
                extra.FreeType = 1;
                server.SetConnectionExtra(extra.ConnIdForServer, extra);
                extra.Server.Disconnect(extra.ConnIdForServer);
            }


            return(HandleResult.Ok);
        }
Esempio n. 3
0
        /// <summary>
        /// 数据到达了
        /// </summary>
        /// <param name="dwConnId"></param>
        /// <param name="pData"></param>
        /// <param name="iLength"></param>
        /// <returns></returns>
        protected virtual HandleResult OnServerReceive(IntPtr dwConnId, IntPtr pData, int iLength)
        {
            try
            {
                // 获取附加数据
                IntPtr extraPtr = IntPtr.Zero;

                if (server.GetConnectionExtra(dwConnId, ref extraPtr) == false)
                {
                    return(HandleResult.Error);
                }

                // extra 就是accept里传入的附加数据了
                ConnExtraData extra = (ConnExtraData)Marshal.PtrToStructure(extraPtr, typeof(ConnExtraData));

                AddMsg(string.Format(" > [Server->OnServerReceive] -> ({0} bytes)", iLength));

                // 服务端收到数据了,应该调用agent发送到顶层服务器,实现 client(N)->server->targetServer 的中转
                if (agent.Send(extra.ConnIdForClient, pData, iLength) == false)
                {
                    return(HandleResult.Error);
                }

                return(HandleResult.Ok);
            }
            catch (Exception)
            {
                return(HandleResult.Error);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 出错了
        /// </summary>
        /// <param name="connId"></param>
        /// <param name="enOperation"></param>
        /// <param name="errorCode"></param>
        /// <returns></returns>
        protected virtual HandleResult OnServerClose(IntPtr connId, SocketOperation enOperation, int errorCode)
        {
            if (errorCode == 0)
            {
                AddMsg(string.Format(" > [{0},OnServerClose]", connId));
            }
            else
            {
                AddMsg(string.Format(" > [{0},OnServerError] -> OP:{1},CODE:{2}", connId, enOperation, errorCode));
            }
            // return HPSocketSdk.HandleResult.Ok;

            // 获取附加数据
            IntPtr extraPtr = IntPtr.Zero;

            if (server.GetConnectionExtra(connId, ref extraPtr) == false)
            {
                return(HandleResult.Error);
            }

            // extra 就是accept里传入的附加数据了
            ConnExtraData extra = (ConnExtraData)Marshal.PtrToStructure(extraPtr, typeof(ConnExtraData));

            if (extra.FreeType == 0)
            {
                // 由client(N)断开连接,释放agent数据
                agent.Disconnect(extra.ConnIdForClient);
                agent.SetConnectionExtra(extra.ConnIdForClient, null);
            }

            server.SetConnectionExtra(connId, null);

            return(HandleResult.Ok);
        }
Esempio n. 5
0
        /// <summary>
        /// 出错了
        /// </summary>
        /// <param name="connId"></param>
        /// <param name="enOperation"></param>
        /// <param name="errorCode"></param>
        /// <returns></returns>
        protected virtual HandleResult OnAgentClose(IntPtr connId, SocketOperation enOperation, int errorCode)
        {
            if (errorCode == 0)
            {
                AddMsg(string.Format(" > [{0},OnAgentClose]", connId));
            }
            else
            {
                AddMsg(string.Format(" > [{0},OnAgentError] -> OP:{1},CODE:{2}", connId, enOperation, errorCode));
            }
            // return HPSocketSdk.HandleResult.Ok;

            // 获取附加数据
            IntPtr extraPtr = IntPtr.Zero;

            if (agent.GetConnectionExtra(connId, ref extraPtr) == false)
            {
                return(HandleResult.Error);
            }

            ConnExtraData extra = (ConnExtraData)Marshal.PtrToStructure(extraPtr, typeof(ConnExtraData));

            agent.SetConnectionExtra(connId, null);

            if (extra.FreeType == 0)
            {
                // 由Target断开连接,释放server连接
                extra.FreeType = 1;
                server.SetConnectionExtra(extra.ConnIdForServer, extra);
                extra.Server.Disconnect(extra.ConnIdForServer);
            }

            return(HandleResult.Ok);
        }
Esempio n. 6
0
        /// <summary>
        /// 客户进入
        /// </summary>
        /// <param name="dwConnId"></param>
        /// <param name="pClient"></param>
        /// <returns></returns>
        protected virtual HandleResult OnServerAccept(IntPtr dwConnId, IntPtr pClient)
        {
            // 获取客户端ip和端口
            string ip   = string.Empty;
            ushort port = 0;

            if (server.GetRemoteAddress(dwConnId, ref ip, ref port))
            {
                AddMsg(string.Format(" > [{0},OnServerAccept] -> PASS({1}:{2})", dwConnId, ip.ToString(), port));
            }
            else
            {
                AddMsg(string.Format(" > [{0},OnServerAccept] -> Server_GetClientAddress() Error", dwConnId));
                return(HandleResult.Error);
            }

            IntPtr clientConnId = IntPtr.Zero;

            // 一次不成功的事偶尔可能发生,三次连接都不成功,那就真连不上了
            // 当server有连接进入,使用agent连接到目标服务器
            if (agent.Connect(TargetAddr, TargetPort, ref clientConnId) == false)
            {
                if (agent.Connect(TargetAddr, TargetPort, ref clientConnId) == false)
                {
                    if (agent.Connect(TargetAddr, TargetPort, ref clientConnId) == false)
                    {
                        AddMsg(string.Format(" > [Client->Connect] fail -> ID:{0}", clientConnId));
                        return(HandleResult.Error);
                    }
                }
            }


            // 设置附加数据
            ConnExtraData extra = new ConnExtraData();

            extra.ConnIdForServer = dwConnId;
            extra.ConnIdForClient = clientConnId;
            extra.Server          = server;
            extra.FreeType        = 0;
            if (server.SetConnectionExtra(dwConnId, extra) == false)
            {
                AddMsg(string.Format(" > [{0},OnServerAccept] -> server.SetConnectionExtra fail", dwConnId));
                return(HandleResult.Error);
            }

            if (agent.SetConnectionExtra(clientConnId, extra) == false)
            {
                server.SetConnectionExtra(dwConnId, null);
                AddMsg(string.Format(" > [{0}-{1},OnServerAccept] -> agent.SetConnectionExtra fail", dwConnId, clientConnId));
                return(HandleResult.Error);
            }

            return(HandleResult.Ok);
        }
Esempio n. 7
0
        /// <summary>
        /// 数据到达了
        /// </summary>
        /// <param name="dwConnId"></param>
        /// <param name="pData"></param>
        /// <param name="iLength"></param>
        /// <returns></returns>
        protected virtual HandleResult OnAgentReceive(IntPtr dwConnId, IntPtr pData, int iLength)
        {
            // 获取附加数据
            IntPtr extraPtr = IntPtr.Zero;

            if (agent.GetConnectionExtra(dwConnId, ref extraPtr) == false)
            {
                return(HandleResult.Error);
            }

            ConnExtraData extra = (ConnExtraData)Marshal.PtrToStructure(extraPtr, typeof(ConnExtraData));

            AddMsg(string.Format(" > [{0},OnAgentReceive] -> ({1} bytes)", dwConnId, iLength));
            if (extra.Server.Send(extra.ConnIdForServer, pData, iLength) == false)
            {
                return(HandleResult.Error);
            }

            return(HandleResult.Ok);
        }
Esempio n. 8
0
        /// <summary>
        /// 客户进入
        /// </summary>
        /// <param name="connId"></param>
        /// <param name="pClient"></param>
        /// <returns></returns>
        protected virtual HandleResult OnServerAccept(IntPtr connId, IntPtr pClient)
        {
            // 获取客户端ip和端口
            string ip = string.Empty;
            ushort port = 0;
            if (server.GetRemoteAddress(connId, ref ip, ref port))
            {
                AddMsg(string.Format(" > [{0},OnServerAccept] -> PASS({1}:{2})", connId, ip.ToString(), port));
            }
            else
            {
                AddMsg(string.Format(" > [{0},OnServerAccept] -> Server_GetClientAddress() Error", connId));
                return HandleResult.Error;
            }

            IntPtr clientConnId = IntPtr.Zero;

            // 一次不成功的事偶尔可能发生,三次连接都不成功,那就真连不上了
            // 当server有连接进入,使用agent连接到目标服务器
            if (agent.Connect(TargetAddr, TargetPort, ref clientConnId) == false)
            {
                if (agent.Connect(TargetAddr, TargetPort, ref clientConnId) == false)
                {
                    if (agent.Connect(TargetAddr, TargetPort, ref clientConnId) == false)
                    {
                        AddMsg(string.Format(" > [Client->Connect] fail -> ID:{0}", clientConnId));
                        return HandleResult.Error;
                    }
                }
            }


            // 设置附加数据
            ConnExtraData extra = new ConnExtraData();
            extra.ConnIdForServer = connId;
            extra.ConnIdForClient = clientConnId;
            extra.Server = server;
            extra.FreeType = 0;
            if (server.SetConnectionExtra(connId, extra) == false)
            {
                AddMsg(string.Format(" > [{0},OnServerAccept] -> server.SetConnectionExtra fail", connId));
                return HandleResult.Error;
            }

            if (agent.SetConnectionExtra(clientConnId, extra) == false)
            {
                server.SetConnectionExtra(connId, null);
                AddMsg(string.Format(" > [{0}-{1},OnServerAccept] -> agent.SetConnectionExtra fail", connId, clientConnId));
                return HandleResult.Error;
            }

            return HandleResult.Ok;
        }