Example #1
0
        void SendToHole(String msg)
        {
            var ep = new IPEndPoint(HoleServer.Address, HoleServer.Port + 1);

            EnsureServer();
            var server = Server as UdpServer;

            if (server != null)
            {
                server.Client.Send(msg, null, HoleServer);
                //server.Send("test", null, HoleServer);
                if (msg.StartsWith("reg"))
                {
                    server.Client.Send("checknat", null, ep);
                }
            }
            else
            {
                var client = new TcpSession() as ISocketSession;
                //client.Address = Server.LocalEndPoint.Address;
                //client.Port = Server.LocalEndPoint.Port;
                //client.ReuseAddress = true;
                //client.Connect(ep);
                client.Local.EndPoint = Server.Local.EndPoint;
                client.Send("checknat");
                WriteLog("HoleServer数据到来:{0}", client.ReceiveString());
                client.Dispose();

                EnsureClient();
                //Client.Send(msg, null);
                Client.Send(msg);
            }
        }
Example #2
0
        ///// <summary>
        ///// SOAP主体
        ///// </summary>
        //const String SOAP_BODY = "" +
        //    "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
        //    "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n" +
        //    "<s:Body>\r\n" +
        //    "{0}\r\n" +
        //    "</s:Body>\r\n" +
        //    "</s:Envelope>\r\n";

        /// <summary>发送SOAP请求,发送xml,返回xml</summary>
        /// <param name="uri"></param>
        /// <param name="action"></param>
        /// <param name="xml"></param>
        /// <returns></returns>
        static String SOAPRequest(Uri uri, String action, String xml)
        {
            //String body = String.Format(SOAP_BODY, xml);
            String header = String.Format(SOAP_HEADER, uri.PathAndQuery, uri.Host + ":" + uri.Port, action, Encoding.UTF8.GetByteCount(xml));

            var client  = new TcpSession();
            var session = client as ISocketSession;

            try
            {
                //client.Connect(uri.Host, uri.Port);
                client.Remote.Host = uri.Host;
                client.Remote.Port = uri.Port;
                session.Send(header + xml);

                String response = session.ReceiveString();
                if (String.IsNullOrEmpty(response))
                {
                    return(null);
                }

                Int32 p = response.IndexOf("\r\n\r\n");
                if (p < 0)
                {
                    return(null);
                }

                response = response.Substring(p).Trim();
                if (String.IsNullOrEmpty(response))
                {
                    response = session.ReceiveString();
                }

                //Envelope env = null;
                //XmlSerializer serial = new XmlSerializer(typeof(Envelope));
                //using (var reader = new StringReader(response))
                //{
                //    env = serial.Deserialize(reader) as Envelope;
                //}
                var env = response.ToXmlEntity <Envelope>();
                if (env == null || env.Body == null)
                {
                    return(null);
                }

                if (!String.IsNullOrEmpty(env.Body.Fault))
                {
                    throw env.Body.ThrowException();
                }

                return(env.Body.Xml);
            }
            finally { client.Dispose(); }
        }
Example #3
0
        void SendToHole(String msg)
        {
            var ep = new IPEndPoint(HoleServer.Address, HoleServer.Port + 1);
            EnsureServer();
            var server = Server as UdpServer;
            if (server != null)
            {
                server.Client.Send(msg, null, HoleServer);
                //server.Send("test", null, HoleServer);
                if (msg.StartsWith("reg"))
                {
                    server.Client.Send("checknat", null, ep);
                }
            }
            else
            {
                var client = new TcpSession() as ISocketSession;
                //client.Address = Server.LocalEndPoint.Address;
                //client.Port = Server.LocalEndPoint.Port;
                //client.ReuseAddress = true;
                //client.Connect(ep);
                client.Local.EndPoint = Server.Local.EndPoint;
                client.Send("checknat");
                WriteLog("HoleServer数据到来:{0}", client.ReceiveString());
                client.Dispose();

                EnsureClient();
                //Client.Send(msg, null);
                Client.Send(msg);
            }
        }
Example #4
0
 public void Dispose()
 {
     session?.Dispose();
     connection?.Dispose();
 }