Exemplo n.º 1
0
        public override Dynamic.Net.Base.INetCommand GetCommand(INetSession session, System.IO.Stream stream)
        {
            INetCommand  command = null;
            StreamReader sr      = new StreamReader(stream);
            string       header  = sr.ReadToEnd();

            if (header.StartsWith("GET ", StringComparison.OrdinalIgnoreCase))
            {
                HandshakeRequestCommand handshakeCmd = new HandshakeRequestCommand();
                if (handshakeCmd.Parse(header))
                {
                    command = handshakeCmd;
                }
            }
            else
            {
                WebSocketSession wSession = session as WebSocketSession;
                if (wSession != null && wSession.IsHandShake)
                {
                }
                else
                {
                    session.Close();
                }
            }
            return(command);
        }
Exemplo n.º 2
0
 public void Push(INetSession session)
 {
     lock (this._typeToObjects)
     {
         this._typeToObjects[session.GetType()].Enqueue(session);
     }
 }
Exemplo n.º 3
0
        /// <summary>添加会话。子类可以在添加会话前对会话进行一些处理</summary>
        /// <param name="session"></param>
        protected virtual void AddSession(INetSession session)
        {
            // 估算完成时间,锁争夺过大时提示
            using (var tc = new TimeCost("NetServer.AddSession", 100))
            {
                tc.Log = Log;

                var dic = Sessions;
                lock (dic)
                {
                    if (session.Host == null)
                    {
                        session.Host = this;
                    }
                    session.OnDisposed += (s, e) =>
                    {
                        var dic2 = Sessions;
                        lock (dic2)
                        {
                            dic2.Remove((s as INetSession).ID);
                        }
                    };
                    dic[session.ID] = session;
                }
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Initliaze this instance.
 /// </summary>
 public virtual void     Initliaze()
 {
     if (session == null)
     {
         session = new XNetTcpSession();
     }
 }
Exemplo n.º 5
0
        /// <summary>已重载。</summary>
        /// <param name="session"></param>
        /// <param name="stream"></param>
        protected override void OnReceive(INetSession session, Stream stream)
        {
            if (stream.Length == 0)
            {
                return;
            }

            var p = stream.Position;

            if (stream.Length > 100)
            {
                WriteLog("Echo {0} [{1}]", session.Remote, stream.Length);
            }
            else
            {
                WriteLog("Echo {0} [{1}] {2}", session.Remote, stream.Length, stream.ToStr());
            }

            //Send(e.Socket, e.Buffer, e.Offset, stream.Length, e.RemoteEndPoint);
            //session.Send(e.Buffer, e.Offset, stream.Length, e.RemoteEndPoint);
            //session.Send(e.Buffer, e.Offset, stream.Length);
            stream.Position = p;
            //session.Send(stream);
            session.Send(stream.ReadBytes());
        }
Exemplo n.º 6
0
        /// <summary>添加会话。子类可以在添加会话前对会话进行一些处理</summary>
        /// <param name="session"></param>
        protected virtual void AddSession(INetSession session)
        {
            // 估算完成时间,锁争夺过大时提示
            using (var tc = new TimeCost("NetServer.AddSession", 100))
            {
                tc.Log = Log;

                //var dic = Sessions;
                //lock (dic)
                //{
                if (session.Host == null)
                {
                    session.Host = this;
                }
                session.OnDisposed += (s, e) =>
                {
                    //var dic2 = Sessions;
                    //lock (dic2)
                    //{
                    //    dic2.Remove((s as INetSession).ID);
                    //}
                    var id = (s as INetSession).ID;
                    if (id > 0)
                    {
                        Sessions.Remove(id);
                    }
                };
                Sessions[session.ID] = session;
                //}
            }
        }
Exemplo n.º 7
0
    public static void AppNetSessionClosed(INetSession netSession, EVENTSESSIONPLACE eventPlace)
    {
        //if(msgStruct.dwPlayerName == 1)
        //{

        //}
    }
Exemplo n.º 8
0
 public void Clear()
 {
     this.type    = Type.Invalid;
     this.session = null;
     this.error   = string.Empty;
     this.data    = null;
 }
Exemplo n.º 9
0
        AppOnline CreateOnline(IApp app, INetSession ns, IDictionary <String, Object> ps)
        {
            var ip = ns.Remote.Host;

            var machine = ps["machine"] + "";
            var pid     = ps["processid"].ToInt();
            var ver     = ps["version"] + "";
            var compile = ps["compile"].ToDateTime();

            var online = GetOnline(app, ns);

            // 客户端特性
            online.Client  = $"{(ip.IsNullOrEmpty() ? machine : ip)}@{pid}";
            online.Name    = machine;
            online.Version = ver;
            online.Compile = compile;

            // 服务器特性
            pid           = Process.GetCurrentProcess().Id;
            online.Server = Local.EndPoint + "@" + pid;
            online.Save();

            // 真正的用户
            Session["AppOnline"] = online;

            // 下线
            ns.OnDisposed += (s, e) => online.Delete();

            //// 版本和编译时间
            //if (app.Version.IsNullOrEmpty() || app.Version.CompareTo(ver) < 0) app.Version = ver;
            //if (app.Compile.Year < 2000 || app.Compile < compile) app.Compile = compile;

            return(online);
        }
Exemplo n.º 10
0
        protected override TLSConnection CreateConnection(INetSession session, Socket acceptSocket)
        {
            TLSWSConnection wsConnection = ( TLSWSConnection )base.CreateConnection(session, acceptSocket);

            wsConnection.subProtocols = this._subProtocols;
            return(wsConnection);
        }
Exemplo n.º 11
0
        AppOnline CreateOnline(IApp app, INetSession ns, String machine, Int32 pid)
        {
            var ip = ns.Remote.Host;

            var online = GetOnline(app, ns);

            online.Client    = $"{(ip.IsNullOrEmpty() ? machine : ip)}@{pid}";
            online.Name      = machine;
            online.ProcessId = pid;
            online.UpdateIP  = ip;
            //online.Version = version;

            online.Server = Local + "";
            //online.Save();

            // 真正的用户
            Session["AppOnline"] = online;

            // 下线
            ns.OnDisposed += (s, e) =>
            {
                online.Delete();
                WriteHistory("下线", true, $"[{online.Name}]登录于{online.CreateTime},最后活跃于{online.UpdateTime}");
            };

            return(online);
        }
Exemplo n.º 12
0
        public INetSession netSession; // 接收消息的网络会话

        // 重置
        public void Reset()
        {
            msgCommand = 0;    // 消息命令标记
            msgOffset  = 0;    // 消息在缓冲中的起始位置
            msgLength  = 0;    // 消息的长度
            netSession = null; // 接收消息的网络会话
        }
Exemplo n.º 13
0
        /// <summary>已重载。</summary>
        /// <param name="session"></param>
        /// <param name="pk"></param>
        protected override void OnReceive(INetSession session, Packet pk)
        {
            var count = pk.Total;

            if (count == 0)
            {
                return;
            }

            //var p = pk.Position;
            if (count > 100)
            {
                WriteLog("Echo {0} [{1}]", session.Remote, count);
            }
            else
            {
                WriteLog("Echo {0} [{1}] {2}", session.Remote, count, pk.ToStr());
            }

            //Send(e.Socket, e.Buffer, e.Offset, stream.Length, e.RemoteEndPoint);
            //session.Send(e.Buffer, e.Offset, stream.Length, e.RemoteEndPoint);
            //session.Send(e.Buffer, e.Offset, stream.Length);
            //pk.Position = p;
            //session.Send(stream);
            session.Send(pk);
        }
        public virtual void WriteCommand(INetCommand command, INetSession session)
        {
            Byte[] rdata = null;
            if (command != null)
            {
                rdata = command.CommandName;
                if (rdata != null && rdata.Length > 0)
                {
                    session.WriteBytes(rdata, 0, rdata.Length);
                    WriteCommandNameEndBytes(command, session);
                }

                if (command.Parameters != null)
                {
                    foreach (Byte[] pData in command.Parameters)
                    {
                        if (pData != null && pData.Length > 0)
                        {
                            session.WriteBytes(pData, 0, pData.Length);
                        }
                        WriteCommandParameterSplitBytes(command, session);
                    }
                }
            }

            WriteFrameEndBytes(command, session);
        }
Exemplo n.º 15
0
        public virtual void SessionCreated(INetSession session)
        {
            session.SessionStarted -= session_SessionStarted;
            session.SessionStarted += new EventHandler(session_SessionStarted);

            session.SessionClosed -= session_SessionClosed;
            session.SessionClosed += new EventHandler(session_SessionClosed);
        }
Exemplo n.º 16
0
 public TCPConnector(INetSession session)
 {
     this.session        = session;
     this._connEventArgs = new SocketAsyncEventArgs {
         UserToken = this
     };
     this._connEventArgs.Completed += this.OnIOComplete;
 }
Exemplo n.º 17
0
        /// <summary>添加会话</summary>
        /// <param name="session"></param>
        protected override void AddSession(INetSession session)
        {
            if (session is ProxySession ss)
            {
                ss.Host = this;
            }

            base.AddSession(session);
        }
Exemplo n.º 18
0
        /// <summary>
        /// 监听外网消息
        /// </summary>
        /// <param name="portForCustomer"></param>
        /// <returns></returns>
        private NetServer MakeCustomerListenPort(int portForCustomer)
        {
            NetServer ForCustomerSer = new NetServer
            {
                Port       = portForCustomer,
                Log        = XTrace.Log,
                SessionLog = XTrace.Log,
                LogSend    = true,
                LogReceive = true
            };

            ForCustomerSer.Received += (s, e) =>
            {
                var customerInService = s as INetSession; //最后数据还要回发到此外网连接中。


                //制作消息
                Qi_NETFLY_Message message = new Qi_NETFLY_Message();
                message.MessageType           = MessageType.SERVER_TO_CLIENT_FOR_CUSTOMER;
                message.CustomerSession_MsgID = Guid.NewGuid().ToString(); //customerInService;

                SingList.Add(new Sing()
                {
                    CustomerSession_MsgID = message.CustomerSession_MsgID, CustomerSession = customerInService
                });

                //从端口号判定,应该发送到哪个客户端。
                INetSession clientSession = null;
                int         index         = GetClientSession_Index_From_PortsForListing(customerInService.Host.Local.Port, ref message);



                if (index == -1)
                {
                    clientSession = null;
                }
                else
                {
                    clientSession = PortsForListing[index].ServerForClient;
                }

                if (clientSession != null)
                {
                    //补充消息
                    message.CustomerData = e.Packet.Data;
                    string json = Newtonsoft.Json.JsonConvert.SerializeObject(message);

                    byte[] replyMessage = json.GetBytes();
                    clientSession.Send(replyMessage); // 转发过去给客户端
                }
            };
            ForCustomerSer.Start();

            return(ForCustomerSer);
        }
Exemplo n.º 19
0
        /// <summary>
        /// 发送消息到指定的session
        /// </summary>
        /// <param name="sessionID">session id</param>
        /// <param name="msg">消息</param>
        /// <param name="rpcEntry">RPC描述</param>
        /// <param name="transTarget">转发目标</param>
        /// <param name="nsid">转发的网络id</param>
        public bool Send(uint sessionID, IMessage msg, RPCEntry rpcEntry = null, Protos.MsgOpts.Types.TransTarget transTarget = Protos.MsgOpts.Types.TransTarget.Undefine, ulong nsid = 0u)
        {
            INetSession session = this.GetSession(sessionID);

            if (session == null)
            {
                return(false);
            }
            (( NetSessionBase )session).Send(msg, rpcEntry, transTarget, nsid);
            return(true);
        }
Exemplo n.º 20
0
        /// <summary>添加会话。子类可以在添加会话前对会话进行一些处理</summary>
        /// <param name="session"></param>
        protected override void AddSession(INetSession session)
        {
            var s = session as WebSession;

            if (s != null)
            {
                s.Server = this;
            }

            base.AddSession(session);
        }
Exemplo n.º 21
0
        public bool CloseSession(uint sessionID, string reason)
        {
            INetSession session = this.GetSession(sessionID);

            if (session == null)
            {
                return(false);
            }
            session.Close(true, reason);
            return(true);
        }
Exemplo n.º 22
0
 public static void AppNetSesssionConnected(INetSession netSession, EVENTSESSIONPLACE eventPlace, bool connectSuccess)
 {
     if (connectSuccess)
     {
         Console.WriteLine("success");
     }
     else
     {
         Console.WriteLine("failed");
     }
 }
Exemplo n.º 23
0
        /// <summary>
        /// 发送数据到指定sesion
        /// </summary>
        /// <param name="sessionID">session id</param>
        /// <param name="data">数据</param>
        /// <param name="offset">数据偏移量</param>
        /// <param name="size">数据长度</param>
        public bool Send(uint sessionID, byte[] data, int offset, int size)
        {
            INetSession session = this.GetSession(sessionID);

            if (session == null)
            {
                return(false);
            }
            session.Send(data, offset, size);
            return(true);
        }
Exemplo n.º 24
0
        /// <summary>添加会话。子类可以在添加会话前对会话进行一些处理</summary>
        /// <param name="session"></param>
        protected override void AddSession(INetSession session)
        {
            var ps = session as IProxySession;

            if (ps != null && ps.Proxy == null)
            {
                ps.Proxy = this;
            }

            base.AddSession(session);
        }
Exemplo n.º 25
0
        /// <summary>数据返回</summary>
        /// <param name="session"></param>
        /// <param name="stream"></param>
        protected override void OnReceive(INetSession session, Stream stream)
        {
            string sss = stream.ToStr();

            if (sss == "<policy-file-request/>\0")
            {
                session.Send(System.Text.Encoding.UTF8.GetBytes(_Policy.ToCharArray()));
            }
            session.Dispose();
            return;
        }
Exemplo n.º 26
0
 public void RemoveSession(INetSession session)
 {
     lock (SyncLocker)
     {
         if (session != null && SessionList.ContainsKey(session.SessionID))
         {
             SessionList.Remove(session.SessionID);
             session.SessionStarted -= session_SessionStarted;
             session.SessionClosed  -= session_SessionClosed;
         }
     }
 }
Exemplo n.º 27
0
 static public int Close(IntPtr l)
 {
     try {
         INetSession self = (INetSession)checkSelf(l);
         self.Close();
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Exemplo n.º 28
0
        void UpdateOnline(IApp app, JobTask ji, INetSession ns)
        {
            var online = GetOnline(app, ns);

            online.Total   += ji.Total;
            online.Success += ji.Success;
            online.Error   += ji.Error;
            online.Cost    += ji.Cost;
            online.Speed    = ji.Speed;
            online.LastKey  = ji.Key;
            online.SaveAsync();
        }
Exemplo n.º 29
0
        public override INetCommand Execute(INetSession session)
        {
            INetCommand cmd = new PongCommand();

            cmd.Encoding = session.Encoding;
            cmd.SetParameter(
                session.Encoding.GetBytes(
                    String.Format("Response:{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now)),
                0);

            return(cmd);
        }
Exemplo n.º 30
0
        /// <summary>添加会话。子类可以在添加会话前对会话进行一些处理</summary>
        /// <param name="session"></param>
        protected override void AddSession(INetSession session)
        {
            var ps = session as ProxySession;

            ps.RemoteServerUri = RemoteServer;
            // 如果不是Tcp/Udp,则使用本地协议
            if (!RemoteServer.IsTcp && !RemoteServer.IsUdp)
            {
                ps.RemoteServerUri.Type = Local.Type;
            }

            base.AddSession(session);
        }
Exemplo n.º 31
0
        /// <summary>实例化一个网络会话消息提供者</summary>
        /// <param name="session"></param>
        public SessionMessageProvider(INetSession session)
        {
            Session = session;

            session.Received += new EventHandler<ReceivedEventArgs>(client_Received);
        }