Example #1
0
    private void Update()
    {
        if (socketConnection != null)
        {
            if (socketConnection.Connected)
            {
                if (OnServerConnected != null)
                {
                    isConnected = true;
                    OnServerConnected("Open : " + socketConnection.Client.RemoteEndPoint);
                }

                if (!clientReceiveThread.IsAlive)
                {
                    clientReceiveThread.Abort();
                    clientReceiveThread = new Thread(() => ListenForData(null, 0, null, null));
                    clientReceiveThread.IsBackground = true;
                    clientReceiveThread.Start();
                }
            }
            else
            {
                OnServerDisconnect?.Invoke("Close");
            }

            /*
             * else if(socketConnection.Connected == false)
             * {
             *
             *  OnServerDisconnect("Close");
             *  isConnected = false;
             *  socketConnection.Close();
             *
             *  //Try to reconnect after 5 seconds of connection loss
             *  while (reconnect_time > 5)
             *  {
             *
             *      reconnect_time = 0f;
             *      connect("_CONEC_");
             *  }
             *  reconnect_time += Time.deltaTime;
             * }
             * else
             * {
             *  reconnect_time = 0f;
             * }
             *
             */
        }
    }
Example #2
0
 private void Write(string line, bool console = true)
 {
     try
     {
         writer.WriteLine(line);
         writer.Flush();
         if (console)
         {
             IrcLog(line);
         }
     }
     catch (System.IO.IOException) {
         IrcLog($"Error: Disconnected from {Config.UplinkHost}");
         OnServerDisconnect?.Invoke(this, new EventArgs());
     }
 }
    public FizzySteamyMirror()
    {
        // dispatch the events from the server
        server.OnConnected += (id) => OnServerConnect?.Invoke(id);
        server.OnDisconnected += (id) => OnServerDisconnect?.Invoke(id);
        server.OnReceivedData += (id, data) => OnServerData?.Invoke(id, data);
        server.OnReceivedError += (id, exception) => OnServerError?.Invoke(id, exception);

        // dispatch events from the client
        client.OnConnected += () => OnClientConnect?.Invoke();
        client.OnDisconnected += () => OnClientDisconnect?.Invoke();
        client.OnReceivedData += (data) => OnClientData?.Invoke(data);
        client.OnReceivedError += (exception) => OnClientError?.Invoke(exception);

        Debug.Log("FizzySteamyMirror initialized!");
    }
 public void ServerDisconnect()
 {
     OnServerDisconnect?.Invoke(this);
 }
Example #5
0
        public void ConnectOrDisconnect()
        {
            if (TCPClient.Connected)
            {
                TCPClient.Client.Disconnect(true);

                UDPListen = false;
                TCPListen = false;
                Clients.Clear();

                if (UPnPEnabled)
                {
                    ClearUpUPnP();
                }

                if (OnServerDisconnect != null)
                {
                    OnServerDisconnect.Invoke(this, new EventArgs());
                }

                if (OnResultsUpdate != null)
                {
                    OnResultsUpdate.Invoke(this, "Disconnected.");
                }
            }
            else
            {
                try
                {
                    InternetAccessAdapter = GetAdapterWithInternetAccess();

                    if (OnResultsUpdate != null)
                    {
                        OnResultsUpdate.Invoke(this, "Adapter with Internet Access: " + InternetAccessAdapter);
                    }

                    TCPClient = new TcpClient();
                    TCPClient.Client.Connect(ServerEndpoint);

                    UDPListen = true;
                    TCPListen = true;

                    SendMessageUDP(LocalClientInfo.Simplified(), ServerEndpoint);
                    LocalClientInfo.InternalEndpoint = (IPEndPoint)UDPClient.Client.LocalEndPoint;

                    if (UPnPEnabled)
                    {
                        UPnPMappings = UPnPNAT.StaticPortMappingCollection;
                        ClearUpUPnP();

                        if (LocalClientInfo.InternalEndpoint != null)
                        {
                            if (OnResultsUpdate != null)
                            {
                                OnResultsUpdate.Invoke(this, "UDP Listening on Port " + LocalClientInfo.InternalEndpoint.Port);
                            }

                            if (AttemptUPnP(LocalClientInfo.InternalEndpoint.Port))
                            {
                                if (OnResultsUpdate != null)
                                {
                                    OnResultsUpdate.Invoke(this, "UPnP Map Added");
                                }

                                LocalClientInfo.UPnPEnabled = true;
                            }
                            else
                            {
                                if (OnResultsUpdate != null)
                                {
                                    OnResultsUpdate.Invoke(this, "UPnP Mapping Not Possible");
                                }
                            }
                        }
                    }

                    Thread.Sleep(500);
                    SendMessageTCP(LocalClientInfo);

                    Thread KeepAlive = new Thread(new ThreadStart(delegate
                    {
                        while (TCPClient.Connected)
                        {
                            Thread.Sleep(5000);
                            SendMessageTCP(new KeepAlive());
                        }
                    }));

                    KeepAlive.IsBackground = true;
                    KeepAlive.Start();

                    if (OnServerConnect != null)
                    {
                        OnServerConnect.Invoke(this, new EventArgs());
                    }
                }
                catch (Exception ex)
                {
                    if (OnResultsUpdate != null)
                    {
                        OnResultsUpdate.Invoke(this, "Error when connecting " + ex.Message);
                    }
                }
            }
        }