rename "easy" statemachines: call them block instead
[platform/upstream/curl.git] / lib / pingpong.h
1 #ifndef HEADER_CURL_PINGPONG_H
2 #define HEADER_CURL_PINGPONG_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
11  *
12  * This software is licensed as described in the file COPYING, which
13  * you should have received as part of this distribution. The terms
14  * are also available at http://curl.haxx.se/docs/copyright.html.
15  *
16  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17  * copies of the Software, and permit persons to whom the Software is
18  * furnished to do so, under the terms of the COPYING file.
19  *
20  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21  * KIND, either express or implied.
22  *
23  ***************************************************************************/
24
25 #include "curl_setup.h"
26
27 #if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_FTP) || \
28   !defined(CURL_DISABLE_POP3) || !defined(CURL_DISABLE_SMTP)
29 #define USE_PINGPONG
30 #endif
31
32 /* forward-declaration, this is defined in urldata.h */
33 struct connectdata;
34
35 /*
36  * 'pingpong' is the generic struct used for protocols doing server<->client
37  * conversations in a back-and-forth style such as FTP, IMAP, POP3, SMTP etc.
38  *
39  * It holds response cache and non-blocking sending data.
40  */
41 struct pingpong {
42   char *cache;     /* data cache between getresponse()-calls */
43   size_t cache_size;  /* size of cache in bytes */
44   size_t nread_resp;  /* number of bytes currently read of a server response */
45   char *linestart_resp; /* line start pointer for the server response
46                            reader function */
47   bool pending_resp;  /* set TRUE when a server response is pending or in
48                          progress, and is cleared once the last response is
49                          read */
50   char *sendthis; /* allocated pointer to a buffer that is to be sent to the
51                      server */
52   size_t sendleft; /* number of bytes left to send from the sendthis buffer */
53   size_t sendsize; /* total size of the sendthis buffer */
54   struct timeval response; /* set to Curl_tvnow() when a command has been sent
55                               off, used to time-out response reading */
56   long response_time; /* When no timeout is given, this is the amount of
57                          milliseconds we await for a server response. */
58
59   struct connectdata *conn; /* points to the connectdata struct that this
60                                belongs to */
61
62   /* Function pointers the protocols MUST implement and provide for the
63      pingpong layer to function */
64
65   CURLcode (*statemach_act)(struct connectdata *conn);
66
67   bool (*endofresp)(struct connectdata *conn, char *ptr, size_t len,
68                     int *code);
69 };
70
71 /*
72  * Curl_pp_statemach()
73  *
74  * called repeatedly until done. Set 'wait' to make it wait a while on the
75  * socket if there's no traffic.
76  */
77 CURLcode Curl_pp_statemach(struct pingpong *pp, bool wait);
78
79 /* initialize stuff to prepare for reading a fresh new response */
80 void Curl_pp_init(struct pingpong *pp);
81
82 /* Returns timeout in ms. 0 or negative number means the timeout has already
83    triggered */
84 long Curl_pp_state_timeout(struct pingpong *pp);
85
86
87 /***********************************************************************
88  *
89  * Curl_pp_sendf()
90  *
91  * Send the formated string as a command to a pingpong server. Note that
92  * the string should not have any CRLF appended, as this function will
93  * append the necessary things itself.
94  *
95  * made to never block
96  */
97 CURLcode Curl_pp_sendf(struct pingpong *pp,
98                        const char *fmt, ...);
99
100 /***********************************************************************
101  *
102  * Curl_pp_vsendf()
103  *
104  * Send the formated string as a command to a pingpong server. Note that
105  * the string should not have any CRLF appended, as this function will
106  * append the necessary things itself.
107  *
108  * made to never block
109  */
110 CURLcode Curl_pp_vsendf(struct pingpong *pp,
111                         const char *fmt,
112                         va_list args);
113
114 /*
115  * Curl_pp_readresp()
116  *
117  * Reads a piece of a server response.
118  */
119 CURLcode Curl_pp_readresp(curl_socket_t sockfd,
120                           struct pingpong *pp,
121                           int *code, /* return the server code if done */
122                           size_t *size); /* size of the response */
123
124
125 CURLcode Curl_pp_flushsend(struct pingpong *pp);
126
127 /* call this when a pingpong connection is disconnected */
128 CURLcode Curl_pp_disconnect(struct pingpong *pp);
129
130 int Curl_pp_getsock(struct pingpong *pp, curl_socket_t *socks,
131                     int numsocks);
132
133 #endif /* HEADER_CURL_PINGPONG_H */