Java Code Examples for org.apache.mina.core.buffer.IoBuffer#array()

The following examples show how to use org.apache.mina.core.buffer.IoBuffer#array() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebSocketDecoder.java    From game-server with MIT License 6 votes vote down vote up
/**
*   Try parsing the message as a websocket handshake request. If it is such
*   a request, then send the corresponding handshake response (as in Section 4.2.2 RFC 6455).
*/
private boolean tryWebSockeHandShake(IoSession session, IoBuffer in, ProtocolDecoderOutput out) {
    
    try{
        String payLoadMsg = new String(in.array());
        String socketKey = WebSocketUtils.getClientWSRequestKey(payLoadMsg);
        if(socketKey.length() <= 0){
            return false;
        }
        String challengeAccept = WebSocketUtils.getWebSocketKeyChallengeResponse(socketKey);            
        WebSocketHandShakeResponse wsResponse = WebSocketUtils.buildWSHandshakeResponse(challengeAccept);
        session.setAttribute(WebSocketUtils.SessionAttribute, true);
        session.write(wsResponse);
        return true;
    }
    catch(Exception e){
        // input is not a websocket handshake request.
        return false;
    }        
}
 
Example 2
Source File: Packet.java    From red5-websocket with Apache License 2.0 5 votes vote down vote up
/**
 * Builds the packet which just wraps the IoBuffer.
 * 
 * @param buffer
 * @return packet
 */
public static Packet build(IoBuffer buffer) {
    if (buffer.hasArray()) {
        return new Packet(buffer.array());
    }
    byte[] buf = new byte[buffer.remaining()];
    buffer.get(buf);
    return new Packet(buf);
}
 
Example 3
Source File: XinqiMessage.java    From gameserver with Apache License 2.0 5 votes vote down vote up
/**
 * Encode this message to byte array.
 * @return
 */
public byte[] toByteArray() {
	IoBuffer buf = ProtobufEncoder.encodeXinqiMessage(this);
	if ( buf != null ) {
		return buf.array();
	} else {
		return null;
	}
}
 
Example 4
Source File: WebMessageDecoder.java    From cim with Apache License 2.0 5 votes vote down vote up
private boolean isHandShakeRequest(IoSession iosession, IoBuffer buffer){
	if(Objects.equals(iosession.getAttribute(SOURCE),WEBSOCKET)) {
		return false;
	}

	buffer.mark();
	String data = new String(buffer.array());
	boolean handShake = getSecWebSocketKey(data) != null && Objects.equals(getUpgradeProtocol(data),WEBSOCKET);
	buffer.reset();
	return handShake;
}
 
Example 5
Source File: OutboundHandshake.java    From red5-client with Apache License 2.0 4 votes vote down vote up
/**
 * Decodes the second server response (S2).
 * <pre>
 * S2 = Copy of C1 bytes
 * </pre>
 * @param in incoming handshake S2
 * @return true if validation passes and false otherwise
 */
public boolean decodeServerResponse2(IoBuffer in) {
    log.debug("decodeServerResponse2");
    // the handshake type byte is not included
    byte[] s2;
    if (in.hasArray()) {
        s2 = in.array();
    } else {
        s2 = new byte[Constants.HANDSHAKE_SIZE];
        in.get(s2);
    }
    //if (log.isTraceEnabled()) {
    //    log.trace("S2: {}", Hex.encodeHexString(s2));
    //}
    if (fp9Handshake) {
        if (s2[4] == 0 && s2[5] == 0 && s2[6] == 0 && s2[7] == 0) {
            log.warn("Server refused signed authentication");
        }
        // validate server response part 2, not really required for client
        byte[] signature = new byte[DIGEST_LENGTH];
        byte[] digest = new byte[DIGEST_LENGTH];
        calculateHMAC_SHA256(c1, digestPosClient, DIGEST_LENGTH, GENUINE_FMS_KEY, GENUINE_FMS_KEY.length, digest, 0);
        calculateHMAC_SHA256(s2, 0, Constants.HANDSHAKE_SIZE - DIGEST_LENGTH, digest, DIGEST_LENGTH, signature, 0);
        log.debug("Digest key: {}", Hex.encodeHexString(digest));
        // FP10 stuff
        if (handshakeType == RTMPConnection.RTMP_ENCRYPTED_XTEA) {
            log.debug("RTMPE type 8 XTEA");
            // encrypt signatureResp
            for (int i = 0; i < DIGEST_LENGTH; i += 8) {
                //encryptXtea(signature, i, digest[i] % 15);
            }
        } else if (handshakeType == RTMPConnection.RTMP_ENCRYPTED_BLOWFISH) {
            log.debug("RTMPE type 9 Blowfish");
            // encrypt signatureResp
            for (int i = 0; i < DIGEST_LENGTH; i += 8) {
                //encryptBlowfish(signature, i, digest[i] % 15);
            }
        }
        log.debug("Signature calculated: {}", Hex.encodeHexString(signature));
        log.debug("Server sent signature: {}", Hex.encodeHexString(s2));
        if (!Arrays.equals(signature, Arrays.copyOfRange(s2, (Constants.HANDSHAKE_SIZE - DIGEST_LENGTH), (Constants.HANDSHAKE_SIZE - DIGEST_LENGTH) + DIGEST_LENGTH))) {
            log.info("Server not genuine");
            return false;
        } else {
            log.debug("Compatible flash server");
        }
    } else {
        if (!Arrays.equals(s2, c1)) {
            log.info("Client signature doesn't match!");
        }
    }
    return true;
}
 
Example 6
Source File: MsgUtil.java    From game-server with MIT License 3 votes vote down vote up
/**
 * 游戏前端收到消息改为网关内部消息进行转发
 * 
 * @author JiangZhiYong
 * @QQ 359135103 2017年7月21日 上午11:01:10
 * @param bytes
 * @return
 */
public static byte[] clientToGame(int msgID, byte[] bytes) {
	IoBuffer ioBuffer = IoBuffer.allocate(bytes.length - 4);
	ioBuffer.putInt(msgID); // 消息ID
	ioBuffer.put(bytes, 8, bytes.length - 8);
	return ioBuffer.array();
}