コード例 #1
0
        /// <summary>
        /// 处理有效的socket数据接收
        /// </summary>
        private void HanlderIsSocketConnectedDataReceived(SocketConnectionInfo connection, int bytesRead, IAsyncResult asyncResult)
        {
            if (bytesRead == 0 || (bytesRead > 0 && bytesRead < SocketConnectionInfo.BufferSize))
            {
                byte[] _buffer         = connection.Buffer;
                int    _totalBytesRead = connection.BytesRead;
                connection        = new SocketConnectionInfo();
                connection.Buffer = new byte[SocketConnectionInfo.BufferSize];
                connection.Socket = ((SocketConnectionInfo)asyncResult.AsyncState).Socket;

                if (this.Protocol == SocketProtocol.UDP)
                {
                    connection.Socket.BeginReceiveFrom(connection.Buffer, 0, connection.Buffer.Length, SocketFlags.None, ref ipeSender, new AsyncCallback(DataReceived), connection);
                }
                else if (this.Protocol == SocketProtocol.TCP)
                {
                    connection.Socket.BeginReceive(connection.Buffer, 0, connection.Buffer.Length, SocketFlags.None, new AsyncCallback(DataReceived), connection);
                }

                if (_totalBytesRead < _buffer.Length)
                {
                    Array.Resize <byte>(ref _buffer, _totalBytesRead);
                }

                OnDataReceived.RaiseEvent(null, CreateSocketSeesion(connection, _buffer));

                _buffer = null;
            }
            else
            {
                Array.Resize <Byte>(ref connection.Buffer, connection.Buffer.Length + SocketConnectionInfo.BufferSize);

                if (this.Protocol == SocketProtocol.UDP)
                {
                    connection.Socket.BeginReceiveFrom(connection.Buffer, 0, connection.Buffer.Length, SocketFlags.None, ref ipeSender, new AsyncCallback(DataReceived), connection);
                }
                else if (this.Protocol == SocketProtocol.TCP)
                {
                    connection.Socket.BeginReceive(connection.Buffer, 0, connection.Buffer.Length, SocketFlags.None, new AsyncCallback(DataReceived), connection);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// 处理特殊的数据接受,buffer大于零的情况
        /// </summary>
        private void HanlderSpecialDataReceived(SocketConnectionInfo connection, int bytesRead, IAsyncResult asyncResult)
        {
            Array.Resize <byte>(ref connection.Buffer, connection.BytesRead);

            OnDataReceived.RaiseEvent(null, CreateSocketSeesion(connection, connection.Buffer));
        }