Ejemplo n.º 1
0
        void recvProc(IAsyncResult ar)//异步Receive
        {
            stateobject state = (stateobject)ar.AsyncState;
            XmlDocument Doc   = new XmlDocument();
            SipSocket   temp;

            if (state.isClosed)
            {
                return;
            }
            try
            {
                state.socket.BeginReceive(state.recv, 0, state.BufferSize, 0, new AsyncCallback(recvProc), state);
                int i = state.socket.EndReceive(ar);
                this.Dispatcher.BeginInvoke(new Action(() => Result.AppendText(Encoding.GetEncoding("GB2312").GetString(state.recv, 0, i))));
                SipSocket.Add(state.socket, new SIPTools(state.recv, i));
                temp = SipSocket.FindSipSocket(state.socket);
                string[] result;
                Doc = SIPTools.XmlExtract(recv, i);
                if (Doc == null)
                {
                    return;
                }
                if (InterfaceC.IsRequest(Doc))
                {
                    temp.SendResponse(InterfaceC.Request(Doc));
                }
                else
                {
                    result = InterfaceC.Response(Doc);
                    if (result != null)
                    {
                        SipSocket.InsertResult(state.socket, result);
                        for (int k = 0; k < result.Length; k++)
                        {
                            Console.WriteLine(result[k]);
                        }
                    }
                }
            }
            catch (SocketException e)
            {
                state.isClosed = true;
                state.socket.Dispose();
                Console.WriteLine(e.Message);
                return;
            }
            catch (XmlException e)
            {
                Console.WriteLine(e.Message);
                return;
            }
        }