Ejemplo n.º 1
0
        /// <summary>
        /// 接收指令
        /// </summary>
        /// <param name="socket"></param>
        /// <returns>RequestCommandsEnum.Error 连接自动中断</returns>
        protected RequestCommandsEnum ReceiveCommand(Socket socket)
        {
            RequestCommandsEnum command = RequestCommandsEnum.Default;
            int readCount = 0;

            try
            {
                readCount = socket.Receive(Buffer, RequestConstants.SocketCommandLength, 0);
            }
            catch
            {
                command = RequestCommandsEnum.Error;
            }
            if (readCount == 0)
            {
                CloseConnection(socket);
            }
            else
            {
                string text = System.Text.Encoding.Default.GetString(Buffer, 0, 1);
                if (!Enum.TryParse <RequestCommandsEnum>(text, out command))
                {
                    Log.WriteLog("指令错误:", text);
                    CloseConnection(socket);
                }
            }
            return(command);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 发送指令及数据
 /// </summary>
 /// <param name="socket"></param>
 /// <param name="command"></param>
 /// <param name="text"></param>
 /// <returns></returns>
 protected int SendCommandText(Socket socket, RequestCommandsEnum command, string text = "")
 {
     if (CheckConnection(socket))
     {
         byte[] buffer = System.Text.Encoding.Default.GetBytes(((int)command).ToString() + text);
         return(socket.Send(buffer, buffer.Length, 0));
     }
     else
     {
         CloseConnection(socket);
         return(-1);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 启用拨号
        /// </summary>
        public void Call(string targetName)
        {
            //主拨检查
            int result = IsOtherCallExist(targetName);

            if (result == -1)
            {
                //当前拨号已存在
                OnTaskExisted();
                return;
            }
            else if (result == 1)
            {
                Log.WriteLog("已存在主拨请求");
                OnConfirmCallWhileHandling(targetName);
                //检查任务是否已取消
                while (TargetClient != null)
                {
                    //检查任务是否可以继续执行
                    if (CLPop(targetName + TaskParam._Submit))
                    {
                        //清理其他任务
                        ClearOtherCall();
                        //检查其他任务是否已清理
                        int timeS = 1;
                        int timeE = 4;
                        while (timeS < timeE)
                        {
                            Thread.Sleep(500);
                            if (IsOtherCallExist(targetName) == 0)
                            {
                                break;
                            }
                            timeS++;
                            if (timeS == timeE)
                            {
                                OnTaskClearFailed();
                                return;
                            }
                        }
                        break;
                    }
                    else if (CLPop(targetName + TaskParam._Cancel))
                    {
                        return;
                    }
                    else
                    {
                        Thread.Sleep(200);
                    }
                }
            }
            //请求服务器
            Socket server = Connect(ServerHost, ServerPort);

            if (!CheckConnection(server))
            {
                return;//连接失败错误
            }
            //主动拨号请求
            TargetClient = server;
            TargetName   = targetName;
            Log.WriteLog("客户{0}启用拨号对象:{1}", Name, targetName);
            string text = string.Format(RequestConstants.CallPattern, Name, targetName);

            SendCommandText(TargetClient, RequestCommandsEnum.CallCommand, text);

            //非阻塞式
            while (CheckConnection(TargetClient))
            {
                //接收请求反馈
                RequestCommandsEnum command = ReceiveCommand(TargetClient);
                switch (command)
                {
                //默认回复Default
                case RequestCommandsEnum.Default:
                    if (CLPop(targetName + TaskParam._Cancel))
                    {
                        Log.WriteLog("主动中断通话请求");
                        OnCallCanceled(targetName);
                        CloseTargetCall();
                        return;
                    }
                    else
                    {
                        SendCommandText(TargetClient, RequestCommandsEnum.Default);
                        break;
                    }

                case RequestCommandsEnum.RejectCommand:
                    Log.WriteLog("对方拒绝通话请求", Name);
                    OnCallRejected(targetName);
                    CloseTargetCall();
                    return;

                case RequestCommandsEnum.AcceptCommand:
                    Log.WriteLog("对方接收通话请求", Name);
                    OnCallAccepted(targetName);
                    StartAudio(TargetClient, targetName, true);
                    return;

                case RequestCommandsEnum.Error:
                case RequestCommandsEnum.CallCommand:
                case RequestCommandsEnum.CancelCommand:
                    Log.WriteLog("信号异常", Name);
                    OnCallRejected(targetName);
                    //OnCallError(targetName);
                    CloseTargetCall();
                    return;
                }
            }

            //阻塞式
            ////接收请求反馈
            //RequestCommandsEnum command = ReceiveCommand(server);
            //switch (command)
            //{
            //    case RequestCommandsEnum.CancelCommand:
            //        CloseConnection(server,true);
            //        //OnCallCanceled(targetName);
            //        CallingClient = null;
            //        break;
            //    case RequestCommandsEnum.Default:
            //    case RequestCommandsEnum.Error:
            //    case RequestCommandsEnum.CallCommand:
            //    case RequestCommandsEnum.SignOnCommand:
            //        Log.WriteLog("通话请求反馈内容错误", Name);
            //        CloseConnection(server,true);
            //        OnCallErrored(targetName);
            //        CallingClient = null;
            //        break;
            //    case RequestCommandsEnum.RejectCommand:
            //        Log.WriteLog("对方拒接通话请求", Name);
            //        CloseConnection(server, true);
            //        OnCallRejected(targetName);
            //        CallingClient = null;
            //        break;
            //    case RequestCommandsEnum.AcceptCommand:
            //        Log.WriteLog("对方接收通话请求", Name);
            //        StartAudio(server, targetName,true);
            //        OnCallAccepted(targetName);
            //        CallingClient = null;
            //        break;
            //}
        }