Search in sources :

Example 11 with ErrorPacket

use of com.alibaba.cobar.net.mysql.ErrorPacket in project cobar by alibaba.

the class MySQLChannel method sendAutocommit.

/**
 * 发送事务递交模式设置
 */
private void sendAutocommit(boolean autocommit) throws IOException {
    CommandPacket cmd = getAutocommitCommand(autocommit);
    cmd.write(out);
    out.flush();
    BinaryPacket bin = receive();
    switch(bin.data[0]) {
        case OkPacket.FIELD_COUNT:
            this.autocommit = autocommit;
            break;
        case ErrorPacket.FIELD_COUNT:
            ErrorPacket err = new ErrorPacket();
            err.read(bin);
            throw new ErrorPacketException(new String(err.message, charset));
        default:
            throw new UnknownPacketException(bin.toString());
    }
}
Also used : ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket) ErrorPacketException(com.alibaba.cobar.exception.ErrorPacketException) UnknownPacketException(com.alibaba.cobar.exception.UnknownPacketException) CommandPacket(com.alibaba.cobar.net.mysql.CommandPacket) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Example 12 with ErrorPacket

use of com.alibaba.cobar.net.mysql.ErrorPacket in project cobar by alibaba.

the class MySQLChannel method sendCharset.

/**
 * 发送字符集设置
 */
private void sendCharset(int ci) throws IOException {
    // 发送命令: 直接写入到out中即可
    CommandPacket cmd = getCharsetCommand(ci);
    cmd.write(out);
    out.flush();
    BinaryPacket bin = receive();
    switch(bin.data[0]) {
        case OkPacket.FIELD_COUNT:
            this.charsetIndex = ci;
            this.charset = CharsetUtil.getCharset(ci);
            this.dbCharset = CharsetUtil.getCharset(ci);
            break;
        case ErrorPacket.FIELD_COUNT:
            ErrorPacket err = new ErrorPacket();
            err.read(bin);
            throw new ErrorPacketException(new String(err.message, charset));
        default:
            throw new UnknownPacketException(bin.toString());
    }
}
Also used : ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket) ErrorPacketException(com.alibaba.cobar.exception.ErrorPacketException) UnknownPacketException(com.alibaba.cobar.exception.UnknownPacketException) CommandPacket(com.alibaba.cobar.net.mysql.CommandPacket) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Example 13 with ErrorPacket

use of com.alibaba.cobar.net.mysql.ErrorPacket in project cobar by alibaba.

the class MySQLConnectionAuthenticator method handle.

@Override
public void handle(byte[] data) {
    try {
        HandshakePacket packet = source.getHandshake();
        if (packet == null) {
            // 设置握手数据包
            packet = new HandshakePacket();
            packet.read(data);
            source.setHandshake(packet);
            source.setThreadId(packet.threadId);
            // 设置字符集编码
            int charsetIndex = (packet.serverCharsetIndex & 0xff);
            String charset = CharsetUtil.getDbCharset(charsetIndex);
            if (charset != null) {
                source.setCharsetIndex(charsetIndex);
                source.setCharset(CharsetUtil.getCharset(charsetIndex));
                source.setDbCharset(charset);
            } else {
                throw new RuntimeException("Unknown charsetIndex:" + charsetIndex);
            }
            // 发送认证数据包
            source.authenticate();
        } else {
            // 处理认证结果
            switch(data[4]) {
                case OkPacket.FIELD_COUNT:
                    source.setHandler(new MySQLConnectionHandler(source));
                    source.setAuthenticated(true);
                    if (listener != null) {
                        listener.connectionAcquired(source);
                    }
                    break;
                case ErrorPacket.FIELD_COUNT:
                    ErrorPacket err = new ErrorPacket();
                    err.read(data);
                    throw new RuntimeException(new String(err.message));
                case EOFPacket.FIELD_COUNT:
                    auth323(data[3]);
                    break;
                default:
                    throw new RuntimeException("Unknown Packet!");
            }
        }
    } catch (RuntimeException e) {
        if (listener != null) {
            listener.connectionError(e, source);
        }
        throw e;
    }
}
Also used : HandshakePacket(com.alibaba.cobar.net.mysql.HandshakePacket) ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket)

Example 14 with ErrorPacket

use of com.alibaba.cobar.net.mysql.ErrorPacket in project cobar by alibaba.

the class PacketUtil method getShutdown.

public static final ErrorPacket getShutdown() {
    ErrorPacket error = new ErrorPacket();
    error.packetId = 1;
    error.errno = ErrorCode.ER_SERVER_SHUTDOWN;
    error.message = "The server has been shutdown".getBytes();
    return error;
}
Also used : ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket)

Example 15 with ErrorPacket

use of com.alibaba.cobar.net.mysql.ErrorPacket in project cobar by alibaba.

the class CobarDetectorAuthenticator method handle.

@Override
public void handle(byte[] data) {
    CobarDetector source = this.source;
    HandshakePacket hsp = source.getHandshake();
    if (hsp == null) {
        // 设置握手数据包
        hsp = new HandshakePacket();
        hsp.read(data);
        source.setHandshake(hsp);
        // 设置字符集编码
        int charsetIndex = (hsp.serverCharsetIndex & 0xff);
        String charset = CharsetUtil.getDbCharset(charsetIndex);
        if (charset != null) {
            source.setCharsetIndex(charsetIndex);
        } else {
            throw new RuntimeException("Unknown charsetIndex:" + charsetIndex);
        }
        // 发送认证数据包
        source.authenticate();
    } else {
        // 处理认证结果
        switch(data[4]) {
            case OkPacket.FIELD_COUNT:
                source.setHandler(new CobarDetectorHandler(source));
                source.setAuthenticated(true);
                // 认证成功后,发起心跳。
                source.heartbeat();
                break;
            case ErrorPacket.FIELD_COUNT:
                ErrorPacket err = new ErrorPacket();
                err.read(data);
                throw new RuntimeException(new String(err.message));
            default:
                throw new RuntimeException("Unknown packet");
        }
    }
}
Also used : HandshakePacket(com.alibaba.cobar.net.mysql.HandshakePacket) ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket)

Aggregations

ErrorPacket (com.alibaba.cobar.net.mysql.ErrorPacket)26 ErrorPacketException (com.alibaba.cobar.exception.ErrorPacketException)6 UnknownPacketException (com.alibaba.cobar.exception.UnknownPacketException)6 BinaryPacket (com.alibaba.cobar.net.mysql.BinaryPacket)6 CommandPacket (com.alibaba.cobar.net.mysql.CommandPacket)4 HandshakePacket (com.alibaba.cobar.net.mysql.HandshakePacket)4 ServerConnection (com.alibaba.cobar.server.ServerConnection)4 HeartbeatException (com.alibaba.cobar.exception.HeartbeatException)2 UnknownCharsetException (com.alibaba.cobar.exception.UnknownCharsetException)1 UnknownDataNodeException (com.alibaba.cobar.exception.UnknownDataNodeException)1 HeartbeatPacket (com.alibaba.cobar.net.mysql.HeartbeatPacket)1 OkPacket (com.alibaba.cobar.net.mysql.OkPacket)1 Reply323Packet (com.alibaba.cobar.net.mysql.Reply323Packet)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1