Пример #1
0
        // 发送请求数据包并得到响应包
        //requestPackage:请求数据包
        //timeoutSeconds:超时时间(单位:秒),如果超时时间小于等于0,则超时时间为无限大
        public ResponsePackage SendRequest(RequestPackage requestPackage, int timeoutSeconds)
        {
            String requestId = requestPackage.RequestId;

            //发送Request包
            if (!XiepIoHelper.SendPackage(_networkStream, requestPackage))
            {
                //发送失败
                return(null);
            }

            lock (_mapRequestResponse)
            {
                _mapRequestResponse.Add(requestId, null);
            }

            DateTime        startWaitResponseTime = DateTime.Now;
            ResponsePackage responsePackage       = null;

            while (true)
            {
                lock (_mapRequestResponse)
                {
                    responsePackage = _mapRequestResponse[requestId];
                }
                long usedSeconds = Convert.ToInt64((DateTime.Now - startWaitResponseTime).TotalSeconds);
                if ( //如果已经得到响应包
                    responsePackage != null
                    //或者等待已经超时
                    || usedSeconds > timeoutSeconds)
                {
                    break;
                }
                Thread.Sleep(100);
            }
            //从等待字典中移除
            lock (_mapRequestResponse)
            {
                if (_mapRequestResponse.ContainsKey(requestId))
                {
                    _mapRequestResponse.Remove(requestId);
                }
            }
            return(responsePackage);
        }
Пример #2
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());
                }
            }
        }
Пример #3
0
 // 发送事件包
 public Boolean SendEvent(NetworkStream os, EventPackage eventPackage)
 {
     return(XiepIoHelper.SendPackage(os, eventPackage));
 }