예제 #1
0
        /// <summary>
        /// Close all connections and stop all coroutines
        /// </summary>
        public void Disconnect()
        {
            if (Connected)
            {
                // Null check to prevent errors on closing application without disconnecting
                if (Tcp != null && Tcp.Connected)
                {
                    Tcp.Close();
                }

                if (_tcpCo != null)
                {
                    StopCoroutine(_tcpCo);
                    _tcpCo = null;
                }

                // Null check to prevent errors on closing application without disconnecting
                if (Udp != null)
                {
                    Udp.Close();
                }

                Connected = false;
            }
            else
            {
                LocalConsole.Instance.Log("Not currently connected");
            }
        }
예제 #2
0
파일: Server.cs 프로젝트: prepare/box2c
        public void Close()
        {
            Udp.Close();
            Tcp.Close();

            Udp = null;
            Tcp = null;
        }
예제 #3
0
        public void Disconnect(int reason)
        {
            //TODO handle reason
            Udp.Close();

            Tcp.Disconnect(false);
            Tcp.Close();
        }
예제 #4
0
		public void RunTest(IPEndPoint ep)
		{
			int close_cb_called = 0;
			int cl_send_cb_called = 0;
			int cl_recv_cb_called = 0;
			int sv_send_cb_called = 0;
			int sv_recv_cb_called = 0;

			Udp client = new Udp();
			Udp server = new Udp();

			server.Bind(ep);
			server.Message += (msg) => {
				var data = msg.Payload;
				var str = Encoding.ASCII.GetString(data.Array, data.Offset, data.Count);
				Assert.Equal(str, "PING");
				sv_recv_cb_called++;
				server.Send(msg.EndPoint, Encoding.ASCII.GetBytes("PONG"), (s) => {
					sv_send_cb_called++;
					server.Close(() => close_cb_called++);
				});
			};
			server.Resume();

			client.Send(ep, Encoding.ASCII.GetBytes("PING"), (s) => {
				cl_send_cb_called++;
				client.Message += (msg) => {
					var data = msg.Payload;
					var str = Encoding.ASCII.GetString(data.Array, data.Offset, data.Count);
					Assert.Equal(str, "PONG");
					cl_recv_cb_called++;
					client.Close(() => close_cb_called++);
				};
				client.Resume();
			});


			Assert.Equal(0, close_cb_called);
			Assert.Equal(0, cl_send_cb_called);
			Assert.Equal(0, cl_recv_cb_called);
			Assert.Equal(0, sv_send_cb_called);
			Assert.Equal(0, sv_recv_cb_called);

			Loop.Default.Run();

			Assert.Equal(2, close_cb_called);
			Assert.Equal(1, cl_send_cb_called);
			Assert.Equal(1, cl_recv_cb_called);
			Assert.Equal(1, sv_send_cb_called);
			Assert.Equal(1, sv_recv_cb_called);


#if DEBUG
			Assert.Equal(1, UV.PointerCount);
#endif
		}
예제 #5
0
        public static void NotNullUdp(IPEndPoint ep)
        {
            var u = new Udp();
            Action<bool> cb = (_) => { };

            string ipstr = ep.Address.ToString();
            var ip = ep.Address;

            // constructor
            Assert.Throws<ArgumentNullException>(() => new Udp(null));

            // bind
            Assert.Throws<ArgumentNullException>(() => u.Bind(null));
            Assert.Throws<ArgumentNullException>(() => u.Bind(null as string, 0));
            Assert.Throws<ArgumentNullException>(() => u.Bind(null as IPAddress, 0));

            // receive
            Assert.Throws<ArgumentNullException>(() => u.Receive(null));
            Assert.Throws<ArgumentNullException>(() => u.Receive(Encoding.ASCII, null));
            Assert.Throws<ArgumentNullException>(() => u.Receive(null, (_, __) => { }));

            // send
            Assert.Throws<ArgumentNullException>(() => u.Send(null as IPEndPoint, new byte[] { }));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as IPEndPoint, new byte[] { }, 0));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as IPEndPoint, new byte[] { }, cb));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as IPEndPoint, new byte[] { }, 0, cb));

            Assert.Throws<ArgumentNullException>(() => u.Send(ep, null as byte[]));
            Assert.Throws<ArgumentNullException>(() => u.Send(ep, null as byte[], 0));
            Assert.Throws<ArgumentNullException>(() => u.Send(ep, null as byte[], cb));
            Assert.Throws<ArgumentNullException>(() => u.Send(ep, null as byte[], 0, cb));

            Assert.Throws<ArgumentNullException>(() => u.Send(null as string, 0, new byte[] { }));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as string, 0, new byte[] { }, 0));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as string, 0, new byte[] { }, cb));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as string, 0, new byte[] { }, 0, cb));

            Assert.Throws<ArgumentNullException>(() => u.Send(ipstr, 0, null as byte[]));
            Assert.Throws<ArgumentNullException>(() => u.Send(ipstr, 0, null as byte[], 0));
            Assert.Throws<ArgumentNullException>(() => u.Send(ipstr, 0, null as byte[], cb));
            Assert.Throws<ArgumentNullException>(() => u.Send(ipstr, 0, null as byte[], 0, cb));

            Assert.Throws<ArgumentNullException>(() => u.Send(null as IPAddress, 0, new byte[] { }));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as IPAddress, 0, new byte[] { }, 0));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as IPAddress, 0, new byte[] { }, cb));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as IPAddress, 0, new byte[] { }, 0, cb));

            Assert.Throws<ArgumentNullException>(() => u.Send(ip, 0, null as byte[]));
            Assert.Throws<ArgumentNullException>(() => u.Send(ip, 0, null as byte[], 0));
            Assert.Throws<ArgumentNullException>(() => u.Send(ip, 0, null as byte[], cb));
            Assert.Throws<ArgumentNullException>(() => u.Send(ip, 0, null as byte[], 0, cb));

            u.Close();

            Loop.Default.RunOnce();
        }
예제 #6
0
        public static void NotNullUdp(IPEndPoint ep)
        {
            var           u  = new Udp();
            Action <bool> cb = (_) => { };

            string ipstr = ep.Address.ToString();
            var    ip    = ep.Address;

            // constructor
            Assert.Throws <ArgumentNullException>(() => new Udp(null));

            // bind
            Assert.Throws <ArgumentNullException>(() => u.Bind(null));
            Assert.Throws <ArgumentNullException>(() => u.Bind(null as string, 0));
            Assert.Throws <ArgumentNullException>(() => u.Bind(null as IPAddress, 0));

            // receive
            Assert.Throws <ArgumentNullException>(() => u.Receive(null));
            Assert.Throws <ArgumentNullException>(() => u.Receive(Encoding.ASCII, null));
            Assert.Throws <ArgumentNullException>(() => u.Receive(null, (_, __) => { }));

            // send
            Assert.Throws <ArgumentNullException>(() => u.Send(null as IPEndPoint, new byte[] { }));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as IPEndPoint, new byte[] { }, 0));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as IPEndPoint, new byte[] { }, cb));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as IPEndPoint, new byte[] { }, 0, cb));

            Assert.Throws <ArgumentNullException>(() => u.Send(ep, null as byte[]));
            Assert.Throws <ArgumentNullException>(() => u.Send(ep, null as byte[], 0));
            Assert.Throws <ArgumentNullException>(() => u.Send(ep, null as byte[], cb));
            Assert.Throws <ArgumentNullException>(() => u.Send(ep, null as byte[], 0, cb));

            Assert.Throws <ArgumentNullException>(() => u.Send(null as string, 0, new byte[] { }));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as string, 0, new byte[] { }, 0));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as string, 0, new byte[] { }, cb));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as string, 0, new byte[] { }, 0, cb));

            Assert.Throws <ArgumentNullException>(() => u.Send(ipstr, 0, null as byte[]));
            Assert.Throws <ArgumentNullException>(() => u.Send(ipstr, 0, null as byte[], 0));
            Assert.Throws <ArgumentNullException>(() => u.Send(ipstr, 0, null as byte[], cb));
            Assert.Throws <ArgumentNullException>(() => u.Send(ipstr, 0, null as byte[], 0, cb));

            Assert.Throws <ArgumentNullException>(() => u.Send(null as IPAddress, 0, new byte[] { }));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as IPAddress, 0, new byte[] { }, 0));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as IPAddress, 0, new byte[] { }, cb));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as IPAddress, 0, new byte[] { }, 0, cb));

            Assert.Throws <ArgumentNullException>(() => u.Send(ip, 0, null as byte[]));
            Assert.Throws <ArgumentNullException>(() => u.Send(ip, 0, null as byte[], 0));
            Assert.Throws <ArgumentNullException>(() => u.Send(ip, 0, null as byte[], cb));
            Assert.Throws <ArgumentNullException>(() => u.Send(ip, 0, null as byte[], 0, cb));

            u.Close();

            Loop.Default.RunOnce();
        }
예제 #7
0
        public void RunTest(IPEndPoint ep)
        {
            int close_cb_called   = 0;
            int cl_send_cb_called = 0;
            int cl_recv_cb_called = 0;
            int sv_send_cb_called = 0;
            int sv_recv_cb_called = 0;

            Udp client = new Udp();
            Udp server = new Udp();

            server.Bind(ep);
            server.Message += (msg) => {
                var data = msg.Payload;
                var str  = Encoding.ASCII.GetString(data.Array, data.Offset, data.Count);
                Assert.Equal(str, "PING");
                sv_recv_cb_called++;
                server.Send(msg.EndPoint, Encoding.ASCII.GetBytes("PONG"), (s) => {
                    sv_send_cb_called++;
                    server.Close(() => close_cb_called++);
                });
            };
            server.Resume();

            client.Send(ep, Encoding.ASCII.GetBytes("PING"), (s) => {
                cl_send_cb_called++;
                client.Message += (msg) => {
                    var data = msg.Payload;
                    var str  = Encoding.ASCII.GetString(data.Array, data.Offset, data.Count);
                    Assert.Equal(str, "PONG");
                    cl_recv_cb_called++;
                    client.Close(() => close_cb_called++);
                };
                client.Resume();
            });


            Assert.Equal(0, close_cb_called);
            Assert.Equal(0, cl_send_cb_called);
            Assert.Equal(0, cl_recv_cb_called);
            Assert.Equal(0, sv_send_cb_called);
            Assert.Equal(0, sv_recv_cb_called);

            Loop.Default.Run();

            Assert.Equal(2, close_cb_called);
            Assert.Equal(1, cl_send_cb_called);
            Assert.Equal(1, cl_recv_cb_called);
            Assert.Equal(1, sv_send_cb_called);
            Assert.Equal(1, sv_recv_cb_called);


#if DEBUG
            Assert.Equal(1, UV.PointerCount);
#endif
        }
예제 #8
0
        public void CanSendHandles()
        {
            int count = 0;

            Loop.Default.Run(async() => {
                var handles      = new Stack <Handle>();
                string name      = "test";
                var pipelistener = new IPCPipeListener();
                pipelistener.Bind(name);
                pipelistener.Connection += () => {
                    var client = pipelistener.Accept();
                    client.Resume();
                    client.HandleData += (handle, data) => {
                        handles.Push(handle);
                        count++;
                        if (count == 3)
                        {
                            foreach (var h in handles)
                            {
                                h.Close();
                            }
                            pipelistener.Close();
                        }
                    };
                };
                pipelistener.Listen();

                var pipe = new IPCPipe();
                await pipe.ConnectAsync(name);


                var ipep        = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7000);
                var tcplistener = new TcpListener();
                tcplistener.Bind(ipep);
                tcplistener.Connection += () => {
                    var client = tcplistener.Accept();
                    pipe.Write(client, new byte[1], (ex) => {
                        client.Close();
                        tcplistener.Close();
                    });
                };
                tcplistener.Listen();

                var tcp = new Tcp();
                await tcp.ConnectAsync(ipep);
                tcp.Write("HELLO WORLD");

                var udp = new Udp();
                udp.Bind(ipep);
                pipe.Write(udp, Encoding.Default.GetBytes("UDP"), (ex) => udp.Close());
                pipe.Write(pipe, Encoding.Default.GetBytes("pipe"), (ex) => pipe.Close());
            });

            Assert.Equal(3, count);
        }
예제 #9
0
		void TestCanSendHandles(string pipename, IPEndPoint ipep)
		{
			int count = 0;

			Loop.Default.Run(async () => {
				var handles = new Stack<Handle>();
				var pipelistener = new IPCPipeListener();
				pipelistener.Bind(pipename);
				pipelistener.Connection += () => {
					var client = pipelistener.Accept();
					client.Resume();
					client.HandleData += (handle, data) => {
						handles.Push(handle);
						count++;
						if (count == 3) {
							foreach (var h in handles) {
								h.Close();
							}
							pipelistener.Close();
						}
					};
				};
				pipelistener.Listen();

				var pipe = new IPCPipe();
				await pipe.ConnectAsync(pipename);

				var tcplistener = new TcpListener();
				tcplistener.Bind(ipep);
				tcplistener.Connection += () => {
					var client = tcplistener.Accept();
					pipe.Write(client, new byte[1], (ex) => {
						client.Close();
						tcplistener.Close();
					});
				};
				tcplistener.Listen();

				var tcp = new Tcp();
				await tcp.ConnectAsync(ipep);
				tcp.Write("HELLO WORLD");

				var udp = new Udp();
				udp.Bind(ipep);
				pipe.Write(udp, Encoding.Default.GetBytes("UDP"), (ex) => udp.Close());
				pipe.Write(pipe, Encoding.Default.GetBytes("pipe"), (ex) => pipe.Close());
			});

			Assert.Equal(3, count);
		}
예제 #10
0
        public static void Run(IPEndPoint ep)
        {
            int close_cb_called   = 0;
            int cl_send_cb_called = 0;
            int cl_recv_cb_called = 0;
            int sv_send_cb_called = 0;
            int sv_recv_cb_called = 0;

            Udp client = new Udp();
            Udp server = new Udp();

            server.Bind(ep);
            server.Receive(Encoding.ASCII, (rinfo, str) => {
                Assert.AreEqual(str, "PING");
                sv_recv_cb_called++;
                server.Send(rinfo, Encoding.ASCII.GetBytes("PONG"), (s) => {
                    sv_send_cb_called++;
                    server.Close(() => { close_cb_called++; });
                });
            });

            client.Send(ep, Encoding.ASCII.GetBytes("PING"), (s) => {
                cl_send_cb_called++;
                client.Receive(Encoding.ASCII, (rinfo, str) => {
                    Assert.AreEqual(str, "PONG");
                    cl_recv_cb_called++;
                    client.Close(() => { close_cb_called++; });
                });
            });


            Assert.AreEqual(0, close_cb_called);
            Assert.AreEqual(0, cl_send_cb_called);
            Assert.AreEqual(0, cl_recv_cb_called);
            Assert.AreEqual(0, sv_send_cb_called);
            Assert.AreEqual(0, sv_recv_cb_called);

            Loop.Default.Run();

            Assert.AreEqual(2, close_cb_called);
            Assert.AreEqual(1, cl_send_cb_called);
            Assert.AreEqual(1, cl_recv_cb_called);
            Assert.AreEqual(1, sv_send_cb_called);
            Assert.AreEqual(1, sv_recv_cb_called);


#if DEBUG
            Assert.AreEqual(1, UV.PointerCount);
#endif
        }
예제 #11
0
파일: Client.cs 프로젝트: prepare/box2c
        public void Close()
        {
            if (Tcp.Connected)
            {
                Tcp.BatchStream.Write(ServerPacketTypeBase.Disconnected);
                Tcp.BatchStream.Write(PacketTypeBase.EndOfMessage);
                Tcp.Check();
            }

            Udp.Close();
            Tcp.Close();

            Udp = null;
            Tcp = null;
        }
예제 #12
0
파일: Osc.cs 프로젝트: Damagae/iannix-unity
 public void Cancel()
 {
     //Debug.Log("Osc Cancel start");
     if (ReaderRunning)
     {
         ReaderRunning = false;
         ReadThread.Abort();
     }
     if (OscPacketIO != null && OscPacketIO.IsOpen())
     {
         OscPacketIO.Close();
         OscPacketIO = null;
     }
     //Debug.Log("Osc Cancel finished");
 }
예제 #13
0
        public void Disconnect()
        {
            if (!isDisconnected && null != udp)
            {
                isDisconnected = true;

                JObject json = new JObject();
                json[Tag.COMMAND] = Cmd.DISCONNECT;
                json[Tag.ID]      = id;

                udp.Send(json.ToString());

                udp.Close();
                udp = null;
            }
        }
예제 #14
0
        /********************************************************************************/

        private void ctlServerListenStop_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            objServer.Close();

            if (GetServerResult() == 0)
            {
                ctlServerListenStart.Enabled = true;
                ctlServerListenStop.Enabled  = false;
                ctlServerPort.Enabled        = true;
                ctlServerVersion.Enabled     = true;
                Timer.Enabled = false;
            }

            Cursor.Current = Cursors.Default;
        }
예제 #15
0
        /********************************************************************************/

        private void ctlClientClose_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            objClient.Close();

            if (GetClientResult() == 0)
            {
                ctlClientSend.Enabled    = false;
                ctlClientClose.Enabled   = false;
                ctlClientOpen.Enabled    = true;
                ctlClientCommand.Enabled = false;
                ctlClientHost.Enabled    = true;
                ctlClientPort.Enabled    = true;
            }

            Cursor.Current = Cursors.Default;
        }
예제 #16
0
파일: Server.cs 프로젝트: prepare/box2c
        public void Disconnect()
        {
            Active = false;

            foreach (var x in Server.Players)
            {
                if (x == this)
                {
                    continue;
                }

                SendData(x, ClientPacketTypeBase.PlayerDataBits.ActiveBit);
            }

            State = PlayerState.Disconnected;
            Name  = "";
            Server.Players.Remove(this);

            Udp.Close();
            Tcp.Close();
        }
예제 #17
0
        private void OnStop()
        {
            Invalidater.Instance.Unregister(this);

            if (udp != null)
            {
                if (players.Count > 0)
                {
                    JObject res = new JObject();
                    res[Tag.COMMAND] = Cmd.SERVER_CLOSED;

                    Broadcast(res.ToString());
                }

                players.Clear();
                keys.Clear();

                udp.Close();
                udp = null;
            }
        }
예제 #18
0
        public static void Run(IPEndPoint ep)
        {
            int close_cb_called = 0;
            int cl_send_cb_called = 0;
            int cl_recv_cb_called = 0;
            int sv_send_cb_called = 0;
            int sv_recv_cb_called = 0;

            Udp client = new Udp();
            Udp server = new Udp();

            server.Bind(ep);
            server.Receive(Encoding.ASCII, (rinfo, str) => {
                Assert.AreEqual(str, "PING");
                sv_recv_cb_called++;
                server.Send(rinfo, Encoding.ASCII.GetBytes("PONG"), (s) => {
                    sv_send_cb_called++;
                    server.Close(() => { close_cb_called++; });
                });
            });

            client.Send(ep, Encoding.ASCII.GetBytes("PING"), (s) => {
                cl_send_cb_called++;
                client.Receive(Encoding.ASCII, (rinfo, str) => {
                    Assert.AreEqual(str, "PONG");
                    cl_recv_cb_called++;
                    client.Close(() => { close_cb_called++; });
                });
            });

            Assert.AreEqual(0, close_cb_called);
            Assert.AreEqual(0, cl_send_cb_called);
            Assert.AreEqual(0, cl_recv_cb_called);
            Assert.AreEqual(0, sv_send_cb_called);
            Assert.AreEqual(0, sv_recv_cb_called);

            Loop.Default.Run();

            Assert.AreEqual(2, close_cb_called);
            Assert.AreEqual(1, cl_send_cb_called);
            Assert.AreEqual(1, cl_recv_cb_called);
            Assert.AreEqual(1, sv_send_cb_called);
            Assert.AreEqual(1, sv_recv_cb_called);

            #if DEBUG
            Assert.AreEqual(1, UV.PointerCount);
            #endif
        }