P S o c k e t . n e w h o s t , p o r t T C P S e r v e r . n e w h o s t , p o r t s o c k e t . r e a d s o c k e t . r e a d _ n o n b l o c k s o c k e t . w r i t e s o c k e t . w r i t e _ n o n b l o c k
= T C P S e r v e r . n e w ' l o c a l h o s t ' , 4 0 0 0 l o o p d o c = s e r v e r . a c c e p t c < < " H T T P / 1 . 1 2 0 0 O K C o n t e n t - L e n g t h : 1 2 H e l l o w o r l d ! " c . c l o s e e n d
v e n t = e p o l l ( t i m e o u t ) i f f r o m _ s e r v e r ? ( e v e n t ) c o n n e c t i o n = s e r v e r . a c c e p t e l s e c o n n e c t i o n = f i n d _ c o n n _ b y ( e v e n t ) e n d . . . # r e s u m e p r o c e s s i n g e n d
avoid infinite recursion when registering events in callback: c l o s e _ c o n n e c t i o n _ a f t e r _ w r i t i n g E M . n e x t _ t i c k { . . . }
_ w i t h _ f i b e r r e s u l t = ' ' w h i l e b u f f e r = r e a d _ n o n b l o c k r e s u l t < < b u f f e r i f n o t h i n g t o r e a d F i b e r . y i e l d : r e a d i n g ! e n d e n d r e s u l t e n d
e _ w i t h _ f i b e r c o n t e n t l o o p d o w r i t e _ n o n b l o c k c o n t e n t c o n t e n t = u n w r i t t e n p a r t o f c o n t e n t b r e a k i f c o n t e n t . e m p t y ? F i b e r . y i e l d : w r i t i n g ! e n d e n d
v e n t = e p o l l ( t i m e o u t ) i f f r o m _ s e r v e r ? ( e v e n t ) c o n n e c t i o n = s e r v e r . a c c e p t e l s e c o n n e c t i o n = f i n d _ c o n n _ b y ( e v e n t ) e n d . . . # r e s u m e p r o c e s s i n g e n d
t = e p o l l ( t i m e o u t ) i f f r o m _ s e r v e r ? ( e v e n t ) c o n n e c t i o n = s e r v e r . a c c e p t f i b e r = F i b e r . n e w { . . . r e a d _ w i t h _ f i b e r . . . w r i t e _ w i t h _ f i b e r } e l s e f i b e r = f i n d _ f i b e r _ b y ( e v e n t ) e n d f i b e r . r e s u m e e n d