@@ -33,29 +33,29 @@ function Connection() {
33
33
this . on ( "pipe" , function ( source ) {
34
34
that . stream = source ;
35
35
} ) ;
36
-
37
- var self = this ;
38
-
39
- Object . keys ( protocol . types ) . forEach ( function ( k ) {
40
- var v = protocol . types [ k ] ;
41
- self [ v ] = function shim_generator ( opts ) {
42
- var p = generate [ v ] ( opts ) ;
43
- if ( p instanceof Error ) {
44
- self . emit ( 'error' , p )
45
- } else {
46
- self . stream . write ( p ) ;
47
- }
48
- } ;
49
- } ) ;
50
36
} ;
51
-
52
37
util . inherits ( Connection , Writable ) ;
53
38
54
39
Connection . prototype . setPacketEncoding = function ( encoding ) {
55
40
this . _packetEncoding = encoding ;
56
41
return this ;
57
42
} ;
58
43
44
+ for ( var k in protocol . types ) {
45
+ var v = protocol . types [ k ] ;
46
+
47
+ var fun = "" +
48
+ " var p = this.generate." + v + "(opts); " +
49
+ " if (p instanceof Error) { " +
50
+ " this.emit('error', p) " +
51
+ " } else { " +
52
+ " this.stream.write(p); " +
53
+ " } "
54
+ " } " ;
55
+
56
+ Connection . prototype [ v ] = new Function ( "opts" , fun ) ;
57
+ }
58
+
59
59
Connection . prototype . _newPacket = function ( ) {
60
60
this . packet = { } ;
61
61
this . tmp = { pos : 1 , mul : 1 , length : 0 } ;
@@ -138,64 +138,26 @@ Connection.prototype._readPayload = function() {
138
138
} ;
139
139
140
140
( function ( ) {
141
+ var v = protocol . types [ k ] ;
142
+
143
+ var fun = "" +
144
+ " 'use strict'; \n" +
145
+ " var buf = this.data.slice(this.index, this.index + this.packet.length); \n" +
146
+ " var result = null; \n" +
147
+ " this.index += this.packet.length; \n" +
148
+ " switch(this.packet.cmd) { \n" ;
149
+
150
+ Object . keys ( parse ) . forEach ( function ( key ) {
151
+ fun = fun +
152
+ " case '" + key + "': \n" +
153
+ " result = parse." + key + "(buf, this.packet, this._packetEncoding); \n" +
154
+ " break; \n " ;
155
+ } ) ;
141
156
142
- Connection . prototype . _parsePayload = function ( ) {
143
- var buf = this . data . slice ( this . index , this . index + this . packet . length ) ;
144
- var result = null ;
145
- this . index += this . packet . length ;
146
- var cmd = this . packet . cmd ;
147
- switch ( cmd ) {
148
- case protocol . types [ 0 ] :
149
- result = parse [ cmd ] ( buf , this . packet , this . _packetEncoding ) ;
150
- break ;
151
- case protocol . types [ 1 ] :
152
- result = parse [ cmd ] ( buf , this . packet , this . _packetEncoding ) ;
153
- break ;
154
- case protocol . types [ 2 ] :
155
- result = parse [ cmd ] ( buf , this . packet , this . _packetEncoding ) ;
156
- break ;
157
- case protocol . types [ 3 ] :
158
- result = parse [ cmd ] ( buf , this . packet , this . _packetEncoding ) ;
159
- break ;
160
- case protocol . types [ 4 ] :
161
- result = parse [ cmd ] ( buf , this . packet , this . _packetEncoding ) ;
162
- break ;
163
- case protocol . types [ 5 ] :
164
- result = parse [ cmd ] ( buf , this . packet , this . _packetEncoding ) ;
165
- break ;
166
- case protocol . types [ 6 ] :
167
- result = parse [ cmd ] ( buf , this . packet , this . _packetEncoding ) ;
168
- break ;
169
- case protocol . types [ 7 ] :
170
- result = parse [ cmd ] ( buf , this . packet , this . _packetEncoding ) ;
171
- break ;
172
- case protocol . types [ 8 ] :
173
- result = parse [ cmd ] ( buf , this . packet , this . _packetEncoding ) ;
174
- break ;
175
- case protocol . types [ 9 ] :
176
- result = parse [ cmd ] ( buf , this . packet , this . _packetEncoding ) ;
177
- break ;
178
- case protocol . types [ 10 ] :
179
- result = parse [ cmd ] ( buf , this . packet , this . _packetEncoding ) ;
180
- break ;
181
- case protocol . types [ 11 ] :
182
- result = parse [ cmd ] ( buf , this . packet , this . _packetEncoding ) ;
183
- break ;
184
- case protocol . types [ 12 ] :
185
- result = parse [ cmd ] ( buf , this . packet , this . _packetEncoding ) ;
186
- break ;
187
- case protocol . types [ 13 ] :
188
- result = parse [ cmd ] ( buf , this . packet , this . _packetEncoding ) ;
189
- break ;
190
- case protocol . types [ 14 ] :
191
- result = parse [ cmd ] ( buf , this . packet , this . _packetEncoding ) ;
192
- break ;
193
- case protocol . types [ 15 ] :
194
- result = parse [ cmd ] ( buf , this . packet , this . _packetEncoding ) ;
195
- break ;
196
- }
197
- return result ;
198
- }
157
+ fun += "} \n" ;
158
+ fun += "return result; \n" ;
159
+
160
+ Connection . prototype . _parsePayload = new Function ( "parse" , fun ) ;
199
161
} ) ( ) ;
200
162
201
163
Connection . prototype . _write = function ( data , encoding , done ) {
0 commit comments