Exemplo n.º 1
0
        public override ResponsePackage execute(ClientConnectionInfoArgs clientConnectionInfoArgs, RequestPackage requestPackage)
        {
            ResponsePackage responsePackage = new ResponsePackage();

            responsePackage.Response = "XiepPong";
            return(responsePackage);
        }
Exemplo n.º 2
0
 void xsServer_ClientDisconnected(object sender, ClientConnectionInfoArgs e)
 {
     PushLog(e.getInetAddress().ToString() + "已断开与服务器的连接");
 }
Exemplo n.º 3
0
        private void receiveRequestThreadFunction(Object obj)
        {
            Socket socket = (Socket)obj;

            IPEndPoint ipep = (IPEndPoint)socket.RemoteEndPoint;
            //连接的IP地址
            IPAddress remoteInetAddress = ipep.Address;
            int       remotePort        = ipep.Port;

            //客户端连接信息参数对象
            ClientConnectionInfoArgs clientConnectionInfoArgs = new ClientConnectionInfoArgs(socket, remoteInetAddress, remotePort);

            //是否在发送完响应后断开连接
            Boolean isDisconnectWhenSendResponseFinish = false;

            pushLog(String.Format("{0}:{1} 连接到服务器。", remoteInetAddress, remotePort));
            try
            {
                //设置接受超时时间
                socket.ReceiveTimeout = _heartBeatTimeout * 1000;
                NetworkStream networkStream = new NetworkStream(socket);

                ClientConnectedArgs clientConnectedArgs = new ClientConnectedArgs(clientConnectionInfoArgs);
                //触发客户端连接事件
                if (this.ClientConnected != null)
                {
                    ClientConnected.Invoke(this, clientConnectedArgs);
                }

                if (!clientConnectedArgs.getIsAccept())
                {
                    throw new Exception("此连接不被接受,已断开。");
                }
                if (clientConnectedArgs.getEventPackage() != null)
                {
                    XiepIoHelper.SendPackage(networkStream, clientConnectedArgs.getEventPackage());
                }
                while (true)
                {
                    //接收请求包的XML字符串
                    String xml = XiepIoHelper.ReceiveXml(networkStream);
                    if (xml == null)
                    {
                        throw new IOException("获取数据包字符串失败。");
                    }
                    //请求包
                    RequestPackage requestPackage = RequestPackage.fromXml(xml);
                    if (requestPackage == null)
                    {
                        throw new IOException("字符串转换为请求数据包时失败。");
                    }
                    //响应包
                    ResponsePackage responsePackage = null;

                    String requestName = requestPackage.Request;
                    //如果找到了对应的处理器
                    if (this._requestHandlerMap.ContainsKey(requestName))
                    {
                        responsePackage = this._requestHandlerMap[requestName].execute(clientConnectionInfoArgs, requestPackage);
                    }
                    //如果是客户端发来的心跳消息包
                    if (requestPackage.Request.Equals("XiepPing"))
                    {
                    }
                    else
                    {
                        ReceiveRequestArgs receiveRequestArgs = new ReceiveRequestArgs(clientConnectionInfoArgs, requestPackage);
                        receiveRequestArgs.setResponsePackage(responsePackage);
                        //触发接收到客户端请求事件
                        if (ReceiveRequest != null)
                        {
                            ReceiveRequest.Invoke(this, receiveRequestArgs);
                        }

                        responsePackage = receiveRequestArgs.getResponsePackage();
                        isDisconnectWhenSendResponseFinish = receiveRequestArgs.getIsDisconnectWhenSendResponseFinish();

                        //发送响应包
                        if (responsePackage != null)
                        {
                            //为响应数据包添加RequestId
                            responsePackage.RequestId = requestPackage.RequestId;

                            //发送响应包
                            if (!XiepIoHelper.SendPackage(networkStream, responsePackage))
                            {
                                pushLog("错误:发送响应包失败:" + remoteInetAddress + "   |   " + responsePackage);
                            }
                            //触发发送响应后事件
                            AfterSendResponseArgs afterSendResponseArgs = new AfterSendResponseArgs(clientConnectionInfoArgs, requestPackage, responsePackage);
                            if (AfterSendResponse != null)
                            {
                                AfterSendResponse.Invoke(this, afterSendResponseArgs);
                            }

                            if (isDisconnectWhenSendResponseFinish)
                            {
                                return;
                            }
                        }
                    }
                }
            }
            catch { }
            finally
            {
                //触发与客户端连接断开事件
                if (ClientDisconnected != null)
                {
                    ClientDisconnected.Invoke(this, clientConnectionInfoArgs);
                }

                lock (_connectedClientList)
                {
                    _connectedClientList.Remove(socket);
                }
                try
                {
                    socket.Close();
                }
                catch (Exception ex)
                {
                    pushLog("关闭Socket时异常:" + ex.ToString());
                }
            }
        }
Exemplo n.º 4
0
 public abstract ResponsePackage execute(ClientConnectionInfoArgs clientConnectionInfoArgs, RequestPackage requestPackage);