Beispiel #1
0
        public void Start(IPEndPoint localIPEP, ushort bufferSize = 512)
        {
            _Cleanup();

            if (localIPEP == null)
            {
                throw new ArgumentNullException(nameof(localIPEP));
            }

            if (bufferSize < 64)
            {
                throw new ArgumentOutOfRangeException(nameof(bufferSize));
            }

            LocalIPEP  = localIPEP;
            BufferSize = bufferSize;

            _isOpen = new ValueWrapper <bool>(true);

            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            _socket.Bind(localIPEP);

            _thread = new Thread(_ReceiveThread);
            _thread.Start();

            LocalIPEP = (IPEndPoint)_socket.LocalEndPoint;

            OnStart?.Invoke();
        }
Beispiel #2
0
        public void Connect(IPEndPoint serverIPEP, ushort bufferSize = 8 * 1024) // 8KB
        {
            _Cleanup();
            if (bufferSize < 64)
            {
                throw new ArgumentOutOfRangeException(nameof(bufferSize));
            }

            ServerIPEP = serverIPEP ?? throw new ArgumentNullException(nameof(serverIPEP));
            BufferSize = bufferSize;

            _isOpen = new ValueWrapper <bool>(true);

            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            _socket.ReceiveBufferSize = bufferSize;
            _socket.SendBufferSize    = bufferSize;

            _socket.Bind(new IPEndPoint(IPAddress.Any, 0));

            _thread = new Thread(_ReceiveThread);
            _thread.Start();

            LocalIPEP = (IPEndPoint)_socket.LocalEndPoint;

            OnOpen?.Invoke();
        }
        public void Start(IPEndPoint localIPEP, ushort bufferSize = 8 * 1024) // 8KB
        {
            _Cleanup();
            if (bufferSize < 64)
            {
                throw new ArgumentOutOfRangeException(nameof(bufferSize));
            }

            LocalIPEP  = localIPEP ?? throw new ArgumentNullException(nameof(localIPEP));
            BufferSize = bufferSize;

            _isListening = new ValueWrapper <bool>(true);

            _connections       = new Dictionary <IPEndPoint, _Client>();
            _connectionsCached = new Dictionary <IPEndPoint, _Client>();

            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            _socket.ReceiveBufferSize = bufferSize;
            _socket.SendBufferSize    = bufferSize;

            _socket.Bind(localIPEP);
            _socket.Listen(64);

            _thread = new Thread(_ListenThread);
            _thread.Start();

            LocalIPEP = (IPEndPoint)_socket.LocalEndPoint;

            OnStart?.Invoke();
        }
Beispiel #4
0
            public _Client(ValueWrapper <bool> isListening, Socket socket, ushort bufferSize, ParameterizedThreadStart threadFunction, Action <_Client> onClosed)
            {
                _isListening = isListening;

                _onClosed = onClosed;

                BufferSize = bufferSize;
                Buffer     = new byte[bufferSize];

                Socket     = socket;
                RemoteIPEP = (IPEndPoint)socket.RemoteEndPoint;

                Thread = new Thread(threadFunction);
                Thread.Start(this);
            }
Beispiel #5
0
        private void _ReceiveThread()
        {
            try
            {
                var isOpen = _isOpen;
                if (!isOpen.Value)
                {
                    return;
                }

                _socket.Connect(ServerIPEP);
                _isConnected = new ValueWrapper <bool>(true);
                OnConnect?.Invoke();

                var buffer = new byte[BufferSize];
                while (isOpen.Value)
                {
                    var recSize = _socket.Receive(buffer, 0, BufferSize, SocketFlags.None);

                    if (recSize == 0)
                    {
                        OnDisconnect?.Invoke();
                        _Cleanup();
                        return;
                    }

                    var packet = new byte[recSize];
                    Array.Copy(buffer, packet, recSize);
                    OnData?.Invoke(packet);
                }
            }
            catch (ThreadAbortException)
            {
            }
            catch (Exception ex)
            {
                OnDisconnect?.Invoke(ex);
                _Cleanup();
            }
        }