Example #1
0
        public static void Main(string[] args)
        {
            object tLck = new object();
            var    tCount = 0;
            int    lastSended = 0, lastRec = 0;

            while (true)
            {
                if (tCount < 1)
                {
                    var action = new Action(() =>
                    {
                        var client = new TcpClientSocket();
                        //   client.HandleMessage += Client_HandleMessage;
                        if (client.Connect("10.202.196.51", 23456))
                        {
                            lock (tLck)
                                tCount++;
                            Message msg = new Message();
                            //client.SendMessageAsync(msg);
                            var loop = 100000000;
                            Thread.Sleep(1000);
                            while (loop-- > 0)
                            {
                                //  Thread.Sleep(300);
                                //  if (sendCount % 2 == 0)
                                {
                                    msg.Value = $"{sendCount}Client: SendMessgae:{DateTime.Now}";
                                    //client.SendMessage(msg, 10000);
                                }
                                //      else
                                {
                                    msg.Value = $"{sendCount}Client: SendMessgaeSync:{DateTime.Now}";
                                    if (client.SendMessageAsync(msg))
                                    {
                                        lock (lck2)
                                            sendCount++;
                                    }
                                    else
                                    {
                                        lock (tLck)
                                            tCount--;
                                        break;
                                    }
                                }
                            }
                        }
                        client.Dispose();
                    });
                    Task task = new Task(action);
                    task.Start();
                }
                Console.WriteLine($"发出:{sendCount}  收到:{recCount}  线程数:{tCount}");
                Console.Title = $"发出:{sendCount - lastSended}  接收:{recCount - lastRec}  线程数:{tCount}";
                lastSended    = sendCount;
                lastRec       = recCount;
                //sendCount = 0;
                Thread.Sleep(1000);
            }
        }
        public void Connect(IPAddress ip, int port)
        {
            TcpClientSocket tcpClientSocket = new TcpClientSocket();

            this.socket = tcpClientSocket;
            tcpClientSocket.OnConnected += delegate(TcpClientSocket sender)
            {
                this.onConnected();
            };
            tcpClientSocket.Connect(ip, port);
        }
Example #3
0
        public static void GenerateExternal()
        {
            Debug.Log("Connecting...");

            var client = new TcpClientSocket();

            client.OnConnect    += onConnected;
            client.OnReceive    += onReceive;
            client.OnDisconnect += onDisconnect;
            client.Connect(IPAddress.Parse("127.0.0.1"), 3333);

            AssetDatabase.Refresh();
        }
        public void Run()
        {
            TcpSocket.Register(OnConnect, OnClose, OnMessage, Log);
            //string host = "127.0.0.1";
            string host = "111.230.108.129";
            int    port = 8888;

            TcpSocket.Connect(host, port);

            while (true)
            {
                //Console.WriteLine("tcpsocket.Dispatch");
                ClientLogic();
                FixUpdated();
                Thread.Sleep(2000);
            }
        }
Example #5
0
        static void initClient(string[] args)
        {
            IPAddress ip   = null;
            int       port = 0;

            try {
                ip   = IPAddress.Parse(args[1]);
                port = int.Parse(args[2]);
                var client = new TcpClientSocket();
                client.OnDisconnect += onDisconnect;
                client.Connect(ip, port);
                _socket = client;
                start();
            } catch (Exception) {
                Console.WriteLine("Please specify a valid ip and port");
            }
        }
Example #6
0
        public bool Connect(string ipString, int port)
        {
            if (m_ClientSocket != null)
            {
                return(false);
            }

            m_ClientSocket               = new TcpClientSocket();
            m_ClientSocket.OnConnect    += OnConnected;
            m_ClientSocket.OnReceive    += OnReceived;
            m_ClientSocket.OnDisconnect += OnDisconnected;

            Status = ClientNetworkStatus.Connecting;

            m_ClientSocket.Connect(IPAddress.Parse(ipString), port);

            UpdateRunner.AddUpdate(this);

            return(true);
        }
Example #7
0
        public override void Run(string[] args)
        {
            var port = 0;

            try {
                port     = int.Parse(args[1]);
                _command = args[2];
            } catch (Exception) {
                port     = 3333;
                _command = args[1];
            }

            var client = new TcpClientSocket();

            client.OnConnect    += onConnected;
            client.OnReceive    += onReceive;
            client.OnDisconnect += onDisconnect;
            client.Connect(IPAddress.Parse("127.0.0.1"), port);

            while (true)
            {
            }
        }
Example #8
0
        protected override void run()
        {
            var port = 0;

            try {
                port     = int.Parse(_rawArgs[1]);
                _command = string.Join(" ", _rawArgs.Skip(2).ToArray());
            } catch (Exception) {
                port     = 3333;
                _command = string.Join(" ", _rawArgs.Skip(1).ToArray());
            }

            var client = new TcpClientSocket();

            client.OnConnect    += onConnected;
            client.OnReceive    += onReceive;
            client.OnDisconnect += onDisconnect;
            client.Connect(IPAddress.Parse("127.0.0.1"), port);

            while (true)
            {
            }
        }
Example #9
0
        private void TestInternal(string host, int port)
        {
            var client = new TcpClientSocket();

            client.Resolve(host, port.ToString(), (args1) =>
            {
                if (!args1.Successful)
                {
                    Console.WriteLine("failed to resolve host {0}", host);
                    return;
                }

                Console.WriteLine("host resolved to {0}", args1.Data);

                client.Connect(args1.Data[0], (args2) =>
                {
                    if (!args2.Successful)
                    {
                        Console.WriteLine("connection failed");
                        return;
                    }

                    Console.WriteLine("connection to {0} successful", args1.Data);

                    client.Close(false, (args3) =>
                    {
                        if (!args3.Successful)
                        {
                            Console.WriteLine("failed to close connection");
                            return;
                        }

                        Console.WriteLine("connection closed");
                    });
                });
            });
        }
Example #10
0
        private void TestInternal(string host, int port)
        {
            var client = new TcpClientSocket();

            client.Resolve(host, port.ToString(), (args1) =>
            {
                if (!args1.Successful)
                {
                    Console.WriteLine("failed to resolve host {0}", host);
                    return;
                }

                Console.WriteLine("host resolved to {0}", args1.Data);

                client.Connect(args1.Data[0], (args2) =>
                {
                    if (!args2.Successful)
                    {
                        Console.WriteLine("connection failed");
                        return;
                    }

                    Console.WriteLine("connection to {0} successful", args1.Data);

                    client.Close(false, (args3) =>
                    {
                        if (!args3.Successful)
                        {
                            Console.WriteLine("failed to close connection");
                            return;
                        }

                        Console.WriteLine("connection closed");
                    });
                });
            });
        }
Example #11
0
        /// <summary>
        ///   This method gets called by the ServiceHost each time a service provider
        ///   with a matching serviceTypeId was found. Naturally this gets only called
        ///   for service clients.
        /// </summary>
        /// <param name="remoteHostGuid"></param>
        /// <param name="serviceGuid"></param>
        /// <param name="serviceTag"></param>
        /// <param name="remoteEndPoint"></param>
        internal bool AddProvider(ulong remoteHostGuid, ulong remoteServiceGuid,
                                  ulong remoteInterfaceUids, ushort serviceTag, IPEndPoint remoteEndPoint)
        {
            if (IsProvider)
            {
                throw new InvalidOperationException();
            }

            lock (_knownRemoteServiceGuids)
            {
                // Swap the two interface uids to ease comparison with the
                // local uids.
                remoteInterfaceUids =
                    (remoteInterfaceUids << 32) |
                    (remoteInterfaceUids >> 32);

                if (_knownRemoteServiceGuids.Contains(remoteServiceGuid))
                {
                    return(false);
                }
                _knownRemoteServiceGuids.Add(remoteServiceGuid);

                if (remoteInterfaceUids != InterfaceUids)
                {
                    var foundIncompatibleRemoteService = FoundIncompatibleRemoteService;
                    if (foundIncompatibleRemoteService != null)
                    {
                        var e = new FoundIncompatibleRemoteServiceArgs(
                            remoteHostGuid, ServiceGuid, remoteServiceGuid,
                            ServiceUid, InterfaceUids, remoteInterfaceUids,
                            remoteEndPoint);
                        foundIncompatibleRemoteService(this, e);
                    }
                    return(false);
                }

                lock (_connections)
                {
                    foreach (var connection in _connections)
                    {
                        if (connection.Value.ServiceGuid == remoteServiceGuid)
                        {
                            throw new InvalidOperationException();
                        }
                    }

                    bool accept             = true;
                    var  foundRemoteService = FoundRemoteService;
                    if (foundRemoteService != null)
                    {
                        var e = new FoundRemoteServiceArgs(remoteHostGuid, remoteServiceGuid,
                                                           serviceTag, remoteEndPoint);
                        foundRemoteService(this, e);
                        accept = e.Accept;
                    }
                    if (!accept)
                    {
                        return(false);
                    }

                    var socket = new TcpClientSocket();
                    socket.ConnectFailed += OnConnectFailed;
                    socket.Connected     += OnConnected;
                    socket.Disconnected  += OnDisconnected;
                    socket.Connect(remoteEndPoint);
                    return(true);
                }
            }
        }