Пример #1
0
 private void Receive()
 {
     if (IsConnected)
     {
         _socket.BeginReceive(_receiveBuffer, 0, _receiveBuffer.Length, SocketFlags.None, OnReceiveCallback, _socket);
     }
     else
     {
         OnReceiveCompletedEvent?.Invoke(null, SocketClientResult.NotConnect);
     }
 }
Пример #2
0
        private void OnReceiveCallback(IAsyncResult result)
        {
            if (!IsRunning)
            {
                OnReceiveCompletedEvent?.Invoke(string.Empty, null, SocketServerResult.ServerNotRunning);

                return;
            }

            string ip = string.Empty;

            SocketServerResult sRes = SocketServerResult.None;

            byte[] data = null;

            int length = 0;

            try
            {
                ip = result.AsyncState as string;

                IClientSocket client = this[ip];

                length = client.Socket.EndReceive(result);

                if (length > 0)
                {
                    data = new byte[length];

                    Array.Copy(client.CacheBuffer, data, length);

                    sRes = SocketServerResult.Success;
                }
                else
                {
                    DisconnectClient(ip);
                }
            }
            catch
            {
                sRes = SocketServerResult.Exception;
            }
            finally
            {
                if (length > 0)
                {
                    OnReceiveCompletedEvent?.Invoke(ip, data, sRes);

                    Receive(ip);
                }
            }
        }
Пример #3
0
        private void OnReceiveCallback(IAsyncResult result)
        {
            SocketClientResult cRes = SocketClientResult.None;

            byte[] buffer = null;

            int length = 0;

            try
            {
                if (IsConnected)
                {
                    Socket socket = result.AsyncState as Socket;

                    length = socket.EndReceive(result);

                    buffer = new byte[length];

                    Array.Copy(_receiveBuffer, buffer, length);

                    cRes = SocketClientResult.Success;
                }
                else
                {
                    cRes = SocketClientResult.NotConnect;
                }
            }
            catch
            {
                cRes = SocketClientResult.Exception;
            }
            finally
            {
                if (length > 0)
                {
                    OnReceiveCompletedEvent?.Invoke(buffer, cRes);

                    Receive();
                }
                else
                {
                    Disconnect();
                }
            }
        }