示例#1
0
        private void ReadCallback(IAsyncResult ar)
        {
            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            var state   = (StateObject)ar.AsyncState;
            var handler = state.WorkSocket;

            int bytesRead;

            try
            {
                // Read data from the client socket.
                bytesRead = handler.EndReceive(ar);
            }
            catch (SocketException)
            {
                ClearLine();
                RemovePhone(_phoneNumber);
                return;
            }
            catch (ObjectDisposedException)
            {
                ClearLine();
                RemovePhone(_phoneNumber);
                return;
            }

            if (bytesRead > 0)
            {
                // There  might be more data, so store the data received so far.
                state.Sb.Append(Encoding.ASCII.GetString(
                                    state.Buffer, 0, bytesRead));

                // Check for end-of-file tag. If it is not there, read
                // more data.
                var content = state.Sb.ToString();
                var index   = content.IndexOf("<EOF>", StringComparison.Ordinal);
                if (index != -1)
                {
                    state.Sb = new StringBuilder();
                    string command = content.Substring(0, index);
                    if (command == "disconnect")
                    {
                        ClearLine();
                        handler.Shutdown(SocketShutdown.Both);
                        handler.Disconnect(false);
                        RemovePhone(_phoneNumber);
                        return;
                    }
                    if (command.StartsWith("dial,"))
                    {
                        var parts      = command.Split(new[] { ',' });
                        var lineNumber = int.Parse(parts[1]);
                        // line should equal null the first dial
                        if (_line == null)
                        {
                            _line = (SimulatorLine)(new Simulator().GetLine(lineNumber));
                            _line.SubscribeToHangup(HandleHangup);
                        }
                        switch (_line.Status)
                        {
                        case LineStatusTypes.OnHook:
                            Send("idle");
                            _line.UnsubscribeToHangup(HandleHangup);
                            _line = null;
                            break;

                        case LineStatusTypes.Connected:
                            Send("busy");
                            _line.UnsubscribeToHangup(HandleHangup);
                            _line = null;
                            break;

                        case LineStatusTypes.AcceptingCalls:
                            var status = _line.SendRing();
                            switch (status)
                            {
                            case LineStatusTypes.AcceptingCalls:
                                Send("ring");
                                break;

                            case LineStatusTypes.Connected:
                                Send("answered");
                                break;
                            }
                            break;
                        }
                    }
                    else if (command == "hangup")
                    {
                        ClearLine();
                    }
                    else if (command.StartsWith("dialoutResponse,"))
                    {
                        var parts      = command.Split(new[] { ',' });
                        var subCommand = parts[1];
                        switch (subCommand)
                        {
                        case "Busy":
                            _dialAnalysis = CallAnalysis.Busy;
                            break;

                        case "Ignore":
                            _dialAnalysis = CallAnalysis.NoAnswer;
                            break;

                        case "Answering Machine":
                            _dialAnalysis = CallAnalysis.AnsweringMachine;
                            break;

                        case "No Dial Tone":
                            _dialAnalysis = CallAnalysis.NoDialTone;
                            break;

                        case "Answer":
                            _dialAnalysis = CallAnalysis.Connected;
                            break;

                        default:
                            _dialAnalysis = CallAnalysis.Error;
                            break;
                        }
                        lock (_dialoutLock)
                        {
                            Monitor.Pulse(_dialoutLock);
                        }
                    }
                    else if (command.Length == 1)
                    {
                        _line.SendDigit(char.Parse(command));
                    }
                    else
                    {
                        // echo the command back
                        Send(command);
                    }
                }
                // read more or wait for the next command to come in
                handler.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0,
                                     ReadCallback, state);
            }
        }
示例#2
0
        private void btnDial_Click(object sender, EventArgs e)
        {
            CallAnalysis result = line.Dial(txtPhoneNumber.Text, 3500);

            Console.WriteLine(result);
        }