Example #1
0
        internal Query(Connection conn, SqlStringTemplate sql, CommandParams cmdParams)
        {
            //***a query uses conn resource such as parser,writer
            //so 1 query=> 1 connection
            if (sql == null)
            {
                throw new Exception("Sql command can not null.");
            }

            Query bindingQuery = conn.BindingQuery;

            if (bindingQuery != null)
            {
                //check if binding query can be close
                if (!bindingQuery.LateClose())
                {
                    //can't use this conn
                    throw new Exception("connection is in used");
                }
            }
            //--------------------------------------------------------------
            conn.BindingQuery = this;
            //--------------------------------------------------------------
            _conn      = conn;
            _cmdParams = cmdParams;
            //--------------------------------------------------------------

            _nestTables  = false;
            _sqlParserMx = conn.MySqlParserMx;
            _writer      = conn.PacketWriter;
            //_receiveBuffer = null;
            _sqlStrTemplate = sql;
        }
        public Connection(ConnectionConfig userConfig)
        {
            config         = userConfig;
            recvBufferSize = userConfig.recvBufferSize;
            sendBufferSize = userConfig.sendBufferSize;
            socket         = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //
            switch ((CharSets)config.charsetNumber)
            {
            case CharSets.UTF8_GENERAL_CI:
                //_parser = new PacketParser(Encoding.UTF8);
                _writer = new MySqlStreamWriter(Encoding.UTF8);
                break;

            case CharSets.ASCII:
                //_parser = new PacketParser(Encoding.ASCII);
                _writer = new MySqlStreamWriter(Encoding.ASCII);
                break;

            default:
                throw new NotImplementedException();
            }

            //------------------
            //we share recvSendArgs between recvIO and sendIO
            //similar to simple http
            //it is simple, (NOT  duplex like web socket)
            //------------------
            recvSendArgs = new SocketAsyncEventArgs();
            recvSendArgs.SetBuffer(new byte[recvBufferSize + sendBufferSize], 0, recvBufferSize + sendBufferSize);
            recvIO = new RecvIO(recvSendArgs, recvSendArgs.Offset, recvBufferSize, HandleReceive);
            sendIO = new SendIO(recvSendArgs, recvSendArgs.Offset + recvBufferSize, sendBufferSize, HandleSend);
            //------------------
            //common(shared) event listener***
            recvSendArgs.Completed += (object sender, SocketAsyncEventArgs e) =>
            {
                switch (e.LastOperation)
                {
                case SocketAsyncOperation.Receive:
                    recvIO.ProcessReceivedData();
                    break;

                case SocketAsyncOperation.Send:
                    sendIO.ProcessWaitingData();
                    break;

                default:
                    throw new ArgumentException("The last operation completed on the socket was not a receive or send");
                }
            };
            //------------------
            recvSendArgs.AcceptSocket = socket;
            _mysqlParserMx            = new MySqlParserMx(config);
        }
Example #3
0
        public Connection(ConnectionConfig userConfig)
        {
            _config         = userConfig;
            _recvBufferSize = userConfig.recvBufferSize;
            _sendBufferSize = userConfig.sendBufferSize;
            _socket         = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _writer         = new MySqlStreamWriter(_config.GetEncoding());

            //------------------
            //we share recvSendArgs between recvIO and sendIO
            //similar to simple http
            //it is simple, (NOT  duplex like web socket)
            //------------------
            _recvSendArgs = new SocketAsyncEventArgs();
            _recvSendArgs.SetBuffer(new byte[_recvBufferSize + _sendBufferSize], 0, _recvBufferSize + _sendBufferSize);
            _recvIO = new RecvIO(_recvSendArgs, _recvSendArgs.Offset, _recvBufferSize, HandleReceive);
            _sendIO = new SendIO(_recvSendArgs, _recvSendArgs.Offset + _recvBufferSize, _sendBufferSize, HandleSend);
            //------------------

            //common(shared) event listener***
            _recvSendArgsCompleted = (object sender, SocketAsyncEventArgs e) =>
            {
                if (_isDisposed)
                {
                    //it should not occur
                    return;
                }

                switch (e.LastOperation)
                {
                case SocketAsyncOperation.Receive:
                    _recvIO.ProcessReceivedData();
                    break;

                case SocketAsyncOperation.Send:
                    _sendIO.ProcessWaitingData();
                    break;

                default:
                    throw new ArgumentException("The last operation completed on the socket was not a receive or send");
                }
            };
            _recvSendArgs.Completed += _recvSendArgsCompleted;
            //------------------
            _recvSendArgs.AcceptSocket = _socket;
            _mysqlParserMx             = new MySqlParserMx(_config);
        }
Example #4
0
        public Query(Connection conn, SqlStringTemplate sql, CommandParams cmdParams)
        {
            //*** query use conn resource such as parser,writer
            //so 1 query 1 connection      
            Query bindingQuery = conn.BindingQuery;
            if (bindingQuery != null)
            {
                //check if binding query can be close 
                if (!bindingQuery.LateClose())
                {
                    //can't use this conn
                    throw new Exception("connection is in used");
                }
            }
            //--------------------------------------------------------------
            conn.BindingQuery = this;
            //--------------------------------------------------------------
            if (sql == null)
            {
                throw new Exception("Sql command can not null.");
            }
            //--------------------------------------------------------------
            this._conn = conn;
            this._cmdParams = cmdParams;
            //--------------------------------------------------------------

            nestTables = false;
            _sqlParserMx = conn.MySqlParserMx;
            _writer = conn.PacketWriter;
            //_receiveBuffer = null;
            _sqlStrTemplate = sql;
        }
        public Connection(ConnectionConfig userConfig)
        {
            config = userConfig;
            recvBufferSize = userConfig.recvBufferSize;
            sendBufferSize = userConfig.sendBufferSize;
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //
            switch ((CharSets)config.charsetNumber)
            {
                case CharSets.UTF8_GENERAL_CI:
                    //_parser = new PacketParser(Encoding.UTF8);
                    _writer = new MySqlStreamWriter(Encoding.UTF8);
                    break;
                case CharSets.ASCII:
                    //_parser = new PacketParser(Encoding.ASCII);
                    _writer = new MySqlStreamWriter(Encoding.ASCII);
                    break;
                default:
                    throw new NotImplementedException();
            }

            //------------------
            //we share recvSendArgs between recvIO and sendIO
            //similar to simple http 
            //it is simple, (NOT  duplex like web socket)             
            //------------------
            recvSendArgs = new SocketAsyncEventArgs();
            recvSendArgs.SetBuffer(new byte[recvBufferSize + sendBufferSize], 0, recvBufferSize + sendBufferSize);
            recvIO = new RecvIO(recvSendArgs, recvSendArgs.Offset, recvBufferSize, HandleReceive);
            sendIO = new SendIO(recvSendArgs, recvSendArgs.Offset + recvBufferSize, sendBufferSize, HandleSend);
            //------------------
            //common(shared) event listener***
            recvSendArgs.Completed += (object sender, SocketAsyncEventArgs e) =>
            {
                switch (e.LastOperation)
                {
                    case SocketAsyncOperation.Receive:
                        recvIO.ProcessReceivedData();
                        break;
                    case SocketAsyncOperation.Send:
                        sendIO.ProcessWaitingData();
                        break;
                    default:
                        throw new ArgumentException("The last operation completed on the socket was not a receive or send");
                }
            };
            //------------------
            recvSendArgs.AcceptSocket = socket;
            _mysqlParserMx = new MySqlParserMx(config);

        }