コード例 #1
0
ファイル: TcpSocket.cs プロジェクト: unkprog/dpas
        /// <summary>
        /// Метод для асинхронной отправки данных сокету
        /// </summary>
        /// <param name="data">Данные</param>
        /// <param name="socketTo">Сокет, которому отправляются данные</param>
        /// <returns>True - в случае успешной отправки</returns>
        protected bool Send(byte[] data, Socket socketTo)
        {
            try
            {
                if (!socketTo.Connected)
                {
                    return(false);
                }
                TcpSocketAsyncEventArgs e = poolEventArgs.Pop();
                e.AcceptSocket = socketTo;
                if (e.Buffer == null)
                {
                    e.SetBuffer(data, 0, data.Length);
                }
#if DEBUG
                if (isLogging)
                {
                    WriteToLog(string.Concat("Sending ", data.Length, " bytes"));
                }
#endif
                if (!e.Socket.SendAsync(e))
                {
                    ProcessSend(e);
                }
            }
            catch (System.Exception ex)
            {
                SetException(ex, "TcpSocket.Send(byte[] data):");
                return(false);
            }
            return(true);
        }
コード例 #2
0
        // This method is triggered when the accept socket completes an operation async
        // In the case of our accept socket, we are looking for a client connection to complete
        // This method is used to process the accept socket connection
        protected void ProcessAccept(TcpSocketAsyncEventArgs e, bool fromAccept)
        {
#if DEBUG
            if (isLogging)
            {
                WriteToLog(string.Concat("ProcessAccept (fromAccept=", fromAccept, ", e.Socket.Connected=", e.Socket.Connected, ", e.Buffer.Length=", e.Buffer == null ? 0 : e.Buffer.Length, ")"));
            }
#endif
            listenEvent.Set();

            // If the accept socket is connected to a client we will process it
            // otherwise nothing happens
            if (e.Socket.Connected)
            {
                try
                {
                    if (e.BytesTransferred > 0)
                    {
#if DEBUG
                        if (isLogging)
                        {
                            WriteToLog("ProcessAccept: acceptSocket.BytesTransferred > 0");
                        }
#endif
                        ProcessReceive(e);
                    }
                    else
                    {
#if DEBUG
                        if (isLogging)
                        {
                            WriteToLog("ProcessAccept: acceptSocket.ReceiveAsync()");
                        }
#endif
                        //e.SetBuffer(new byte[Settings.BufferSize], 0, Settings.BufferSize);
                        e.SetBuffer(new byte[Settings.BufferSize], 0, Settings.BufferSize);
                        // Start a receive request and immediately check to see if the receive is already complete
                        // Otherwise OnIOCompleted will get called when the receive is complete
                        if (!e.Socket.ReceiveAsync(e))
                        {
                            ProcessReceive(e);
                        }
                    }
                }
                catch (Exception ex)
                {
                    SetException(ex, "TcpServer.ProcessAccept():");
                }

                //// Start the process again to wait for the next connection
                //StartProcessAccept(AsyncEventArgs);
            }
        }