Changeset 7124
- Timestamp:
- 02/25/08 18:27:46 (5 years ago)
- Location:
- lang/actionscript/FxTerm/trunk
- Files:
-
- 1 removed
- 3 modified
- 1 moved
-
FxTerm.as (modified) (20 diffs)
-
FxTermTest.mxml (modified) (1 diff)
-
Telnet.as (moved) (moved from lang/actionscript/FxTerm/trunk/FxTelnet.as) (1 diff)
-
Terminal.as (modified) (4 diffs)
-
TerminalEvent.as (deleted)
Legend:
- Unmodified
- Added
- Removed
-
lang/actionscript/FxTerm/trunk/FxTerm.as
r7117 r7124 15 15 import flash.utils.IDataOutput; 16 16 17 public class FxTerm extends Sprite implements IDataOutput { 18 19 private var telnetClient:FxTelnet; 17 public class FxTerm extends Sprite implements IDataOutput 18 { 20 19 21 20 private var screen:Array; // Array of Array of TextCell … … 41 40 private var drawBuffer:ByteArray; 42 41 43 private var decoderBuffer:ByteArray;42 private var _decoderBuffer:ByteArray; 44 43 45 44 private var ansiForegroundColors:Array; // Array of RGB, length must be 7 … … 62 61 63 62 // IDataOutput interface 64 private var _objectEncoding:uint; 65 private var _endian:String; 63 private var _middleBuffer:ByteArray; 66 64 67 65 /** … … 121 119 fontCalibration(); 122 120 123 resize(col, row);124 125 121 // FIXME 126 122 characterWidthMeasure = new TextField(); … … 129 125 130 126 drawBuffer = new ByteArray(); 131 decoderBuffer = new ByteArray(); 132 133 _objectEncoding = decoderBuffer.objectEncoding; 134 _endian = decoderBuffer.endian; 135 136 telnetClient = new FxTelnet(); 137 telnetClient.addEventListener(ProgressEvent.SOCKET_DATA, dataHandler); 138 telnetClient.connect("127.0.0.1", 5000); 139 this.addEventListener(KeyboardEvent.KEY_DOWN, keyboardHandler); 127 _decoderBuffer = new ByteArray(); 128 _middleBuffer = new ByteArray(); 129 130 resize(col, row); 140 131 } 141 132 … … 150 141 fontCalibration(); 151 142 adjustDisplay(); 152 for(var r:uint = 0; r < rows; ++r) { 153 lineDirty[r] = true; 154 } 155 cursorDirty = true; 156 refresh(); 143 } 144 145 /** 146 * Get font size. 147 */ 148 public function get fontSize():Object 149 { 150 return displayTextFormat.size; 157 151 } 158 152 … … 205 199 206 200 /** 201 * Get number of columns 202 */ 203 public function get col():uint 204 { 205 return cols; 206 } 207 208 /** 207 209 * Set number of columns 208 210 */ … … 213 215 214 216 /** 217 * Get number of rows 218 */ 219 public function get row():uint 220 { 221 return rows; 222 } 223 224 /** 215 225 * Set number of rows 216 226 */ … … 218 228 { 219 229 resize(cols, n); 220 }221 222 // FIXME223 private function keyboardHandler(event:KeyboardEvent):void224 {225 if( event.charCode == 0 ) { return; }226 telnetClient.writeByte(event.charCode);227 230 } 228 231 … … 234 237 public function resize(newcols:uint, newrows:uint):void 235 238 { 239 if( newcols <= 1 ) { newcols = 1; } 240 if( newrows <= 1 ) { newrows = 1; } 241 236 242 screen.length = newrows; 237 243 lineDirty.length = newrows; … … 256 262 tf.selectable = true; 257 263 tf.x = 0; 258 tf.y = lineHeight * r;264 // tf.y will be set at adjustDisplay() 259 265 addChild(tf); 260 266 } … … 287 293 rows = newrows; 288 294 289 for(r = 0; r < rows; ++r) { 290 lineDirty[r] = true; 291 } 292 293 adjustDisplay(); 295 if( ccol >= cols ) { ccol = cols - 1; } 296 if( crow >= rows ) { crow = rows - 1; } 294 297 295 298 // FIXME 296 299 scrolltop = 0; 297 300 scrollbottom = rows - 1; 301 302 adjustDisplay(); 298 303 } 299 304 … … 314 319 { 315 320 var dwidth:Number = calibrateFontWidth * cols; 316 var dheight:Number = calibrateFontHeight * rows;321 var dheight:Number = lineHeight * rows + lineGapFilling; 317 322 background.bitmapData = new BitmapData( 318 323 dwidth, … … 323 328 //cursorCanvas.width = dwidth; 324 329 //cursorCanvas.height = dheight; 330 //this.width = dwidth; 331 //this.height = dheight; 332 for(var r:uint = 0; r < rows; ++r) { 333 fieldArray[r].y = lineHeight * r; 334 } 335 refresh(true); // force refresh 325 336 } 326 337 … … 328 339 /** 329 340 * refresh the console 330 */ 331 public function refresh():void 332 { 341 * @param force force redraw whole screen 342 */ 343 public function refresh(force:Boolean = true):void 344 { 345 if(force) { 346 for(var f:uint = 0; f < rows; ++f) { 347 lineDirty[f] = true; 348 } 349 } 350 333 351 var field:TextField; 334 352 var attr:uint = screen[0][0]; … … 359 377 360 378 // draw cursor 361 if( cursorDirty ) {379 if( cursorDirty || force ) { 362 380 cursorDirty = false; 363 381 cursorCanvas.graphics.clear(); 364 cursorCanvas.graphics.beginFill(ansiForegroundColors[0], 1.0); // FIXME cursor color 382 // FIXME cursor color 383 cursorCanvas.graphics.beginFill(ansiForegroundColors[0], 1.0); 365 384 cursorCanvas.graphics.drawRect( 366 385 (this.width / cols - 0.05) * ccol + 2, … … 412 431 public function writeBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0):void 413 432 { 414 var decoder:ByteArray = decoderBuffer;433 var decoder:ByteArray = _decoderBuffer; 415 434 var c:String; 416 435 var len:uint = (length != 0 ? length : bytes.length); … … 454 473 } 455 474 456 457 // FIXME458 private function dataHandler(event:ProgressEvent):void459 {460 var buffer:ByteArray = telnetClient.buffer;461 var len:uint = buffer.length;462 buffer.position = 0;463 464 var decoder:ByteArray = decoderBuffer;465 var c:String;466 for(var n:uint = 0; n < len; ++n) {467 var b:uint = buffer[n];468 if( escaped ) {469 // escape sequence470 escapeSequence.writeByte(b);471 tryInterpretEscapeSequence();472 } else if( decoder.length == 0 ) {473 if( b >= 1 && b <= 31 ) {474 // control character475 handleControlChar(b);476 } else if( b <= 0x7F ) {477 // single byte character478 if( b != 0 ) { // ignore NULL479 c = String.fromCharCode(b); // FIXME480 // TODO: graphmode481 putNormalCharacter(c);482 }483 } else {484 // first byte of multibyte character485 decoder.writeByte(b);486 }487 } else {488 decoder.position = decoder.length;489 decoder.writeByte(b);490 try {491 decoder.position = 0;492 c = decoder.readMultiByte(decoder.length, charSet);493 if( c.length > 0 ) {494 // complete multibyte character495 decoder.length = 0;496 putNormalCharacter(c);497 }498 } catch (e:Error) {499 // insufficient data to complete multibyte character500 }501 }502 }503 504 buffer.length = 0;505 refresh(); // FIXME506 }507 508 475 private function cursorLineDown():void 509 476 { … … 1133 1100 1134 1101 // FIXME how ad-hoc! 1102 // FIXME this method doesn't work when font size is very small 1135 1103 private function isWideCharacter(c:String):Boolean 1136 1104 { … … 1205 1173 public function set objectEncoding(value:uint):void 1206 1174 { 1207 _ objectEncoding = value;1175 _middleBuffer.objectEncoding = value; 1208 1176 } 1209 1177 public function get objectEncoding():uint 1210 1178 { 1211 return _ objectEncoding;1179 return _middleBuffer.objectEncoding; 1212 1180 } 1213 1181 1214 1182 public function set endian(value:String):void 1215 1183 { 1216 _ endian = value;1184 _middleBuffer.endian = value; 1217 1185 } 1218 1186 public function get endian():String 1219 1187 { 1220 return _ endian;1188 return _middleBuffer.endian; 1221 1189 } 1222 1190 1223 1191 public function writeBoolean(value:Boolean):void 1224 1192 { 1225 var bytes:ByteArray = new ByteArray(); 1226 bytes.writeBoolean(value); 1227 writeBytes(bytes); 1193 var buffer:ByteArray = _middleBuffer; 1194 buffer.writeBoolean(value); 1195 writeBytes(buffer); 1196 buffer.length = 0; 1228 1197 } 1229 1198 1230 1199 public function writeByte(value:int):void 1231 1200 { 1232 var b ytes:ByteArray = new ByteArray();1233 b ytes.endian = _endian;1234 bytes.writeByte(value);1235 writeBytes(bytes);1201 var buffer:ByteArray = _middleBuffer; 1202 buffer.writeByte(value); 1203 writeBytes(buffer); 1204 buffer.length = 0; 1236 1205 } 1237 1206 1238 1207 public function writeDouble(value:Number):void 1239 1208 { 1240 var b ytes:ByteArray = new ByteArray();1241 b ytes.endian = _endian;1242 bytes.writeDouble(value);1243 writeBytes(bytes);1209 var buffer:ByteArray = _middleBuffer; 1210 buffer.writeDouble(value); 1211 writeBytes(buffer); 1212 buffer.length = 0; 1244 1213 } 1245 1214 1246 1215 public function writeFloat(value:Number):void 1247 1216 { 1248 var b ytes:ByteArray = new ByteArray();1249 b ytes.endian = _endian;1250 bytes.writeFloat(value);1251 writeBytes(bytes);1217 var buffer:ByteArray = _middleBuffer; 1218 buffer.writeFloat(value); 1219 writeBytes(buffer); 1220 buffer.length = 0; 1252 1221 } 1253 1222 1254 1223 public function writeInt(value:int):void 1255 1224 { 1256 var b ytes:ByteArray = new ByteArray();1257 b ytes.endian = _endian;1258 bytes.writeInt(value);1259 writeBytes(bytes);1225 var buffer:ByteArray = _middleBuffer; 1226 buffer.writeInt(value); 1227 writeBytes(buffer); 1228 buffer.length = 0; 1260 1229 } 1261 1230 1262 1231 public function writeMultiByte(value:String, charSet:String):void 1263 1232 { 1264 var b ytes:ByteArray = new ByteArray();1265 b ytes.endian = _endian;1266 bytes.writeMultiByte(value, charSet);1267 writeBytes(bytes);1233 var buffer:ByteArray = _middleBuffer; 1234 buffer.writeMultiByte(value, charSet); 1235 writeBytes(buffer); 1236 buffer.length = 0; 1268 1237 } 1269 1238 1270 1239 public function writeObject(object:*):void 1271 1240 { 1272 var bytes:ByteArray = new ByteArray(); 1273 bytes.endian = _endian; 1274 bytes.objectEncoding = _objectEncoding; 1275 bytes.writeObject(object); 1276 writeBytes(bytes); 1241 var buffer:ByteArray = _middleBuffer; 1242 buffer.writeObject(object); 1243 writeBytes(buffer); 1244 buffer.length = 0; 1277 1245 } 1278 1246 1279 1247 public function writeShort(value:int):void 1280 1248 { 1281 var b ytes:ByteArray = new ByteArray();1282 b ytes.endian = _endian;1283 bytes.writeShort(value);1284 writeBytes(bytes);1249 var buffer:ByteArray = _middleBuffer; 1250 buffer.writeShort(value); 1251 writeBytes(buffer); 1252 buffer.length = 0; 1285 1253 } 1286 1254 1287 1255 public function writeUnsignedInt(value:uint):void 1288 1256 { 1289 var b ytes:ByteArray = new ByteArray();1290 b ytes.endian = _endian;1291 bytes.writeUnsignedInt(value);1292 writeBytes(bytes);1257 var buffer:ByteArray = _middleBuffer; 1258 buffer.writeUnsignedInt(value); 1259 writeBytes(buffer); 1260 buffer.length = 0; 1293 1261 } 1294 1262 1295 1263 public function writeUTF(value:String):void 1296 1264 { 1297 var b ytes:ByteArray = new ByteArray();1298 b ytes.endian = _endian;1299 bytes.writeUTF(value);1300 writeBytes(bytes);1265 var buffer:ByteArray = _middleBuffer; 1266 buffer.writeUTF(value); 1267 writeBytes(buffer); 1268 buffer.length = 0; 1301 1269 } 1302 1270 1303 1271 public function writeUTFBytes(value:String):void 1304 1272 { 1305 var b ytes:ByteArray = new ByteArray();1306 b ytes.endian = _endian;1307 bytes.writeUTFBytes(value);1308 writeBytes(bytes);1273 var buffer:ByteArray = _middleBuffer; 1274 buffer.writeUTFBytes(value); 1275 writeBytes(buffer); 1276 buffer.length = 0; 1309 1277 } 1310 1278 -
lang/actionscript/FxTerm/trunk/FxTermTest.mxml
r7117 r7124 1 1 <?xml version="1.0" encoding="utf-8"?> 2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" layout="vertical" xmlns="*"> 2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 3 creationComplete="init()" layout="vertical" xmlns="*" 4 > 5 <mx:Style> 6 Application { 7 backgroundColor: #999999; 8 } 9 .terminalPanel { 10 color: #0f0f0f; 11 backgroundColor: #000000; 12 } 13 .terminalControlLink { 14 color: #606060; 15 disabledColor: #0f0f0f; 16 selectionColor: #000000; 17 separatorColor: #CCCCC4; 18 } 19 </mx:Style> 3 20 <mx:Script> 4 import FxTerm; 5 import Terminal; 21 <![CDATA[ 6 22 import mx.controls.*; 7 23 import mx.containers.*; 8 private var term:FxTerm; 24 import flash.events.*; 25 import mx.events.*; 26 import mx.core.*; 9 27 private function init():void 10 28 { 29 terminal.addEventListener(KeyboardEvent.KEY_DOWN, keyboardHandler); 30 terminal.addEventListener(ResizeEvent.RESIZE, terminalResizedHandler); 31 commandLine.tabEnabled = false; 32 for each(var c:UIComponent in terminalControlLink.getChildren()) { 33 c.tabEnabled = false; 34 } 11 35 } 36 37 private function keyboardHandler(event:KeyboardEvent):void 38 { 39 var code:uint = event.charCode; 40 if(code == 0) { return; } 41 telnet.writeByte(code); 42 } 43 44 private function terminalResizedHandler(event:ResizeEvent):void 45 { 46 trace("resized"); 47 } 48 49 private var hasCommandLineInput:Boolean = false; 50 private function onCommandLineInput(event:TextEvent):void 51 { 52 hasCommandLineInput = true; // 漢字変換中は送信しない 53 // http://aoi-project.com/pblog/article.php?id=11 54 } 55 private function onCommandLineKeyUp(event:KeyboardEvent):void { 56 if (event.keyCode == Keyboard.ENTER && 57 hasCommandLineInput == false) { 58 trace(commandLine.text); 59 if(commandLine.text.length == 0) { 60 // send enter key 61 telnet.writeByte(Keyboard.ENTER); 62 } else { 63 // send text 64 telnet.writeUTFBytes(commandLine.text); 65 commandLine.text = ""; 66 } 67 } 68 hasCommandLineInput = false; 69 } 70 ]]> 12 71 </mx:Script> 13 <!--<mx:Script source="FxTerm.as" />--> 14 <!--<mx:Style source="FxTerm.css" />--> 15 <Terminal id="kk" col="80" row="20"/> 16 <!--<mx:HBox id="pa" />--> 17 <!--<mx:VBox id="term" width="800" height="400"/>--> 18 <!--<mx:Text id="term" width="800" height="400"/>--> 19 <!--<mx:TextArea id="log" width="800" height="100%"/>--> 72 <TelnetService id="telnet" host="localhost" port="5000"/> 73 <mx:Panel id="terminalPanel" styleName="terminalPanel"> 74 <Terminal id="terminal" col="80" row="20" dataProvider="{telnet}"/> 75 <mx:ControlBar><mx:VBox width="100%" horizontalAlign="center"> 76 <mx:LinkBar id="terminalControlLink" styleName="terminalControlLink" 77 dataProvider="terminalControl"/> 78 <mx:ViewStack id="terminalControl" width="100%" minWidth="400"> 79 <mx:HBox label="Command" width="100%"> 80 <mx:TextInput width="100%" id="commandLine" 81 textInput="onCommandLineInput(event)" 82 keyUp="onCommandLineKeyUp(event)" 83 /> 84 </mx:HBox> 85 <mx:HBox label="Size" horizontalAlign="center"> 86 <mx:Label text="Colmuns" styleName="terminalControlLabel"/> 87 <mx:NumericStepper value="{terminal.col}" minimum="1" maximum="400" id="colmunStepper" 88 change="terminal.col=colmunStepper.value" 89 styleName="terminalControl"/> 90 <mx:Spacer width="10"/> 91 <mx:Label text="Rows" styleName="terminalControlLabel"/> 92 <mx:NumericStepper value="{terminal.row}" minimum="1" maximum="40" id="rowStepper" 93 change="terminal.row=rowStepper.value" 94 styleName="terminalControl"/> 95 <mx:Spacer width="10"/> 96 <mx:Label text="Font size" styleName="terminalControlLabel"/> 97 <mx:NumericStepper value="{terminal.fontSize}" minimum="1" maximum="40" id="fontSizeStepper" 98 change="terminal.fontSize=fontSizeStepper.value" 99 styleName="terminalControl"/> 100 </mx:HBox> 101 <mx:HBox label="Color" width="100%"> 102 </mx:HBox> 103 </mx:ViewStack> 104 </mx:VBox></mx:ControlBar> 105 </mx:Panel> 20 106 </mx:Application> 21 107 -
lang/actionscript/FxTerm/trunk/Telnet.as
r7117 r7124 1 1 package 2 2 { 3 import flash.events.*; 4 import flash.net.Socket; 5 import flash.utils.ByteArray; 6 import flash.system.Security; 7 8 //import mx.controls.*; 9 10 public class FxTelnet implements IEventDispatcher { 11 private var socket:Socket; 12 private var eventDispatcher:EventDispatcher; 13 14 public var buffer:ByteArray; 15 private var state:int = 0; 16 private var is_active:Boolean = false; 17 18 public function get bytesAvailable():uint { 19 return buffer.length; 20 } 21 22 public function FxTelnet() { 23 eventDispatcher = new EventDispatcher(this); 24 buffer = new ByteArray(); 25 socket = new Socket(); 26 socket.addEventListener(Event.CONNECT, connectHandler); 27 socket.addEventListener(Event.CLOSE, closeHandler); 28 socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 29 socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler); 30 socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); 31 } 32 33 public function connect(server:String, port:int):void { 34 // Load policy file from remote server. 35 //Security.loadPolicyFile("http://" + serverURL + "/crossdomain.xml"); 36 //Security.loadPolicyFile("http://xcore.local:9200/crossdomain.xml"); 37 try { 38 msg("Trying to connect to " + server + ":" + port + "\n"); 39 socket.connect(server, port); 40 is_active = true; 41 } catch (error:Error) { 42 msg(error.message + "\n"); 43 socket.close(); 44 } 45 } 46 47 public function readMultiByte(charSet:String):String { 48 var buflen:uint = buffer.length; 49 if( buflen == 0 ) { return ""; } 50 buffer.position = 0; 51 var value:String = buffer.readMultiByte(buflen, charSet); 52 buffer.length = 0; 53 return value; 54 } 55 public function readBytes(bytes:ByteArray, offset:uint = 0):void { 56 buffer.position = 0; 57 buffer.readBytes(bytes, offset, 0); 58 buffer.length = 0; 59 } 60 public function writeByte(value:int):void 61 { 62 if( !is_active ) { return; } 63 socket.writeByte(value); 64 } 65 public function writeMultibyte(value:String):void { 66 if( !is_active ) { return; } 67 // 68 } 69 public function writeBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0):void { 70 if( !is_active ) { return; } 71 // 72 } 73 74 private function connectHandler(event:Event):void { 75 if (socket.connected) { 76 msg("connected...\n"); 77 } else { 78 msg("unable to connect\n"); 79 } 80 } 81 private function closeHandler(event:Event):void { 82 msg("closed...\n"); 83 is_active = false; 84 } 85 private function ioErrorHandler(event:IOErrorEvent):void { 86 msg("Unable to connect: socket error.\n"); 87 is_active = false; 88 } 89 private function securityErrorHandler(event:SecurityErrorEvent):void { 90 //msg(event.text + "\n"); 91 is_active = false; 92 //Alert.show(event.text); 93 } 94 95 private const WILL:int = 0xFB; // 251 - WILL (option code) 96 private const WONT:int = 0xFC; // 252 - WON'T (option code) 97 private const DO:int = 0xFD; // 253 - DO (option code) 98 private const DONT:int = 0xFE; // 254 - DON'T (option code) 99 private const IAC:int = 0xFF; // 255 - Interpret as Command (IAC) 100 private function socketDataHandler(ev:ProgressEvent):void { 101 var n:int = socket.bytesAvailable; 102 // Loop through each available byte returned from the socket connection. 103 while (--n >= 0) { 104 // Read next available byte. 105 var b:int = socket.readUnsignedByte(); 106 switch (state) { 107 case 0: 108 // If the current byte is the "Interpret as Command" code, set the state to 1. 109 if (b == IAC) { 110 state = 1; 111 // Else display the character using the msg() method. 112 } else { 113 buffer.writeByte(b); 114 } 115 break; 116 case 1: 117 // If the current byte is the "DO" code, set the state to 2. 118 if (b == DO) { 119 state = 2; 120 } else { 121 state = 0; 122 } 123 break; 124 // Blindly reject the option. 125 case 2: 126 /* 127 Write the "Interpret as Command" code, "WONT" code, 128 and current byte to the socket and send the contents 129 to the server by calling the flush() method. 130 */ 131 socket.writeByte(IAC); 132 socket.writeByte(WONT); 133 socket.writeByte(b); 134 socket.flush(); 135 state = 0; 136 break; 137 } 138 } 139 var event:Event = new ProgressEvent( 140 ProgressEvent.SOCKET_DATA, 3 4 import flash.events.*; 5 import flash.net.Socket; 6 import flash.utils.ByteArray; 7 import flash.utils.IDataOutput; 8 9 public class Telnet extends EventDispatcher implements IDataOutput 10 { 11 private var _socket:Socket; 12 private var _buffer:ByteArray; 13 private var _state:int = 0; 14 private var _active:Boolean = false; 15 16 private static const WILL:int = 0xFB; // 251 - WILL (option code) 17 private static const WONT:int = 0xFC; // 252 - WON'T (option code) 18 private static const DO:int = 0xFD; // 253 - DO (option code) 19 private static const DONT:int = 0xFE; // 254 - DON'T (option code) 20 private static const IAC:int = 0xFF; // 255 - Interpret as Command (IAC) 21 22 // IDataOutput interface 23 private var _middleBuffer:ByteArray; 24 25 public function Telnet() 26 { 27 _buffer = new ByteArray(); 28 _middleBuffer = new ByteArray(); 29 var socket:Socket = _socket = new Socket(); 30 socket.addEventListener(Event.CONNECT, connectHandler); 31 socket.addEventListener(Event.CLOSE, closeHandler); 32 socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 33 socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler); 34 socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); 35 } 36 37 public function connect(host:String, port:int):void 38 { 39 try { 40 _socket.connect(host, port); 41 _active = true; 42 } catch (error:Error) { 43 _socket.close(); 44 var event:IOErrorEvent = new IOErrorEvent( 45 IOErrorEvent.IO_ERROR, 141 46 false, 142 47 false, 143 buffer.length, 144 0 48 "failed connect to " + host + ":" + port 145 49 ); 146 50 dispatchEvent(event); 147 51 } 148 149 private function msg(value:String):void { 150 buffer.writeUTFBytes(value); 151 var event:Event = new ProgressEvent( 152 ProgressEvent.SOCKET_DATA, 153 false, 154 false, 155 buffer.length, 156 0 157 ); 158 dispatchEvent(event); 52 } 53 54 public function close():void 55 { 56 _socket.close(); 57 _active = false; 58 } 59 60 public function get bytesAvailable():uint 61 { 62 return _buffer.length; 63 } 64 65 public function readBytes(bytes:ByteArray, offset:uint = 0):void 66 { 67 var buffer:ByteArray = _buffer; 68 buffer.position = 0; 69 buffer.readBytes(bytes, offset, 0); 70 buffer.length = 0; 71 } 72 73 74 public function writeByte(value:int):void 75 { 76 if( !_active ) { return; } 77 if( value == IAC ) { 78 _socket.writeByte(value); 79 _socket.writeByte(value); 80 } else { 81 _socket.writeByte(value); 159 82 } 160 161 162 163 // delegate EventDispatcher 164 public function addEventListener(type:String, 165 listener:Function, 166 useCapture:Boolean = false, 167 priority:int = 0, 168 useWeakReference:Boolean = false):void 169 { 170 eventDispatcher.addEventListener(type, listener, useCapture, 171 priority, useWeakReference); 83 } 84 85 public function writeBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0):void { 86 if( !_active ) { return; } 87 _socket.writeBytes(bytes, offset, length); 88 // FIXME convert IAC to IAC IAC 89 } 90 91 private function connectHandler(event:Event):void 92 { 93 dispatchEvent(event); 94 } 95 96 private function closeHandler(event:Event):void 97 { 98 _active = false; 99 dispatchEvent(event); 100 } 101 102 private function ioErrorHandler(event:IOErrorEvent):void 103 { 104 _active = false; 105 dispatchEvent(event); 106 } 107 108 private function securityErrorHandler(event:SecurityErrorEvent):void 109 { 110 _active = false; 111 dispatchEvent(event); 112 } 113 114 private function socketDataHandler(ev:ProgressEvent):void 115 { 116 var socket:Socket = _socket; 117 var buffer:ByteArray = _buffer; 118 var beforeLength:uint = _buffer.length; 119 var state:int = _state; 120 var n:int = socket.bytesAvailable; 121 while (--n >= 0) { 122 var b:int = socket.readUnsignedByte(); 123 switch (state) { 124 case 0: 125 if (b == IAC) { 126 state = 1; 127 } else { 128 buffer.writeByte(b); 129 } 130 break; 131 case 1: 132 if (b == DO) { 133 state = 2; 134 } else { 135 state = 0; 136 } 137 break; 138 case 2: 139 socket.writeByte(IAC); 140 socket.writeByte(WONT); 141 socket.writeByte(b); 142 socket.flush(); 143 state = 0; 144 break; 145 } 172 146 } 173 174 public function removeEventListener(type:String, 175 listener:Function, 176 useCapture:Boolean = false):void 177 { 178 eventDispatcher.removeEventListener(type, listener, useCapture); 179 } 180 181 public function dispatchEvent(event:Event):Boolean 182 { 183 return eventDispatcher.dispatchEvent(event); 184 } 185 186 public function hasEventListener(type:String):Boolean 187 { 188 return eventDispatcher.hasEventListener(type); 189 } 190 191 192 public function willTrigger(type:String):Boolean 193 { 194 return eventDispatcher.willTrigger(type); 195 } 147 _state = state; 148 var event:Event = new ProgressEvent( 149 ProgressEvent.PROGRESS, 150 false, 151 false, 152 buffer.length - beforeLength, 153 buffer.length 154 ); 155 dispatchEvent(event); 156 } 157 158 159 /** 160 * IDataOutput interface 161 */ 162 public function set objectEncoding(value:uint):void 163 { 164 _middleBuffer.objectEncoding = value; 165 } 166 public function get objectEncoding():uint 167 { 168 return _middleBuffer.objectEncoding; 169 } 170 171 public function set endian(value:String):void 172 { 173 _middleBuffer.endian = value; 174 } 175 public function get endian():String 176 { 177 return _middleBuffer.endian; 178 } 179 180 public function writeBoolean(value:Boolean):void 181 { 182 var buffer:ByteArray = _middleBuffer; 183 buffer.writeBoolean(value); 184 writeBytes(buffer); 185 buffer.length = 0; 186 } 187 188 public function writeDouble(value:Number):void 189 { 190 var buffer:ByteArray = _middleBuffer; 191 buffer.writeDouble(value); 192 writeBytes(buffer); 193 buffer.length = 0; 194 } 195 196 public function writeFloat(value:Number):void 197 { 198 var buffer:ByteArray = _middleBuffer; 199 buffer.writeFloat(value); 200 writeBytes(buffer); 201 buffer.length = 0; 202 } 203 204 public function writeInt(value:int):void 205 { 206 var buffer:ByteArray = _middleBuffer; 207 buffer.writeInt(value); 208 writeBytes(buffer); 209 buffer.length = 0; 210 } 211 212 public function writeMultiByte(value:String, charSet:String):void 213 { 214 var buffer:ByteArray = _middleBuffer; 215 buffer.writeMultiByte(value, charSet); 216 writeBytes(buffer); 217 buffer.length = 0; 218 } 219 220 public function writeObject(object:*):void 221 { 222 var buffer:ByteArray = _middleBuffer; 223 buffer.writeObject(object); 224 writeBytes(buffer); 225 buffer.length = 0; 226 } 227 228 public function writeShort(value:int):void 229 { 230 var buffer:ByteArray = _middleBuffer; 231 buffer.writeShort(value); 232 writeBytes(buffer); 233 buffer.length = 0; 234 } 235 236 public function writeUnsignedInt(value:uint):void 237 { 238 var buffer:ByteArray = _middleBuffer; 239 buffer.writeUnsignedInt(value); 240 writeBytes(buffer); 241 buffer.length = 0; 242 } 243 244 public function writeUTF(value:String):void 245 { 246 var buffer:ByteArray = _middleBuffer; 247 buffer.writeUTF(value); 248 writeBytes(buffer); 249 buffer.length = 0; 250 } 251 252 public function writeUTFBytes(value:String):void 253 { 254 var buffer:ByteArray = _middleBuffer; 255 buffer.writeUTFBytes(value); 256 writeBytes(buffer); 257 buffer.length = 0; 196 258 } 197 259 } 260 261 } -
lang/actionscript/FxTerm/trunk/Terminal.as
r7117 r7124 3 3 4 4 import flash.utils.ByteArray; 5 import mx.core.UIComponent;6 5 import flash.utils.IDataOutput; 7 6 import flash.events.Event; 8 import TerminalEvent; 7 import flash.events.ProgressEvent; 8 import flash.text.TextFormat; 9 10 import mx.core.UIComponent; 11 import mx.events.*; 9 12 10 13 public class Terminal extends UIComponent implements IDataOutput 11 14 { 12 private var term:FxTerm;15 private var _term:FxTerm; 13 16 private var _dataProvider:Object; 14 17 private var _buffer:ByteArray; … … 18 21 super(); 19 22 _buffer = new ByteArray(); 20 term = new FxTerm();21 this.width = term.width;22 this.height = term.height;23 addChild( term);23 _term = new FxTerm(); 24 this.width = _term.width; 25 this.height = _term.height; 26 addChild(_term); 24 27 } 25 28 26 29 public function set dataProvider(object:Object):void 27 30 { 28 _dataProvider.addEventListener(TerminalEvent.TERMINAL_DATA, dataHandler); 31 if(_dataProvider) { 32 _dataProvider.removeEventListner( 33 ProgressEvent.PROGRESS, dataHandler); 34 } 35 object.addEventListener(ProgressEvent.PROGRESS, dataHandler); 29 36 _dataProvider = object; 30 if(_dataProvider) {31 _dataProvider.removeEventListner(TerminalEvent.TERMINAL_DATA, dataHandler);32 }33 37 } 34 38 35 private function dataHandler(event: TerminalEvent):void39 private function dataHandler(event:ProgressEvent):void 36 40 { 37 41 var buffer:ByteArray = _buffer; … … 39 43 writeBytes(buffer); 40 44 refresh(); 45 buffer.length = 0; 41 46 } 42 47 … … 47 52 } 48 53 49 public function set fontSize(value:Object):void 50 { term.fontSize = value; } 54 private function resized():void 55 { 56 this.width = _term.width; 57 this.height = _term.height; 58 } 59 60 [Bindable] 61 public function get charSet():String 62 { return _term.charSet; } 63 public function set charSet(charset:String):void 64 { _term.charSet = charset; } 65 66 public function set textFormat(format:TextFormat):void 67 { _term.textFormat = format; resized(); } 68 69 [Bindable] 70 public function get fontSize():Number 71 { return Number(_term.fontSize); } 72 public function set fontSize(value:Number):void 73 { _term.fontSize = value as Object; resized(); } 74 51 75 public function set foregroundColors(color8:Array):void 52 { term.foregroundColors = color8; }76 { _term.foregroundColors = color8; } 53 77 public function set backgroundColors(color8:Array):void 54 { term.backgroundColors = color8; }78 { _term.backgroundColors = color8; } 55 79 public function setForegroundColor(n:uint, color:uint):uint 56 { return term.setForegroundColor(n, color); }80 { return _term.setForegroundColor(n, color); } 57 81 public function setBackgroundColor(n:uint, color:uint):uint 58 { return term.setBackgroundColor(n, color); }82 { return _term.setBackgroundColor(n, color); } 59 83 60 84 public function resize(cols:uint, rows:uint):void 61 { term.resize(cols, rows); changed(); } 85 { _term.resize(cols, rows); changed(); resized(); } 86 87 [Bindable] 88 public function get col():uint 89 { return _term.col; } 62 90 public function set col(n:uint):void 63 { term.col = n; changed(); } 91 { _term.col = n; changed(); resized(); } 92 93 [Bindable] 94 public function get row():uint 95 { return _term.row; } 64 96 public function set row(n:uint):void 65 { term.row = n; changed(); } 66 public function refresh():void 67 { term.refresh(); changed(); } 97 { _term.row = n; changed(); resized(); } 98 99 public function refresh(force:Boolean = true):void 100 { _term.refresh(force); changed(); } 68 101 69 102 public function set objectEncoding(value:uint):void 70 { term.objectEncoding = value; }103 { _term.objectEncoding = value; } 71 104 public function get objectEncoding():uint 72 { return term.objectEncoding } 105 { return _term.objectEncoding } 106 73 107 public function set endian(value:String):void 74 { term.endian = value; }108 { _term.endian = value; } 75 109 public function get endian():String 76 { return term.endian; } 110 { return _term.endian; } 111 77 112 public function writeBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0):void 78 { term.writeBytes(bytes, offset, length); }113 { _term.writeBytes(bytes, offset, length); } 79 114 public function writeBoolean(value:Boolean):void 80 { term.writeBoolean(value); }115 { _term.writeBoolean(value); } 81 116 public function writeByte(value:int):void 82 { term.writeByte(value); }117 { _term.writeByte(value); } 83 118 public function writeDouble(value:Number):void 84 { term.writeDouble(value); }119 { _term.writeDouble(value); } 85 120 public function writeFloat(value:Number):void 86 { term.writeFloat(value); }121 { _term.writeFloat(value); } 87 122 public function writeInt(value:int):void 88 { term.writeInt(value); }123 { _term.writeInt(value); } 89 124 public function writeMultiByte(value:String, charSet:String):void 90 { term.writeMultiByte(value, charSet); }125 { _term.writeMultiByte(value, charSet); } 91 126 public function writeObject(object:*):void 92 { term.writeObject(object); }127 { _term.writeObject(object); } 93 128 public function writeShort(value:int):void 94 { term.writeShort(value); }129 { _term.writeShort(value); } 95 130 public function writeUnsignedInt(value:uint):void 96 { term.writeUnsignedInt(value); }131 { _term.writeUnsignedInt(value); } 97 132 public function writeUTF(value:String):void 98 { term.writeUTF(value); }133 { _term.writeUTF(value); } 99 134 public function writeUTFBytes(value:String):void 100 { term.writeUTFBytes(value); }135 { _term.writeUTFBytes(value); } 101 136 } 102 137
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)