コード例 #1
0
 public void Initialize(IPEndPoint endpoint, Guid peerId, IReliableSock socket, ISerializer serializer, IApplicationConfig config, IShamanLogger logger)
 {
     _endpoint = endpoint;
     _peerId   = peerId;
     _socket   = socket;
     _logger   = logger;
 }
コード例 #2
0
        public void Add(IPEndPoint endPoint, IReliableSock socket)
        {
            var peer = new T();

            peer.Initialize(endPoint, Guid.NewGuid(), socket, _serializer, _config, _logger);
            _peers.TryAdd(endPoint, peer);
        }
コード例 #3
0
        public void Connect(string address, int port)
        {
            //switch Sockets implementation.BEGIN
            //_socket = new HazelSock(_logger);
            _socket = new LiteNetSock(_logger);
            //switch sockets implementation.END

            _socket.OnPacketReceived += (endPoint, dataPacket, release) =>
            {
                _onPackageReceived(dataPacket, release);
                //todo if using hazel then this part should be considered in releaseAction param
//                _socket.ReturnBufferToPool(dataPacket.Buffer);
            };
            _socket.OnDisconnected += OnDisconnected;

            _socket.OnConnected += ep => { OnConnectedToServer?.Invoke(); };

            _ep = GetIPEndPointFromHostName(address, port, false); // new IPEndPoint(IPAddress.Parse(address), port);
            _socket.Connect(_ep);

            _connected = true;
            _isTicking = false;
            //Send(new ConnectedEvent());
            _socketTickTask = _taskScheduler.ScheduleOnInterval(() =>
            {
                lock (_stateSync)
                {
                    if (!_connected)
                    {
                        return;
                    }

                    lock (_isTickingMutex)
                    {
                        if (_isTicking)
                        {
                            return;
                        }
                        _isTicking = true;
                    }

                    try
                    {
                        _socket.Tick();
                    }
                    catch (Exception ex)
                    {
                        _logger.Error($"Socket tick error: {ex}");
                    }
                    finally
                    {
                        lock (_isTickingMutex)
                            _isTicking = false;
                    }
                }
            }, 0, 10);
            _logger?.Debug($"Receive loop started");
        }
コード例 #4
0
 public void Initialize(IPEndPoint endpoint, Guid peerId, IReliableSock socket, ISerializer serializer,
                        IApplicationConfig config, IShamanLogger logger)
 {
     throw new NotImplementedException();
 }
コード例 #5
0
        public void Listen()
        {
            //create reliable socket
            switch (Config.SocketType)
            {
            case SocketType.BareSocket:
                _reliableSocket = _socketFactory.GetReliableSockWithBareSocket(_logger);
                break;

            case SocketType.ThreadSocket:
                _reliableSocket = _socketFactory.GetReliableSockWithThreadSocket(_logger);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            _reliableSocket.Listen(_port);

            _reliableSocket.AddEventCallbacks(OnReceivePacket, OnNewClientConnect, OnClientDisconnect);

            _lastTick       = DateTime.UtcNow;
            _isTicking      = false;
            _socketTickTask = TaskScheduler.ScheduleOnInterval(() =>
            {
                if (_isStopping)
                {
                    return;
                }
                lock (_isTickingMutex)
                {
                    if (_isTicking)
                    {
                        return;
                    }
                    _isTicking = true;
                }
                var duration = (DateTime.UtcNow - _lastTick).Milliseconds;
                _lastTick    = DateTime.UtcNow;
                if (duration > _maxSendDuration) // overlapping not matters
                {
                    _maxSendDuration = duration;
                }

                try
                {
                    _reliableSocket.Tick();
                }
                catch
                {
                    //empty
                }
                finally
                {
                    lock (_isTickingMutex)
                        _isTicking = false;
                }
            }, 0, Config.SocketTickTimeMs);

            //start protection
            _protectionManager.Start();
        }