Exemple #1
0
        public void CloseTcpServer()
        {
            if (Client != null)
            {
                Client?.Shutdown(SocketShutdown.Both);
                Client?.Close();
            }

            if (ServerSocket != null)
            {
                ServerSocket?.Close();
            }
        }
        private void DisposeCurrentSession()
        {
            CleanClients();

            try
            {
                ServerSocket?.Close();
            }
            catch (ObjectDisposedException)
            {
            }
            catch (SocketException socketException)
            {
                OnCaughtException(socketException, EventCode.Disconnect);
            }
            catch (Exception e)
            {
                OnCaughtException(e, EventCode.Other);
            }
        }
Exemple #3
0
        public void ConnectionFailed(Mule.Network.ServerSocket sender)
        {
            if (!IsConnecting && sender != connectedSocket_)
            {
                // just return, cleanup is done by the socket itself
                return;
            }

            ED2KServer pServer = MuleApplication.Instance.ServerList.GetServerByAddress(sender.CurrentServer.Address, sender.CurrentServer.Port);

            switch (sender.ConnectionState)
            {
            case ConnectionStateEnum.CS_FATALERROR:
                //TODO:Log
                break;

            case ConnectionStateEnum.CS_DISCONNECTED:
                MuleApplication.Instance.SharedFiles.ClearED2KPublishInfo();
                break;

            case ConnectionStateEnum.CS_SERVERDEAD:
                if (pServer != null)
                {
                    pServer.AddFailedCount();
                }
                break;

            case ConnectionStateEnum.CS_ERROR:
                break;

            case ConnectionStateEnum.CS_SERVERFULL:
                break;

            case ConnectionStateEnum.CS_NOTCONNECTED:
                break;
            }

            // IMPORTANT: mark this socket not to be deleted in StopConnectionTry(),
            // because it will delete itself after this function!
            sender.IsDeleting = true;

            switch (sender.ConnectionState)
            {
            case ConnectionStateEnum.CS_FATALERROR:
            {
                bool autoretry = !IsSingleConnect;
                StopConnectionTry();
                if (MuleApplication.Instance.Preference.IsReconnect && autoretry &&
                    retryTimer_ == null)
                {
                    // There are situations where we may get Winsock error codes which indicate
                    // that the network is down, although it is not. Those error codes may get
                    // thrown only for particular IPs. If the first server in our list has such
                    // an IP and will therefor throw such an error we would never connect to
                    // any server at all. To circumvent that, start the next auto-connection
                    // attempt with a different server (use the next server in the list).
                    startAutoConnectPos_ = 0;         // default: start at 0
                    if (pServer != null)
                    {
                        // If possible, use the "next" server.
                        int iPosInList =
                            MuleApplication.Instance.ServerList.GetPositionOfServer(pServer);
                        if (iPosInList >= 0)
                        {
                            startAutoConnectPos_ = (uint)((iPosInList + 1) %
                                                          MuleApplication.Instance.ServerList.ServerCount);
                        }
                    }

                    retryTimer_ =
                        new Timer(new TimerCallback(RetryConnectTimer),
                                  this, 0,
                                  MuleConstants.ONE_SEC_MS * MuleConstants.CS_RETRYCONNECTTIME);
                }
                break;
            }

            case ConnectionStateEnum.CS_DISCONNECTED:
            {
                MuleApplication.Instance.SharedFiles.ClearED2KPublishInfo();
                IsConnected = false;
                if (connectedSocket_ != null)
                {
                    connectedSocket_.Close();
                    connectedSocket_ = null;
                }
                MuleApplication.Instance.Statistics.ServerConnectTime = 0;
                MuleApplication.Instance.Statistics.Add2TotalServerDuration();
                if (MuleApplication.Instance.Preference.IsReconnect && !IsConnecting)
                {
                    ConnectToAnyServer();
                }
                break;
            }

            case ConnectionStateEnum.CS_ERROR:
            case ConnectionStateEnum.CS_NOTCONNECTED:
            {
                if (!IsConnecting)
                {
                    break;
                }
            }
                goto case ConnectionStateEnum.CS_SERVERDEAD;

            case ConnectionStateEnum.CS_SERVERDEAD:
            case ConnectionStateEnum.CS_SERVERFULL:
            {
                if (!IsConnecting)
                {
                    break;
                }
                if (IsSingleConnect)
                {
                    if (pServer != null && sender.IsServerCryptEnabledConnection &&
                        !MuleApplication.Instance.Preference.IsClientCryptLayerRequired)
                    {
                        // try reconnecting without obfuscation
                        ConnectToServer(pServer, false, true);
                        break;
                    }
                    StopConnectionTry();
                    break;
                }

                Dictionary <ulong, ServerSocket> .Enumerator pos = connectionAttemps_.GetEnumerator();
                while (pos.MoveNext())
                {
                    if (pos.Current.Value == sender)
                    {
                        connectionAttemps_.Remove(pos.Current.Key);
                        break;
                    }
                }
                TryAnotherConnectionRequest();
            }
            break;
            }
        }
Exemple #4
0
 public void Stop()
 {
     server.Close();
 }
Exemple #5
0
 static void Exit()
 {
     ServerSocket.Close();
     MaintainLevels.Close();
 }
        public void Start()
        {
            stopped = false;
            mappingStrategy.Load();
            pool = new ThreadPool("AGIServer", poolSize);
#if LOGGER
            logger.Info("Thread pool started.");
#endif
            try
            {
                var ipAddress = IPAddress.Parse(address);
                serverSocket = new ServerSocket(port, ipAddress, SocketEncoding);
            }
            catch (IOException ex)
            {
#if LOGGER
                logger.Error("Unable start AGI Server: cannot to bind to " + address + ":" + port + ".", ex);
#endif
                throw ex;
            }
#if LOGGER
            logger.Info("Listening on " + address + ":" + port + ".");
#endif

            try
            {
                SocketConnection socket;
                while ((socket = serverSocket.Accept()) != null)
                {
#if LOGGER
                    logger.Info("Received connection.");
#endif
                    var connectionHandler = new AGIConnectionHandler(socket, mappingStrategy, SC511_CAUSES_EXCEPTION,
                                                                     SCHANGUP_CAUSES_EXCEPTION);
                    pool.AddJob(connectionHandler);
                }
            }
            catch (IOException ex)
            {
                if (!stopped)
                {
#if LOGGER
                    logger.Error("IOException while waiting for connections (1).", ex);
#endif
                    throw ex;
                }
            }
            finally
            {
                if (serverSocket != null)
                {
                    try
                    {
                        serverSocket.Close();
                    }
#if LOGGER
                    catch (IOException ex)
                    {
                        logger.Error("IOException while waiting for connections (2).", ex);
                    }
#else
                    catch { }
#endif
                }
                serverSocket = null;
                pool.Shutdown();
#if LOGGER
                logger.Info("AGIServer shut down.");
#endif
            }
        }
        public void OnConnectionInfoAvailable(WifiP2pInfo info)
        {
            if (_progressDialog != null && _progressDialog.IsShowing)
            {
                _progressDialog.Dismiss();
            }

            _info = info;

            View.Visibility = ViewStates.Visible;

            // The owner IP is now known.
            var view = _contentView.FindViewById <TextView>(Resource.Id.group_owner);

            view.Text = Resources.GetString(Resource.String.group_owner_text)
                        + ((info.IsGroupOwner) ? Resources.GetString(Resource.String.yes)
                            : Resources.GetString(Resource.String.no));

            // InetAddress from WifiP2pInfo struct.
            view      = _contentView.FindViewById <TextView>(Resource.Id.device_info);
            view.Text = "Group Owner IP - " + _info.GroupOwnerAddress.HostAddress;

            // After the group negotiation, we assign the group owner as the file
            // server. The file server is single threaded, single connection server
            // socket.
            if (_info.GroupFormed && _info.IsGroupOwner)
            {
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        var serverSocket = new ServerSocket(8988);
                        Log.Debug(WiFiDirectActivity.Tag, "Server: Socket opened");
                        var client = serverSocket.Accept();
                        Log.Debug(WiFiDirectActivity.Tag, "Server: connection done");
                        var f = new File(Environment.ExternalStorageDirectory + "/"
                                         + Activity.PackageName + "/wifip2pshared-" + DateTime.Now.Ticks + ".jpg");
                        var dirs = new File(f.Parent);
                        if (!dirs.Exists())
                        {
                            dirs.Mkdirs();
                        }
                        f.CreateNewFile();

                        Log.Debug(WiFiDirectActivity.Tag, "Server: copying files " + f);
                        var inputStream = client.InputStream;
                        CopyFile(inputStream, new FileStream(f.ToString(), FileMode.OpenOrCreate));
                        serverSocket.Close();
                        return(f.AbsolutePath);
                    }
                    catch (IOException e)
                    {
                        Log.Error(WiFiDirectActivity.Tag, e.Message);
                        return(null);
                    }
                })
                .ContinueWith(result =>
                {
                    if (result != null)
                    {
                        _contentView.FindViewById <TextView>(Resource.Id.status_text).Text = "File copied - " +
                                                                                             result.Result;
                        var intent = new Intent();
                        intent.SetAction(Intent.ActionView);
                        intent.SetDataAndType(Android.Net.Uri.Parse("file://" + result.Result), "image/*");
                        Activity.StartActivity(intent);
                    }
                });
            }
            else if (_info.GroupFormed)
            {
                _contentView.FindViewById <Button>(Resource.Id.btn_start_client).Visibility = ViewStates.Visible;
                _contentView.FindViewById <TextView>(Resource.Id.status_text).Text          =
                    Resources.GetString(Resource.String.client_text);
            }

            _contentView.FindViewById <Button>(Resource.Id.btn_connect).Visibility = ViewStates.Gone;
        }
Exemple #8
0
        private void RealVideoImage_ServerUnInit_button_Click(object sender, EventArgs e)
        {
            controlAccept = false;
            photoAccept   = false;
            videoAccept   = false;
            heartAccept   = false;

            if (controlThread != null && controlThread.IsAlive)
            {
                controlThread.Abort();
                controlThread = null;
            }

            if (videoThread != null)
            {
                videoThread.Abort();
                videoThread = null;
            }

            if (photoThread != null)
            {
                photoThread.Abort();
                photoThread = null;
            }

            if (heartThread != null)
            {
                heartThread.Abort();
                heartThread = null;
            }

            if (controlServer != null)
            {
                controlServer.Close();
                controlServer = null;
            }

            if (photoServer != null)
            {
                photoServer.Close();
                photoServer = null;
            }

            if (videoServer != null)
            {
                videoServer.Close();
                videoServer = null;
            }

            if (heartServer != null)
            {
                heartServer.Close();
                heartServer = null;
            }

            updateNodeStatus_timer.Enabled = false;

            lock (dicSync)
            {
                armClientDictionary.Clear();
            }

            lock (listSync)
            {
                armClientList.Clear();
            }
        }
Exemple #9
0
 public void Close()
 {
     _server.Close();
     SplashKit.CloseAllConnections();
 }
Exemple #10
0
 public void Close()
 {
     serversocket.Close();
 }