| 1 | /* |
| 2 | * Copyright (c) 2008, https://code.google.com/p/pyronet/ |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 8 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
| 9 | * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. |
| 10 | * |
| 11 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 12 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 13 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
| 14 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 15 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 16 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 17 | */ |
| 18 | |
| 19 | package jawnae.pyronet.traffic; |
| 20 | |
| 21 | import java.nio.ByteBuffer; |
| 22 | |
| 23 | import jawnae.pyronet.PyroClient; |
| 24 | |
| 25 | public abstract class ByteSinkPacket32 implements ByteSink { |
| 26 | public static void sendTo(PyroClient client, byte[] payload) { |
| 27 | boolean isExtreme = (payload.length > 0xFFFFFFFF - 4); |
| 28 | |
| 29 | byte[] wrapped = new byte[4 + (isExtreme ? 0 : payload.length)]; |
| 30 | wrapped[0] = (byte) (payload.length >> 24); |
| 31 | wrapped[1] = (byte) (payload.length >> 16); |
| 32 | wrapped[2] = (byte) (payload.length >> 8); |
| 33 | wrapped[3] = (byte) (payload.length >> 0); |
| 34 | |
| 35 | if (!isExtreme) { |
| 36 | System.arraycopy(payload, 0, wrapped, 4, payload.length); |
| 37 | } |
| 38 | |
| 39 | client.write(client.selector().malloc(wrapped)); |
| 40 | |
| 41 | if (isExtreme) { |
| 42 | client.write(client.selector().malloc(payload)); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // |
| 47 | |
| 48 | ByteSinkLength current; |
| 49 | |
| 50 | public ByteSinkPacket32() { |
| 51 | this.reset(); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void reset() { |
| 56 | this.current = new ByteSinkLength(4) { |
| 57 | @Override |
| 58 | public void onReady(ByteBuffer buffer) { |
| 59 | // header is received |
| 60 | int len = buffer.getInt(0); |
| 61 | |
| 62 | current = new ByteSinkLength(len) { |
| 63 | @Override |
| 64 | public void onReady(ByteBuffer buffer) { |
| 65 | // sometime we want do reset in |
| 66 | // ByteSinkPacket24.this.onReady, then add the sink back |
| 67 | // to feeder, in such process, onReady should be execute |
| 68 | // at last. |
| 69 | current = null; |
| 70 | |
| 71 | // content is received |
| 72 | ByteSinkPacket32.this.onReady(buffer); |
| 73 | } |
| 74 | }; |
| 75 | } |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public int feed(byte b) { |
| 81 | if (this.current == null) { |
| 82 | throw new IllegalStateException(); |
| 83 | } |
| 84 | |
| 85 | int result = this.current.feed(b); |
| 86 | |
| 87 | if (result == FEED_ACCEPTED) { |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | // 'current' will be replaced by now |
| 92 | |
| 93 | if (this.current == null) { |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | else { |
| 98 | return FEED_ACCEPTED; |
| 99 | } |
| 100 | // return this.current.feed(b); |
| 101 | } |
| 102 | } |
| 103 | |