コード例 #1
0
ファイル: SocketClient.cs プロジェクト: fr830/UcAsp.RPC
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        /// <param name="channel"></param>
        /// <returns></returns>
        ///
        public override void Call(object dataev, int i)
        {
            DataEventArgs e = (DataEventArgs)dataev;

            try
            {
                TaskTicks time = new TaskTicks();
                if (RunTime.TryGetValue(e.TaskId, out time))
                {
                    time.IntoQueTime = DateTime.Now.Ticks;
                    RunTime.TryUpdate(e.TaskId, time, time);
                }
            }
            catch (Exception ex)
            { Console.WriteLine(ex); }
            Stopwatch watch = new Stopwatch();

            watch.Start();

            ChannelPool channel = Channels[i];

            try
            {
                e.CallHashCode = i;
                Socket _client = channel.Client;
                if (_client == null || _client.Connected != true)
                {
                    channel.Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    channel.Client.Connect(channel.IpPoint);
                    channel.Available = true;
                }
                _client = channel.Client;

                byte[] _bf = e.ToByteArray();
                #region 異步
                _client.BeginSend(_bf, 0, _bf.Length, SocketFlags.None, null, null);
                #endregion
                #region 異步接收
                StateObject state = new StateObject();
                RunStateObjects.TryAdd(e.TaskId, state);
                state.WorkSocket = _client;
                state.Builder.ReSet();
                SocketError error;
                _client.BeginReceive(state.Buffer, 0, StateObject.BufferSize, SocketFlags.None, out error, ReceiveCallback, state);
                #endregion
                watch.Stop();
                //monitor.Write(e.TaskId, e.ActionCmd, "...", watch.ElapsedMilliseconds, _bf.LongLength.ToString());
                Channels[i].PingActives = DateTime.Now.Ticks;
            }
            catch (SocketException sex)
            {
                foreach (ChannelPool p in Channels)
                {
                    if (p.IpPoint == channel.IpPoint)
                    {
                        p.Available = false;
                    }
                }
                e.StatusCode = StatusCode.Error;
                lock (ResultTask)
                {
                    e.StatusCode = StatusCode.Serious;
                    DataEventArgs timout = _timeoutTask.FirstOrDefault(o => o.TaskId == e.TaskId);
                    if (timout == null)
                    {
                        ResultTask.AddOrUpdate(e.TaskId, e, (key, value) => value = e);
                    }
                    else
                    {
                        _timeoutTask.Remove(timout);
                    }
                }
                _log.Error(sex);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                lock (ResultTask)
                {
                    e.StatusCode = StatusCode.Serious;
                    DataEventArgs timout = _timeoutTask.FirstOrDefault(o => o.TaskId == e.TaskId);
                    if (timout == null)
                    {
                        ResultTask.AddOrUpdate(e.TaskId, e, (key, value) => value = e);
                    }
                    else
                    {
                        _timeoutTask.Remove(timout);
                    }
                }
                _log.Error(ex);
            }
        }