Пример #1
0
        public void Start(IPEndPoint localEndPoint)
        {
            if (Listening)
            {
                AsyncOnErrMessageRecived?.Invoke("Listener is already running!");
                return;
            }

            new Thread(() =>
            {
                Listener = new Socket(localEndPoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                try
                {
                    Listener.Bind(localEndPoint);
                    Listener.Listen(100);

                    Listening = true;
                    while (Listening)
                    {
                        ManualResetEvent.Reset();
                        AsyncOnMessageRecived?.Invoke("Waiting for a connection...");
                        Listener.BeginAccept(new AsyncCallback(AcceptCallback), Listener);
                        ManualResetEvent.WaitOne();
                    }
                }
                catch (SocketException e)
                {
                    AsyncOnErrMessageRecived?.Invoke(e.ToString());
                    AsyncOnMessageRecived?.Invoke("Try listen again...");
                    Stop();
                    Start(localEndPoint);
                }
            }).Start();
        }
Пример #2
0
        public void Stop()
        {
            if (!Listening)
            {
                AsyncOnErrMessageRecived?.Invoke("Listener is not running!");
                return;
            }

            Listener.Close();
            Listener.Dispose();
            Listening = false;
        }
 private void SendCallback(IAsyncResult ar)
 {
     try
     {
         int bytesSent = Client.EndSend(ar);
         AsyncOnMessageRecived?.Invoke(string.Format("Sent {0} bytes to client {0}.", bytesSent, IPAddress));
     }
     catch (Exception e)
     {
         AsyncOnErrMessageRecived?.Invoke(e.ToString());
     }
 }
Пример #4
0
 private void AcceptCallback(IAsyncResult ar)
 {
     ManualResetEvent.Set();
     try
     {
         Socket clientSocket = Listener.EndAccept(ar);
         AsyncOnClientConnected?.Invoke(clientSocket);
     }
     catch (ObjectDisposedException e)
     {
         AsyncOnErrMessageRecived?.Invoke(e.Message);
     }
 }
        private void Receivecallback(IAsyncResult ar)
        {
            try
            {
                int receivedByte = Client.EndReceive(ar);
                if (receivedByte == 0)
                {
                    AsyncOnMessageRecived?.Invoke("Error Handled, Client force disconnected");
                    Dispose();
                }

                if (!HeaderParsed)
                {
                    parseResult = PacketParser.HeaderParse(Buffer, (uint)receivedByte);

                    if (parseResult.HeaderParsed == false)
                    {
                        Process(sizeof(int) - receivedByte);
                    }
                    else if (parseResult.ContentLength == parseResult.ReceivedContentLength)
                    {
                        byte[] bufferClone = new byte[Buffer.Length];
                        System.Buffer.BlockCopy(Buffer, 0, bufferClone, 0, sizeof(int) + parseResult.ContentLength);

                        HeaderParsed = false;
                        AsyncOnClientDataRecived?.Invoke(this, bufferClone, parseResult);
                        Process(sizeof(int));
                    }
                    else
                    {
                        LeftContentByte = parseResult.ContentLength - parseResult.ReceivedContentLength;
                        ContentOffSet   = sizeof(int) + parseResult.ReceivedContentLength;

                        ContentFullBuffer = new byte[sizeof(int) + parseResult.ContentLength];
                        System.Buffer.BlockCopy(Buffer, 0, ContentFullBuffer, 0, receivedByte);

                        HeaderParsed = true;
                        Process(LeftContentByte);
                    }
                }
                else
                {
                    System.Buffer.BlockCopy(Buffer, 0, ContentFullBuffer, ContentOffSet, receivedByte);

                    LeftContentByte -= receivedByte;
                    ContentOffSet   += receivedByte;

                    if (LeftContentByte <= 0)
                    {
                        byte[] bufferClone = new byte[ContentFullBuffer.Length];
                        System.Buffer.BlockCopy(ContentFullBuffer, 0, bufferClone, 0, sizeof(int) + parseResult.ContentLength);

                        HeaderParsed = false;
                        AsyncOnClientDataRecived?.Invoke(this, bufferClone, parseResult);
                        Process(sizeof(int));
                    }
                    else
                    {
                        Process(LeftContentByte);
                    }
                }
            }
            catch (SocketException e)
            {
                AsyncOnErrMessageRecived?.Invoke(e.Message);
                AsyncOnMessageRecived?.Invoke("Error Handled, Client force disconnected");
                Dispose();
            }
            catch (ObjectDisposedException e)
            {
                AsyncOnErrMessageRecived?.Invoke(e.Message);
                AsyncOnMessageRecived?.Invoke("Error Handled, Client force disconnected");
                Dispose();
            }
        }
Пример #6
0
        private void Receivecallback(IAsyncResult ar)
        {
            try
            {
                int receivedByte = _client.EndReceive(ar);
                if (receivedByte == 0)
                {
                    AsyncOnMessageRecived?.Invoke("Error Handled, Client force disconnected");
                    Dispose();
                }

                if (!_headerParsed)
                {
                    _parseResult = PacketParser.HeaderParse(_buffer, (uint)receivedByte);

                    if (_parseResult.HeaderParsed == false)
                    {
                        Process(PacketParser.HeaderSize - receivedByte);
                    }
                    else if (_parseResult.ContentLength == _parseResult.ReceivedContentLength)
                    {
                        byte[] bufferClone = new byte[_buffer.Length];
                        Buffer.BlockCopy(_buffer, 0, bufferClone, 0, PacketParser.HeaderSize + _parseResult.ContentLength);

                        _headerParsed = false;
                        AsyncOnClientDataRecived?.Invoke(this, bufferClone, _parseResult);
                        Process(PacketParser.HeaderSize);
                    }
                    else
                    {
                        _leftContentByte = _parseResult.ContentLength - _parseResult.ReceivedContentLength;
                        _contentOffSet   = PacketParser.HeaderSize + _parseResult.ReceivedContentLength;

                        _contentFullBuffer = new byte[PacketParser.HeaderSize + _parseResult.ContentLength];
                        Buffer.BlockCopy(_buffer, 0, _contentFullBuffer, 0, receivedByte);

                        _headerParsed = true;
                        Process(_leftContentByte);
                    }
                }
                else
                {
                    Buffer.BlockCopy(_buffer, 0, _contentFullBuffer, _contentOffSet, receivedByte);

                    _leftContentByte -= receivedByte;
                    _contentOffSet   += receivedByte;

                    if (_leftContentByte <= 0)
                    {
                        byte[] bufferClone = new byte[_contentFullBuffer.Length];
                        Buffer.BlockCopy(_contentFullBuffer, 0, bufferClone, 0, PacketParser.HeaderSize + _parseResult.ContentLength);

                        _headerParsed = false;
                        AsyncOnClientDataRecived?.Invoke(this, bufferClone, _parseResult);
                        Process(PacketParser.HeaderSize);
                    }
                    else
                    {
                        Process(_leftContentByte);
                    }
                }
            }
            catch (SocketException e)
            {
                AsyncOnErrMessageRecived?.Invoke(e.Message);
                AsyncOnMessageRecived?.Invoke("Error Handled, Client force disconnected");
                Dispose();
            }
            catch (ObjectDisposedException e)
            {
                AsyncOnErrMessageRecived?.Invoke(e.Message);
                AsyncOnMessageRecived?.Invoke("Error Handled, Client force disconnected");
                Dispose();
            }
        }
 private void ClientSocket_AsyncOnErrMessageRecived(string msg) => AsyncOnErrMessageRecived?.Invoke(msg);