Unnamed Fossil Project

Check-in [5c2217113b]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:updated
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:5c2217113b2a60468791ccbec5447a80f76ad52b
User & Date: alexey@sudachen.name 2012-03-04 15:34:49
Context
2012-03-04
15:35
updated check-in: c5449f1ab4 user: alexey@sudachen.name tags: trunk
15:34
updated check-in: 5c2217113b user: alexey@sudachen.name tags: trunk
2012-03-03
09:29
httpx updated check-in: bce04069bb user: alexey@sudachen.name tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added asio.hc.

























































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412

/*

Copyright © 2010-2012, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    http://www.gnu.org/licenses/

Otherwise:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization of the copyright holder.

*/

#ifndef C_once_5F140392_CCAA_4FE1_88CB_DC84EE3B25A0
#define C_once_5F140392_CCAA_4FE1_88CB_DC84EE3B25A0

#ifdef _LIBYOYO
#define _YOYO_ASIO_BUILTIN
#endif

#include "yoyo.hc"

#ifdef __windoze
  #include <winsock.h>
  typedef SOCKET socket_t;
#else
  #include <errno.h>
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <netdb.h>
  #include <netinet/in.h>
  #include <arpa/inet.h>
  typedef int socket_t;
#endif

enum  
  {
    ASIO_COMPLETED    = 0x00000001,
    ASIO_CLOSED       = 0x00000002,
    ASIO_SYNCHRONOUSE = 0x00200000,
    ASIO_TIMER        = 0x00100000,
    ASIO_FAILED       = 0x8e000000,
    ASIO_INTERRAPTED  = 0x8f000000,
    ASIO_PENGING      = 0,
    ASIO_CONTINUE     = 0,
  
    ASIO_ST_SEND      = 1,
    ASIO_ST_RECV      = 2,
    ASIO_ST_NOTIFY    = 3,
    ASIO_ST_ACCEPT    = 4,
    
    ASIO_MAP_BASE     = 101,
  };
  
#define ASIO_SUCCEEDED(State) (!((State)&0xff000000))

int (asio_recv_callback_t)(void *obj,int status,int count);
int (asio_send_callback_t)(void *obj,int status,int count);
int (asio_accept_callback_t)(void *obj,int status,socket_t fd,struct sockaddr *addr);
int (asio_notify_callback_t)(void *obj,int status);
int (asio_any_callback_t)(void *obj,int status,...);

typedef struct _YOYO_ASIO_STATE
  {
    socket_t fd;
    longptr_t accum;
    int fdsno,count,mincount;
    struct sockaddr addr;
    void *obj, *dta;
    asio_any_callback_t cbk;
    int (*perform)(struct _YOYO_ASIO_STATE *st);
    YOYO_ASIO_STATE *next;
  } YOYO_ASIO_STATE;

#ifdef _YOYO_ASIO_BUILTIN
static int Asio_St_Count = 0;
static YOYO_ASIO_STATE *Asio_St_Map[ASIO_MAP_BASE] = {0};
static YOYO_ASIO_STATE *Asio_St_Pool = 0;
static int Asio_Is_Init = 0;
#endif
  ;
  
YOYO_ASIO_STATE **Asio_Map_Fd(socket_t fd)
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE **st = Asio_St_Map[fd/ASIO_MAP_BASE];
    while ( *st && (*st)->fd != fd ) st = &(*st)->next;
    return st;
  }
#endif
  ;
  
YOYO_ASIO_STATE *Asio_Pool_Get_State()
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st = Asio_St_Pool;
    if ( !Asio_St_Pool )
      {
        Asio_St_Pool = __Malloc_Npl(sizeof(YOYO_ASIO_STATE));
        st = Asio_St_Pool;
      }
    Asio_St_Pool = Asio_St_Pool->next;
    memset(st,0,sizeof(YOYO_ASIO_STATE));
    return st;
  }
#endif
  ;
  
void Asio_Release_State(YOYO_ASIO_STATE **pst)
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st = *pst;
    *pst = st->next;
    st->next = Asio_St_Pool;
    Asio_St_Pool = st;
    --Asio_St_Count;
  }
#endif
  ;
  
int Asio_Status_Repeat_Or_Die()
#ifdef _YOYO_ASIO_BUILTIN
  {
  #ifdef __windoze
    int e = WSAGetLastError();
    if ( WSAEWOULDBLOCK == e || WSAEINPROGRESS == e )
  #else
    if ( EWOULDBLOCK == errno || EINPROGRESS == errno )
  #endif
      return ASIO_PENGING;
    return ASIO_FAILED;
  }
#endif
  ;
  
int Asio_Do_Accept(YOYO_ASIO_STATE *st)
#ifdef _YOYO_ASIO_BUILTIN
  {
    int len = sizeof(st->addr);
    int e = accept(st->fd,&st->addr,&len);
    if ( e < 0 ) 
      return Asio_Status_Repeat_Or_Die()
    st->accum = e;
    return ASIO_COMPLETED;
  }
#endif  
  ;
  
int Asio_Do_Recv(YOYO_ASIO_STATE *st)
#ifdef _YOYO_ASIO_BUILTIN
  {
    int e = recv(st->fd,(char*)st->dta+st->accum,st->count-st->accum,0);
    if ( e < 0 || (!e && st->accum < st->mincount) )
      return Asio_Status_Repeat_Or_Die()
    if ( !e && st->count )
      return ASIO_CLOSED;
    st->accum += e;
    return st->accum >= st->mincount ? ASIO_COMPLETED : ASIO_PENGING;  
  }
#endif
  ;
  
int Asio_Do_Send(YOYO_ASIO_STATE *st)
#ifdef _YOYO_ASIO_BUILTIN
  {
    int e = send(st->fd,(char*)st->dta+st->accum,st->count-st->accum,0);
    if ( e <= 0 ) 
      return Asio_Status_Repeat_Or_Die()
    st->accum += e;
    return st->accum >= st->mincount ? ASIO_COMPLETED : ASIO_PENGING;  
  }
#endif
  ;
  
void Asio_Perform_Interrupt()
#ifdef _YOYO_ASIO_BUILTIN
  {
    int i;
    for ( i = 0; i < ASIO_MAP_BASE; ++i )
      {
        YOYO_ASIO_STATE *pst = &Asio_St_Map[i];
        while ( *pst )
          {
            __Auto_Release
              {
                YOYO_ASIO_STATE st = **pst;
                void *obj = __Pool_Ptr(st.obj,Yo_Unrefe);
                Asio_Release_State(pst);
                
                if ( st.cbk ) 
                  st.cbk(obj,status,st.accum,&st.addr);
              }
          }
      }
  }
#endif
  ;
  
void Asio_At_Exit()
#ifdef _YOYO_ASIO_BUILTIN
  {
    Asio_Perform_Interrupt();
    while ( Asio_St_Pool ) 
      {
        YOYO_ASIO_STATE * st = Asio_St_Pool;
        Asio_St_Pool = st->next;
        free(st);
      }
  }
#endif
  ;
  
YOYO_ASIO_STATE *Asio_Alloc_State(socket_t fd, int tag)
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st;
    YOYO_ASIO_STATE **pst = Asio_Map_Fd(fd);
    
    if ( !Asio_Is_Init )
      {
        atexit(Asio_At_Exit);
      }
    
    if ( *pst ) 
      __Raise_Format(YOYO_ERROR_ALREADY_EXISTS,(__yoTa("ASIO duplicate descriptor: %d",0),fd));
    *pst = st = Asio_Pool_Get_State();

    switch ( tag )
      {
        case ASIO_ST_RECV:
          st->perform = Asio_Do_Recv; 
          break;
        case ASIO_ST_ACCEPT:
          st->perform = Asio_Do_Accept; 
          break;
        case ASIO_ST_SEND:    
          st->fdsno = 1; 
          st->perform = Asio_Do_Send; 
          break;
        case ASIO_ST_NOTIFY:  
          break;
      }
      
    ++Asio_St_Count;
    return st;
  }
#endif
  ;
  
void Asio_Complete(socket_t fd, int failed)
#ifdef _YOYO_ASIO_BUILTIN
  {
    __Auto_Release
      {
        YOYO_ASIO_STATE **pst = Asio_Map_Fd(fd);
        if ( *pst )
          {
            int status = failed ? ASIO_FAILED 
                         : (*pst)->perform ? (*pst)->perform(*pst) 
                         : ASIO_COMPLETED;
            
            if ( status != ASIO_PENGING )
              {
                YOYO_ASIO_STATE st = **pst;
                void *obj     = __Pool_Ptr(st.obj,Yo_Unrefe);
                Asio_Release_State(pst);
                if ( st.cbk ) st.cbk(obj,status,st.accum);
              }
          }
      } 
  }
#endif
  ;
  
int Asio_Recv(socket_t fd, void *dta, int count, int mincount, void *obj, asio_recv_callback_t callback)
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st = Asio_Alloc_State(fd,ASIO_ST_RECV);
    st->obj      = __Refe(obj);
    st->cbk      = (asio_any_callback_t)callback;
    st->count    = count;
    st->mincount = mincount;
    st->dta      = dta;
  }
#endif
  ;
    
int Asio_Send(socket_t fd, void *dta, int count, void *obj, asio_send_callback_t callback)
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st = Asio_Alloc_State(fd,ASIO_ST_SEND);
    st->obj      = __Refe(obj);
    st->cbk      = (asio_any_callback_t)callback;
    st->count    = count;
    st->mincount = count;
    st->dta      = dta;
  }
#endif
  ;
    
int Asio_Notify(socket_t fd, void *obj, asio_notify_callback_t callback)
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st = Asio_Alloc_State(fd,ASIO_ST_NOTIFY);
    st->obj      = __Refe(obj);
    st->cbk      = (asio_any_callback_t)callback;
  }
#endif
  ;
  
int Asio_Accept(socket_t fd, char *host, int port, void *obj, asio_accept_callback_t callback)
#ifdef _YOYO_ASIO_BUILTIN
  {
    YOYO_ASIO_STATE *st = Asio_Alloc_State(fd,ASIO_ST_ACCEPT);
    st->obj      = __Refe(obj);
    st->cbk      = (asio_any_callback_t)callback;
  }
#endif
  ;
  
int Asio_Perform_IO(int ms, int maxms)
#ifdef _YOYO_ASIO_BUILTIN
  {
    int i,j;
    timeval timeout = { ms/1000, ms*1000 };
    quad_t start    = maxms?System_Millis():0;

  #ifdef __windoze

    fd_set fds[3] = { {0, {-1}}, {0, {-1}}, {0, {-1}} };
    for ( i = 0; i < ASIO_MAP_BASE; ++i )
      {
        YOYO_ASIO_STATE *st = Asio_St_Map[i];
        while ( st )
          {
            fd_set *q = fds[st->fdsno];
            if ( fds[2].fd_count < FD_SETSIZE )
              {
                q->fd_array[q->fd_count] = st->fd;
                ++q->fd_count; 
                fds[2].fd_array[fds[2].fd_count] = st->fd;
                ++fds[2].fd_count; 
              }
            else break;
            st = st->next;
          }
      }
    
    i = select(Yo_MIN(FD_SETSIZE,Asio_St_Count),&fds[0],&fds[1],&fds[2],&timeout);

    if ( i < 0 )
      __Raise_Format(YOYO_ERROR_IO,(__yoTa("ASIO select failed: %d",0),i));

    for ( i = 2; i >= 0; --i )
      for ( j = 0; j < fds[i].fd_count; ++j )
        {
          Asio_Complete(fds[i].fd_array[j],i==2);
          if ( maxms && ( maxms < (System_Millis() - start) ) )
            goto l;
        }

  #else
  #error "not implemented!"
  #endif

  l:      
    return Asio_St_Count;
  }
#endif
  ;
  
int Asio_Perform_Timer(int interval)
#ifdef _YOYO_ASIO_BUILTIN
  {
  }
#endif
  ;
  
#endif /* C_once_5F140392_CCAA_4FE1_88CB_DC84EE3B25A0 */

Changes to httpx.hc.

48
49
50
51
52
53
54
55
56
57
58
59
60
61









































62
63

















































































































































































































































































































































































64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
..
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241

#include "yoyo.hc"
#include "string.hc"
#include "buffer.hc"
#include "dicto.hc"
#include "file.hc"
#include "tcpip.hc"
#include "bio.hc"
//#include "ssl.hc"

#ifdef _LIBYOYO
#define _YOYO_HTTPX_BUILTIN
#endif










































enum
  {

















































































































































































































































































































































































    YOYO_HTTPX_DOING_NOTHING      = 0,
    YOYO_HTTPX_IS_PREPARING       = 1,
    YOYO_HTTPX_IS_RESOLVING       = 2,
    YOYO_HTTPX_IS_CONNECTING      = 3,
    YOYO_HTTPX_IS_QUERYING        = 4,
    YOYO_HTTPX_IS_GETTING_STATUS  = 5,
    YOYO_HTTPX_IS_GETTING_HEADERS = 6,
    YOYO_HTTPX_IS_GETTING_CONTENT = 7,
    YOYO_HTTPX_IS_FINISHED        = 8,
    YOYO_HTTPX_IS_FAILED          = 9,
    YOYO_HTTPX_RESOLVING_ERROR    = 0x1001,
    YOYO_HTTPX_URLPARSING_ERROR   = 0x1002,
    YOYO_HTTPX_GETTING_ERROR      = 0x1003,
    
    YOYO_HTTPX_GET                = __FOUR_CHARS('G','E','T','_'),
    YOYO_HTTPX_PUT                = __FOUR_CHARS('P','U','T','_'),
    YOYO_HTTPX_POST               = __FOUR_CHARS('P','O','S','T'),
................................................................................

typedef struct _YOYO_HTTPX_NTFY
  {
    void (*progress)(struct _YOYO_HTTPX_NTFY *, int st, int count, int total);
    void (*error)(struct _YOYO_HTTPX_NTFY *, int st, char *msg);
  } YOYO_HTTPX_NTFY;

typedef struct _YOYO_URL 
  {
    char *host, *user, *passw, *query, *args, *anchor, *uri;
    int   port, proto;
  } YOYO_URL;

void YOYO_URL_Destruct(YOYO_URL *url)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    free(url->host);
    free(url->user);
    free(url->passw);
    free(url->query);
    free(url->args);
    free(url->anchor);
    free(url->uri);
    __Destruct(url);
  }
#endif
  ;
  
int Url_Proto(char *S)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    if ( !strcmp_I(S,"http")  )  return YOYO_URL_HTTP;
    if ( !strcmp_I(S,"https") )  return YOYO_URL_HTTPS;
    if ( !strcmp_I(S,"file")  )  return YOYO_URL_FILE;
    return YOYO_URL_UNKNOWN;
  }
#endif
  ;
  
YOYO_URL *Parse_Url(char *url)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    YOYO_URL *urlout = 0;
    __Auto_Ptr(urlout)
      {
        /* proto://user:passwd@host:port/query#anchor?args */
      
        char *p;
        char *pS = url;
        
        char *proto = 0;
        char *host  = 0;
        char *user  = 0;
        char *passw = 0;
        char *uri   = 0;
        char *args  = 0;
        char *query = 0;
        char *anchor= 0;
        int   port  = 0;
        
        p = pS;
        while ( *p && Isalpha(*p) ) ++p;
        
        if ( *p && *p == ':' && p[1] && p[1] == '/' && p[2] && p[2] == '/' )
          {
            proto = Str_Range(pS,p);
            pS = p+3;
          }
          
        p = pS;
        while ( *p && (Isalnum(*p) || *p == '.' || *p == '-' || *p == ':' ) ) ++p;
        
        if ( *p == '@' ) // user/password
          {
            char *q = pS;
            while ( *q != '@' && *q != ':' ) ++q;
            if ( *q == ':' ) 
              {
                user = Str_Range(pS,q);
                passw = Str_Range(q+1,p);
              }
            else
              user = Str_Range(pS,p);
            pS = p+1;
          }
          
        p = pS;
        while ( *p && (Isalnum(*p) || *p == '.' || *p == '-') ) ++p;
        
        if ( *p == ':' )
          {
            host = Str_Range(pS,p);
            pS = p+1; ++p;
            while ( *p && Isdigit(*p) ) ++p;
            if ( *p == '/' || !*p )
              { 
                port = strtol(pS,0,10); 
              }
            else
              __Raise(YOYO_ERROR_ILLFORMED,__yoTa("invalid port value",0));
            pS = p;
          }
        else if ( !*p || *p == '/' )
          {
            host = Str_Range(pS,p);
            pS = p;
          }
        
        uri = Str_Copy(pS,-1);  
        
        p = pS;
        while ( *p && *p != '?' && *p != '#' ) ++p;
        query = Str_Range(pS,p);

        if ( *p == '#' )
          {
            pS = ++p;
            while ( *p && *p != '?' ) ++p;
            anchor = Str_Range(pS,p);
          }
           
        if ( *p == '?' ) 
          {
            pS = ++p;
            while ( *p ) ++p;
            args = Str_Range(pS,p);
          }
                          
        urlout = __Object_Dtor(sizeof(YOYO_URL),YOYO_URL_Destruct);
        urlout->args  = __Retain(args);
        urlout->anchor= __Retain(anchor);
        urlout->query = __Retain(query);
        urlout->uri   = __Retain(uri);
        urlout->host  = __Retain(host);
        urlout->passw = __Retain(passw);
        urlout->user  = __Retain(user);
        urlout->port  = port;
        urlout->proto = Url_Proto(proto);
      }
      
    return urlout;
  }
#endif
  ;

typedef struct _YOYO_HTTPX
  {
    void *strm;
    YOYO_BIO *bio_in;
    YOYO_BIO *bio_out;
    







|
|





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|
|
|
|
|
|
|
|
|







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
...
501
502
503
504
505
506
507









































































































































508
509
510
511
512
513
514

#include "yoyo.hc"
#include "string.hc"
#include "buffer.hc"
#include "dicto.hc"
#include "file.hc"
#include "tcpip.hc"
#include "url.hc"
#include "asio.hc"

#ifdef _LIBYOYO
#define _YOYO_HTTPX_BUILTIN
#endif

/*
добавить отсылку на пост
добваить разбор хидеров ответа
добавить вызов колбек хендлера на финише
в колбек хендлер на терминирование и завершение добавить закрытие соеденения (тольео если не завершение с кипэлайвом)
*/

typedef int (*httpx_collback_t)(void *obj, struct _YOYO_HTTPX *httpx, int status);

typedef struct _YOYO_HTTPX
  {
    void *netchnl;
    YOYO_URL *url;
    YOYO_URL *proxy_url;
    
    int  method;
    int  maxcount;
    int  left;
    
    void *outstrm;
    void *poststrm;
    void *mon_obj;
    httpx_collback_t mon_callback;

    YOYO_BUFFER *bf;
    
    YOYO_DICTO  *hdrs;
    YOYO_BUFFER *content;
    int   streamed;
    int   status;
    char *status_text;
    
    int async:      1;
    int keepalive:  1;
    int legacy10:   1;
    int ssl:        1;
    int proxy:      1;
    int nline:      1;
        
  } YOYO_HTTPX;

enum 
  {
    HTTPX_ASYNC       = 1,
    HTTPX_KEEPALIVE   = 2,
    HTTPX_LEGACY_10   = 4,
    
    HTTPX_GET         = __FOUR_CHARS('G','E','T','_'),
    HTTPX_PUT         = __FOUR_CHARS('P','U','T','_'),
    HTTPX_POST        = __FOUR_CHARS('P','O','S','T'),
    HTTPX_HEAD        = __FOUR_CHARS('H','E','A','D'),
  
    HTTPX_BROKEN                  = 0x8e000000,
    HTTPX_FAILED                  = 0x8f000000,
    HTTPX_NOTIFY                  = 0x00001000,
    
    /* HTTPX_RESOLVED                = 0x00000f01, */
    HTTPX_PROXY_CONNECTED         = 0x00000f01,
    HTTPX_PROXY_AUTHORIZED        = 0x00000f02,
    HTTPX_SSL_CONNECTED           = 0x00000f03,
    HTTPX_CONNECTED               = 0x00000f10,
    HTTPX_REQUESTED               = 0x00000f20,
    HTTPX_GETTING_HEADERS         = 0x00000f30,
    HTTPX_RESPONDED               = 0x00000f41,
    HTTPX_GETTING_CONTENT         = 0x00000f42,
    HTTPX_FINISHED                = 0x00000f43,
    HTTPX_SEND                    = 0x00000e01,
    HTTPX_RECV                    = 0x00000e02,

    HTTPX_INVALID_RESPONSE        = 0x00000d01,
    HTTPX_INVALID_RESPONSE_CODE   = 0x00000d02,

    HTTPX_PENDING = ASIO_PENDING /* 0 */,
  };

#define HTTPX_SUCCEEDED(Status) (((Status)&0x8f000000) == 0)
#define HTTPX_STATUS(Status)    ((Status)&0x0fff)

int Httpx_Asio_Recv(YOYO_HTTPX *httpx, void *dta, int count, int mincount, asio_callback_t callback)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    if ( !httpx->ssl )
      return Tcp_Asio_Recv(httpx->netchnl,dta,count,mincount,httpx,callback);
  }
#endif
  ;
  
int Httpx_Asio_Send(YOYO_HTTPX *httpx, void *dta, int count, asio_callback_t callback)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    if ( !httpx->ssl )
      return Tcp_Asio_Send(httpx->netchnl,dta,count,httpx,callback);
  }
#endif
  ;
  
YOYO_HTTPX *Httpx_Client(char *url,int flags,void *mon_obj,httpx_collback_t mon_callback)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = __Object(sizeof(YOYO_HTTPX),funcs);
    httpx->url = __Retain(Url_Parse(url));
    
    if ( flags & HTTPX_ASYNC )
      httpx->async = 1;
    if ( flags & HTTPX_KEEPALIVE )
      httpx->keepalive = 1;
    if ( flags & HTTPX_LEGACY_10 )
      httpx->legacy10 = 1;
    
    if ( httpx->url->proto && httpx->url->proto != URLX_HTTP  )  
      __Raise(YOYO_ERROR_ILLFORMED,__yoTa("unknown HTTPX protocol requested",0));

    if ( !httpx->url->port ) 
      if ( !httpx->url->proto || httpx->url->proto == URLX_HTTP ) 
        httpx->url->port = 80;
        
    return httpx;
  }
#endif
  ;
  
int Httpx_Do_Callback(YOYO_HTTPX *httpx, int status)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    if ( httpx->mon_callback )
      return httpx->mon_callback(httpx->mon_obj,httpx,status);
    return status;
  }
#endif
  ;
  
int Httpx_Update_Until_EOH(YOYO_HTTPX *httpx, int count)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    YOYO_BUFFER *bf = httpx->bf;
    int i;
    char *q = bf->at + bf->count;
    bf->count += count;

    for ( i = 0; i < count; ++i )
      {
        if ( q[i] == '\r' && q[i+1] == '\n' )
          { if ( httpx->nline ) return bf->count-(count-i+2); httpx->nline = 1; ++i; } 
        else if ( q[i] == '\n' )
          { if ( httpx->nline ) return bf->count-(count-i+1);  httpx->nline = 1; } 
        ++i;
      }
      
    return 0;
  }      
#endif
  ;
  
void Httpx_Analyze_Headers(YOYO_HTTPX *httpx)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    int i;
    char *q, *Q, *S = httpx->bf->at;
    while (*S!='\n') ++S; /* skip HTTP/1.x line */
    for (;;)
      {
        q = ++S;
        while ( *q != '\n' ) ++q;
        *q = 0;
        for ( i = 1; q-i > S && Isspace(q[-i]); ++i ) q[-i] = 0;
        while (Isspace(*S)) ++S;
        if ( !*S ) break; /* empty line => end of headers*/ 
        Q = S;
        while ( *Q && *Q != ':' ) ++Q;
        if ( *Q == ':' )
          {
            *Q = 0;
            for ( i = 1; Q-i > S && Isspace(Q[-i]); ++i ) Q[-i] = 0;
            ++Q;
            while ( Isspace(*Q) ) ++Q;
            Dicto_Put(httpx->hdrs,S,(Q=Str_Copy_Npl(q,-1)));
          }
      }
  }
#endif
  ;
  
int Cbk_Httpx_Getting_Content(void *netchnl, void *_httpx, int status, byte_t *bytes, int count, int left)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    int L;
    YOYO_HTTPX *httpx = _httpx;
    YOYO_BUFFER *bf = httpx->bf;
    
    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN|HTTPX_GETTING_HEADERS);

    L = Yo_MIN(httpx->left,count);
    httpx->left -= L;
    httpx->streamed += L;
    Oj_Write(httpx->outstrm,bf->at,L,-1);

    if ( !httpx->left )
      return HTTPX_FINISHED;

    if ( status&ASIO_SYNCHRONOUSE )
      return HTTPX_CONTINUE;
    
    return Httpx_Asio_Recv(netchnl,bf->at,bf->capacity,Yo_MIN(httpx->left,bf->capacity)
                        ,httpx
                        ,Cbk_Httpx_Getting_Content);
  }
#endif
  ;
    
int Cbk_Httpx_Getting_Headers(void *netchnl, void *_httpx, int status, byte_t *bytes, int count, int left)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = _httpx;
    YOYO_BUFFER *bf = httpx->bf;
    int iterate = 0;
    
    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN|HTTPX_GETTING_HEADERS);
    
    if ( httpx->bf->count == 0 )
      {
        if ( !strcmp_I(bf->at,"HTTP/1.") )
          {
            char *q = bf->at + 8, *Q;
            bf->at[count] = 0;
            while ( *q && !Isspace(*q) ) ++q;
            while ( Isspace(*q) ) ++q;
            httpx->status = strtol(q,&q,10);
            if ( httpx->status < 100 || httpx->status >= 510 )
              return Httpx_Do_Callback(httpx,HTTPX_FAILED|HTTPX_INVALID_RESPONSE_CODE);    
            while ( *q != '\n' && *q != '\r' && Isspace(*q) ) ++q;
            Q = q;
            while ( *q && *q != '\n' && *q != '\r' && !Isspace(*q) ) ++q;
            httpx->status_text = Str_Range_Npl(Q,q);
            bf->count = (q - bf->at);         
            count -= bf->count; 
            httpx->nline = 0;
          }
        else
          return Httpx_Do_Callback(httpx,HTTPX_FAILED|HTTPX_INVALID_RESPONSE);
      }
    else
      iterate = status&ASIO_SYNCHRONOUSE;
      
    for(;;) 
      {
        int eoh = Httpx_Update_Until_EOH(httpx,count);

        if ( eoh )
          {
            char *foo;
            Httpx_Analyze_Headers(httpx);
            httpx->left = 0;
            
            if ( foo = Dicto_Get(httpx->hdrs,"Content-Length",0) )
              httpx->left = Yo_Minu(Str_To_Int(foo),httpx->maxcount);
              
            if ( httpx->outstrm && httpx->left != 0 )
              {
                int L = bf->count-eoh;
                L = Yo_MIN(httpx->left,L);
                httpx->left -= L;
                Oj_Write(httpx->outstrm,bf->at+eoh,L,-1);
                httpx->streamed += L;
                bf->count = 0;
                while ( httpx->left )
                  {
                    status = Httpx_Asio_Recv(netchnl,bf->at,bf->capacity,Yo_MIN(httpx->left,bf->capacity)
                                          ,httpx
                                          ,Cbk_Httpx_Getting_Content);
                    if ( status != HTTPX_CONTINUE )
                      return status;
                  }
              }
              
            return HTTPX_FINISHED;
          }
        
        if ( iterate )
           return HTTPX_CONTINUE;
           
        Buffer_Grow_Reserve(bf,bf->count + 512);
        status = Httpx_Asio_Recv(netchnl,bf->at,512,1,
                          ,httpx
                          ,Cbk_Httpx_Getting_Headers);    

        if ( status != HTTPX_CONTINUE )
          return status;
      }
  }
#endif
  ;
  
int Cbk_Httpx_Requested(void *netchnl, void *_httpx, int status)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = _httpx;
    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN|HTTPX_REQUESTED);
    httpx->state = HTTPX_REQUESTED;
    Httpx_Do_Callback(httpx,HTTPX_REQUESTED);
    Buffer_Grow_Reserve(httpx->bf,1023);
    return Httpx_Asio_Recv(netchnl, httpx->bf->at, 1023, 12 /* lengthof HTTP/1.1 200 */
                        ,httpx
                        ,Cbk_Httpx_Getting_Headers);    
  }
#endif
  ;
  
int Cbk_Httpx_Connected(void *netchnl,void *_httpx,int status)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = _httpx;
    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN|HTTPX_CONNECTED);
    httpx->state = HTTPX_CONNECTED;
    Httpx_Do_Callback(httpx,HTTPX_CONNECTED);
    /* httpx->bf contains prepared request */
    return Httpx_Asio_Send(netchnl,httpx->bf->at,httpx->bf->count,httpx,Cbk_Httpx_Requested);    
  }
#endif
  ;
  
int Cbk_Httpx_Proxy_Connected(void *netchnl,void *_httpx,int status)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    YOYO_HTTPX *httpx = _httpx;
    if ( !ASIO_SUCCEEDED(status) )
      return Httpx_Do_Callback(httpx,HTTPX_BROKEN|HTTPX_PROXY_CONNECTED);
    httpx->state = HTTPX_PROXY_CONNECTED;
    Httpx_Do_Callback(httpx,HTTPX_PROXY_CONNECTED);
    
    /// auth / ssl connect
    return Cbk_Httpx_Connected(netchnl,_httpx,status);
  }
#endif
  ;
  
int Httpx_Asio_Connect(YOYO_HTTPX *httpx)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    __Unrefe(httpx->netchnl);
    httpx->netchnl = Tcp_Socket(httpx->async?TCPSOK_ASYNC:0);
    
    if ( httpx->proxy_url )
      return Tcp_Asio_Connect(httpx->netchnl,httpx->proxy_url->host,httpx->proxy_url->port,httpx,Cbk_Httpx_Proxy_Connected);
      
    return Tcp_Asio_Connect(httpx->netchnl,httpx->url->host,httpx->url->port,httpx,Cbk_Httpx_Connected);
  }
#endif
  ;

void Build_Http_Request(YOYO_HTTPX *httpx, int method, char *uri, YOYO_DICTO *hdrs)
#ifdef _YOYO_HTTPX_BUILTIN
  {
  }
#endif
  ;
  
int Httpx_Query(YOYO_HTTPX *httpx, int method, char *uri, YOYO_DICTO *hdrs, void *poststrm, void *contstrm, int maxcount)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    if ( httpx->status_text ) { free(httpx->status_text); httpx->status_text = 0; }
    httpx->status = 0;
    httpx->method = method;
    httpx->maxcount = maxcount;
    httpx->streamed = 0;
    Build_Http_Request(httpx,method,uri,hdrs);
    return Httpx_Asio_Connect(httpx->netchnl,httpx,Cbk_Httpx_Connected);
  }
#endif
  ;
  
int Httpx_Head(YOYO_HTTPX *httpx, char *uri)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    return Httpx_Query(httpx,HTTPX_GET,uri,0,0,0,0);
  }
#endif
  ;
  
int Httpx_Get(YOYO_HTTPX *httpx, char *uri, YOYO_DICTO *params, void *contstrm, int maxcount)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    uri = Url_Compose(uri,params,0);
    return Httpx_Query(httpx,HTTPX_GET,uri,0,0,contstrm,maxcount);
  }
#endif
  ;
  
int Httpx_Post(YOYO_HTTPX *httpx, char *uri, YOYO_DICTO *params, void *contstrm, int maxcount)
#ifdef _YOYO_HTTPX_BUILTIN
  {
    void *poststrm = 0;
    if ( params ) 
      {
        YOYO_BUFFER *bf = Buffer_Init(0);
        Url_Xform_Encode(params,Buffer_Init(0));
        poststrm = Buffer_As_File(bf);
      } 
    return Httpx_Query(httpx,HTTPX_GET,uri,0,poststrm,contstrm,maxcount);
  }
#endif
  ;
  




enum
  {
    YOYO_HTTPX_DOING_NOTHING      = 0,
    YOYO_HTTPX_PREPARING       = 1,
    YOYO_HTTPX_RESOLVING       = 2,
    YOYO_HTTPX_CONNECTING      = 3,
    YOYO_HTTPX_QUERYING        = 4,
    YOYO_HTTPX_GETTING_STATUS  = 5,
    YOYO_HTTPX_GETTING_HEADERS = 6,
    YOYO_HTTPX_GETTING_CONTENT = 7,
    YOYO_HTTPX_FINISHED        = 8,
    YOYO_HTTPX_FAILED          = 9,
    YOYO_HTTPX_RESOLVING_ERROR    = 0x1001,
    YOYO_HTTPX_URLPARSING_ERROR   = 0x1002,
    YOYO_HTTPX_GETTING_ERROR      = 0x1003,
    
    YOYO_HTTPX_GET                = __FOUR_CHARS('G','E','T','_'),
    YOYO_HTTPX_PUT                = __FOUR_CHARS('P','U','T','_'),
    YOYO_HTTPX_POST               = __FOUR_CHARS('P','O','S','T'),
................................................................................

typedef struct _YOYO_HTTPX_NTFY
  {
    void (*progress)(struct _YOYO_HTTPX_NTFY *, int st, int count, int total);
    void (*error)(struct _YOYO_HTTPX_NTFY *, int st, char *msg);
  } YOYO_HTTPX_NTFY;











































































































































typedef struct _YOYO_HTTPX
  {
    void *strm;
    YOYO_BIO *bio_in;
    YOYO_BIO *bio_out;
    

Changes to tcpip.hc.

45
46
47
48
49
50
51

52
53
54
55





56
57
58

59
60
61
62
63
64
65
66
67
68
69
...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
















173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
















195
196
197
198
199
200
201
...
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217

218
219
220
221
222

223
224
225
226
227
228
229
230



231
















232
233
234





235

236
237
238
239
240
241
242























243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259

#ifndef C_once_F8F16072_F92E_49E7_A983_54F60965F4C9
#define C_once_F8F16072_F92E_49E7_A983_54F60965F4C9

#include "yoyo.hc"
#include "string.hc"
#include "file.hc"


#ifdef _LIBYOYO
#define _YOYO_TCPIP_BUILTIN
#endif






typedef struct _YOYO_TCPSOK
  {

    int       skt;
    char     *host;
    int       port;
    in_addr_t ip;
  } YOYO_TCPSOK;

#ifdef __windoze
void _WSA_Init()
#ifdef _YOYO_TCPIP_BUILTIN
  {
    static int wsa_status = -1;
................................................................................
  ;
  
void YOYO_TCPSOK_Destruct(YOYO_TCPSOK *sok)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    Tcp_Close(sok);
    free(sok->host);
    __Detruct(sok);
  }
#endif
  ;
  
int Tcp_Read(YOYO_TCPSOK *sok, void *out, int count, int mincount)
#ifdef _YOYO_TCPIP_BUILTIN  
  {
    byte_t *b = out;
    int cc = count;
    while ( cc )
      {
        int q = recv(skt,b,cc,0);
        if ( q < 0 )
          __Raise_Format(YOYO_ERROR_IO,("tcp recv failed with error %s",strerror(errno)));
        STRICT_REQUIRE( q <= cc );
        cc -= q;
        b += q;
        if ( q == 0 && count-cc >= mincount )
          break;
      }
    return count-cc;
  }
#endif
  ;
  
















int Tcp_Write(YOYO_TCPSOK *sok, void *out, int count, int mincount)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    byte_t *b = out;
    int cc = count;
    while ( cc )
      {
        int q = send(skt,b,cc,0);
        if ( q < 0 )
          __Raise_Format(YOYO_ERROR_IO,("tcp send failed with error %s",strerror(errno)));
        STRICT_REQUIRE( q <= cc );
        cc -= q;
        b += q;
        if ( q == 0 && count-cc >= mincount )
          break;
      }
    return count-cc;
  }
#endif
  ;
  
YOYO_TCPSOK *Tcp_Init(in_addr_t ip, int port, int skt, char *hostname)
















#ifdef _YOYO_TCPIP_BUILTIN  
  {
    static YOYO_FUNCTABLE funcs[] = 
      { {0},
        {Oj_Destruct_OjMID,    YOYO_TCPSOK_Destruct},
        {Oj_Close_OjMID,       Tcp_Close},
        {Oj_Read_OjMID,        Tcp_Read},
................................................................................
        {Oj_Write_OjMID,       Tcp_Write},
        //{Oj_Available_OjMID,   Tcp_Available},
        //{Oj_Eof_OjMID,         Tcp_Eof},
        {0}
      };

    YOYO_TCPSOK *sok = __Object(sizeof(YOYO_TCPSOK),funcs);
    sok->skt = skt;
    sok->port = port;
    sok->ip = ip;
    sok->host = hostname ? Str_Copy_Npl(hostname,-1) : Ipv4_Format_Npl(ip);
  }
#endif
  ;
  
YOYO_TCPSOK *Tcp_Open_Inaddr(in_addr_t ip, int port, char *hostname)

#ifdef _YOYO_TCPIP_BUILTIN
  {
    YOYO_TCPSOK *sok;
    
    sockaddr_in addr = {0}; 

    int skt, conerr;
    
    _WSA_Init();
    
    addr.sin_family = AF_INET;
    addr.sin_port   = htons(port);
    addr.sin_addr.s_addr = ip;
    



    skt = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
















    conerr = 0;
    
    if ( skt < 0 || ( conerr = connect(skt,(sockaddr*)&addr,sizeof(addr)) ) < 0 )





        __Raise_Format(YOYO_ERROR_IO,("tcp connection failed: sok %d, point (%s -> %s):%d, error %d"

                          ,skt
                          ,hostname?hostname:Ipv4_Fomat(ip)
                          ,Ipv4_Fomat(ip)
                          ,port
                          ,conerr));

    sok = Tcp_Init(skt,ip,port,hostname);























    return sok;
  }
#endif
  ;
  
YOYO_TCPSOK *Tcp_Open(char *host, int port)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    in_addr_t ip = Dns_Resolve(host);
    YOYO_TCPSOK *sok = Soket_Open_Inaddr(ip,port,hotname);
    return sok;
  }
#endif
  ;
  
#endif /* C_once_F8F16072_F92E_49E7_A983_54F60965F4C9 */








>




>
>
>
>
>



>

<

|







 







|











|












|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







|













|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
|
|
<




|
>


<
<

>
|







>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
<
>
>
>
>
>
|
>

<



|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




|
<
<
<
<
<
<
<
<
<
<


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

67
68
69
70
71
72
73
74
75
...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
...
240
241
242
243
244
245
246
247
248
249

250
251
252
253
254
255
256
257


258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289

290
291
292
293
294
295
296
297

298
299
300
301

302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329










330
331

#ifndef C_once_F8F16072_F92E_49E7_A983_54F60965F4C9
#define C_once_F8F16072_F92E_49E7_A983_54F60965F4C9

#include "yoyo.hc"
#include "string.hc"
#include "file.hc"
#include "asio.hc"

#ifdef _LIBYOYO
#define _YOYO_TCPIP_BUILTIN
#endif

enum  
  {
    TCPSOK_ASYNC = 1,
  };

typedef struct _YOYO_TCPSOK
  {
    in_addr_t ip;
    int       skt;

    int       port;
    int       async: 1;
  } YOYO_TCPSOK;

#ifdef __windoze
void _WSA_Init()
#ifdef _YOYO_TCPIP_BUILTIN
  {
    static int wsa_status = -1;
................................................................................
  ;
  
void YOYO_TCPSOK_Destruct(YOYO_TCPSOK *sok)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    Tcp_Close(sok);
    free(sok->host);
    __Destruct(sok);
  }
#endif
  ;
  
int Tcp_Read(YOYO_TCPSOK *sok, void *out, int count, int mincount)
#ifdef _YOYO_TCPIP_BUILTIN  
  {
    byte_t *b = out;
    int cc = count;
    while ( cc )
      {
        int q = recv(sok->skt,b,cc,0);
        if ( q < 0 )
          __Raise_Format(YOYO_ERROR_IO,("tcp recv failed with error %s",strerror(errno)));
        STRICT_REQUIRE( q <= cc );
        cc -= q;
        b += q;
        if ( q == 0 && count-cc >= mincount )
          break;
      }
    return count-cc;
  }
#endif
  ;

int Tcp_Asio_Recv(YOYO_TCPSOK *sok,void *out, int count, int mincount, void *obj, asio_send_callback_t callback)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    if ( sok->async )
      return Asio_Recv(sok->skt,out,count,mincount,obj,callback);
    else
      {
        int cc = Tcp_Read(sok,out,count,mincount);
        if ( callback )
          return callback(obj,ASIO_COMPLETED|ASIO_SYNCHRONOUSE,cc);
      }
    return 0;
  }
#endif
  ;

int Tcp_Write(YOYO_TCPSOK *sok, void *out, int count, int mincount)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    byte_t *b = out;
    int cc = count;
    while ( cc )
      {
        int q = send(sok->skt,b,cc,0);
        if ( q < 0 )
          __Raise_Format(YOYO_ERROR_IO,("tcp send failed with error %s",strerror(errno)));
        STRICT_REQUIRE( q <= cc );
        cc -= q;
        b += q;
        if ( q == 0 && count-cc >= mincount )
          break;
      }
    return count-cc;
  }
#endif
  ;
  
int Tcp_Asio_Send(YOYO_TCPSOK *sok,void *out, int count, void *obj, asio_send_callback_t callback)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    if ( sok->async )
      return Asio_Send(sok->skt,out,count,obj,callback);
    else
      {
        int cc = Tcp_Write(sok,out,count,count);
        if ( callback )
          return callback(obj,ASIO_COMPLETED|ASIO_SYNCHRONOUSE,cc);
      }
    return 0;
  }
#endif
  ;
  
YOYO_TCPSOK *Tcp_Socket(int flags)
#ifdef _YOYO_TCPIP_BUILTIN  
  {
    static YOYO_FUNCTABLE funcs[] = 
      { {0},
        {Oj_Destruct_OjMID,    YOYO_TCPSOK_Destruct},
        {Oj_Close_OjMID,       Tcp_Close},
        {Oj_Read_OjMID,        Tcp_Read},
................................................................................
        {Oj_Write_OjMID,       Tcp_Write},
        //{Oj_Available_OjMID,   Tcp_Available},
        //{Oj_Eof_OjMID,         Tcp_Eof},
        {0}
      };

    YOYO_TCPSOK *sok = __Object(sizeof(YOYO_TCPSOK),funcs);
    sok->skt = INVALID_SOCKET;
    if ( flags & TCPSOK_ASYNC ) sok->async = 1;
    return sok;

  }
#endif
  ;
  
#define Tcp_IPv4_Connect(Sok,Ip,Port) Tcp_Asio_IPv4_Connet(Sok,Ip,Port,0,0)
int Tcp_Asio_IPv4_Connect(YOYO_TCPSOK *sok, in_addr_t ip, int port, void *obj, asio_notify_callback_t *callback)
#ifdef _YOYO_TCPIP_BUILTIN
  {


    sockaddr_in addr = {0}; 
    socket_t skt;
    int conerr;
    
    _WSA_Init();
    
    addr.sin_family = AF_INET;
    addr.sin_port   = htons(port);
    addr.sin_addr.s_addr = ip;
    
    sok->port = port;
    sok->ip = ip;
    
    skt = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
    sok->skt = skt;

    if ( skt != INVALID_SOCKET && sok->async )
      {
      #ifdef __windoze
        ulong_t nonblock = 1;
        ioctlsocket(skt, FIONBIO, &nonblock);
      #else       
        int arg = fcntl(skt,F_GETFL,0);
        arg |= O_NONBLOCK; 
        fcntl(skt,F_SETFL,arg); 
      #endif
      }
          
    conerr = (skt != INVALID_SOCKET ) ? connect(skt,(sockaddr*)&addr,sizeof(addr)) : -1;
    
    if ( conerr < 0 )
      {

        if ( skt != INVALID_SOCKET && sok->async && Asio_Status_Repeat_Or_Die() == ASIO_PENGING && callback )
          {
            return Asio_Notify(skt,obj,callback);
          }
        else
          __Raise_Format(YOYO_ERROR_IO,
                          (__yoTa("tcp connection failed: sok %d, point %s:%d, error %d",0),
                          ,skt

                          ,Ipv4_Fomat(ip)
                          ,port
                          ,conerr));
      }

      
    if ( callback ) 
      return callback(obj,ASIO_COMPLETED|(sok->async?ASIO_SYNCHRONOUSE:0));
    return 0;
  }
#endif
  ;

#define Tcp_Connect(Sok,Host,Port) Tcp_Asio_Connect(Sok,Host,Port,0,0)
int Tcp_Asio_Connect(YOYO_TCPSOK *sok,char *host,int port,void *obj,asio_notify_callback_t *callback)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    in_addr_t ip = Dns_Resolve(host);
    return Tcp_Asio_IPv4_Connect(sok,ip,port,obj,callback);    
  }
#endif
  ;

YOYO_TCPSOK *Tcp_Open(char *host, int port)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    YOYO_TCPSOK *sok = Tcp_Socket(0);
    Tcp_Connect(sok,host,port);
    return sok;
  }
#endif
  ;











#endif /* C_once_F8F16072_F92E_49E7_A983_54F60965F4C9 */