示例#1
0
        //获取客户端信息列表中指定远程端点的项
        private AsyncSocketClientMsg GetClientMsg(List <AsyncSocketClientMsg> clientMsg, EndPoint endpoint)
        {
            AsyncSocketClientMsg cm = null;

            foreach (var item in clientMsg)
            {
                if (item.remote.ToString() == endpoint.ToString())
                {
                    cm = item;
                    break;
                }
            }
            return(cm);
        }
示例#2
0
        private void ReceiveDataAsync(IAsyncResult Idata)
        {
            int recv = _serverSock.EndReceiveFrom(Idata, ref so.remote);

            if (!isLegal(so.remote))
            {
                so.ClearBuffer();
                _serverSock.BeginReceiveFrom(so.buffer, 0, so.buffer.Length, SocketFlags.None, ref so.remote, new AsyncCallback(ReceiveDataAsync), so.remote);
                return;
            }

            string      receiveData = Encoding.ASCII.GetString(so.buffer, 0, recv);
            DataAnalyze op          = new DataAnalyze();
            string      result      = op.DataAnalyzeResult(receiveData);//获取数据库操作结果

            if (op.GetSqlOrder() != null)
            {
                AsyncSocketClientMsg sendMeg = new AsyncSocketClientMsg(so.remote, result);
                //发送第一个子数据报
                byte[] resultByte = Encoding.ASCII.GetBytes(result);
                byte[] sendData   = new byte[1024];
                int    copylen    = (resultByte.Length > 1020) ? 1020 : resultByte.Length;
                Buffer.BlockCopy(resultByte, 0, sendData, 4, copylen);
                sendData[0] = (byte)(sendMeg.DgramNum / 256);
                sendData[1] = (byte)(sendMeg.DgramNum % 256);
                sendData[2] = 0x00;
                sendData[3] = 0x01;
                _serverSock.BeginSendTo(sendData, 0, sendData.Length, SocketFlags.None, so.remote, new AsyncCallback(SendData), so.remote);

                lock (clientMsg)
                {
                    clientMsg.Remove(GetClientMsg(clientMsg, so.remote));//删除重复请求客户端信息
                    clientMsg.Add(sendMeg);
                }
            }

            else
            {
                int senOrder = GetDgramOrder(so.buffer);
                AsyncSocketClientMsg recClient = GetClientMsg(clientMsg, so.remote);
                if (recClient != null)
                {
                    if (recClient.DgramNum <= senOrder)//检测是否发送完成:发送完成
                    {
                        lock (clientMsg)
                        {
                            clientMsg.Remove(recClient);
                        }
                    }
                    else//发送未完成
                    {
                        byte[] resultByte = Encoding.ASCII.GetBytes(recClient.sendMeg);
                        byte[] sendData   = new byte[1024];
                        int    copyLength = (recClient.DgramNum == (senOrder + 1)) ? resultByte.Length % 1020 : 1020;//每次发送的字节数
                        Buffer.BlockCopy(resultByte, senOrder * 1020, sendData, 4, copyLength);
                        sendData[0] = (byte)(recClient.DgramNum / 256);
                        sendData[1] = (byte)(recClient.DgramNum % 256);
                        sendData[2] = (byte)((senOrder + 1) / 256);
                        sendData[3] = (byte)((senOrder + 1) % 256);
                        _serverSock.BeginSendTo(sendData, 0, sendData.Length, SocketFlags.None, so.remote, new AsyncCallback(SendData), so.remote);
                    }
                }
            }

            so.ClearBuffer();
            _serverSock.BeginReceiveFrom(so.buffer, 0, so.buffer.Length, SocketFlags.None, ref so.remote, new AsyncCallback(ReceiveDataAsync), so.remote);
        }