Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/samples/socketio_connect/main.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #include "stdio.h"
5 #include "azure_c_shared_utility/optimize_size.h"
6 #include "azure_c_shared_utility/xio.h"
7 #include "azure_c_shared_utility/socketio.h"
8 #include "azure_c_shared_utility/platform.h"
9
10 // A simple sample callback.
11 static void on_send_complete(void* context, IO_SEND_RESULT send_result)
12 {
13 (void)context;
14 (void)send_result;
15 }
16
17 static void on_io_open_complete(void* context, IO_OPEN_RESULT open_result)
18 {
19 (void)context, (void)open_result;
20 (void)printf("Open complete called\r\n");
21
22 if (open_result == IO_OPEN_OK)
23 {
24 XIO_HANDLE socketio = (XIO_HANDLE)context;
25 const char to_send[] = "GET / HTTP/1.1\r\n"
26 "Host: www.google.com\r\n"
27 "\r\n";
28 (void)printf("Sending bytes ...\r\n");
29 if (xio_send(socketio, to_send, sizeof(to_send), on_send_complete, NULL) != 0)
30 {
31 (void)printf("Send failed\r\n");
32 }
33 }
34 else
35 {
36 (void)printf("Open error\r\n");
37 }
38 }
39
40 static void on_io_bytes_received(void* context, const unsigned char* buffer, size_t size)
41 {
42 (void)context, (void)buffer;
43 (void)printf("Received %lu bytes\r\n", (unsigned long)size);
44 }
45
46 static void on_io_error(void* context)
47 {
48 (void)context;
49 (void)printf("IO reported an error\r\n");
50 }
51
52 int main(int argc, char** argv)
53 {
54 int result;
55
56 (void)argc, (void)argv;
57
58 if (platform_init() != 0)
59 {
60 (void)printf("Cannot initialize platform.");
61 result = MU_FAILURE;
62 }
63 else
64 {
65 const IO_INTERFACE_DESCRIPTION* socketio_interface = socketio_get_interface_description();
66 if (socketio_interface == NULL)
67 {
68 (void)printf("Error getting socketio interface description.");
69 result = MU_FAILURE;
70 }
71 else
72 {
73 SOCKETIO_CONFIG socketio_config;
74 XIO_HANDLE socketio;
75
76 socketio_config.hostname = "www.google.com";
77 socketio_config.port = 80;
78 socketio = xio_create(socketio_interface, &socketio_config);
79 if (socketio == NULL)
80 {
81 (void)printf("Error creating socket IO.");
82 result = MU_FAILURE;
83 }
84 else
85 {
86 if (xio_open(socketio, on_io_open_complete, socketio, on_io_bytes_received, socketio, on_io_error, socketio) != 0)
87 {
88 (void)printf("Error opening socket IO.");
89 result = MU_FAILURE;
90 }
91 else
92 {
93 unsigned char done = 0;
94 while (!done)
95 {
96 xio_dowork(socketio);
97 }
98
99 result = 0;
100 }
101
102 xio_destroy(socketio);
103 }
104 }
105
106 platform_deinit();
107 }
108
109 return result;
110 }