-
Notifications
You must be signed in to change notification settings - Fork 0
/
niconico_client.c
314 lines (258 loc) · 8.37 KB
/
niconico_client.c
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
//-----------------------------------------------------------------------------------
// vide_chat_client.c : ビデオチャットプログラム
//-----------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <GL/glut.h>
#include <cv.h>
#include <highgui.h>
#include "defs.h"
static int g_socket;
static pthread_t g_tr, g_ts;
static pthread_mutex_t g_mutex;
static int g_width=640, g_height=480, g_depth=8, g_nChannel=3;
static int g_lena_width=640, g_lena_height=480, g_lena_depth=8, g_lena_nChannel=3;
static char g_image_buf[640*480*3];
static int g_switch = 1; //for logout
GLuint g_texture;
//-----------------------------------------------------------------------------------
// socket_open
//-----------------------------------------------------------------------------------
void
socket_open (const char* ip_addr)
{
struct sockaddr_in sin;
// ソケットを生成する
//---------------------------------------------------
if ((g_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("client: socket");
exit(1);
}
// サーバの情報を与える
//---------------------------------------------------
sin.sin_family = AF_INET; // アドレスの型の指定
sin.sin_port = PORT; // ポート番号
sin.sin_addr.s_addr = inet_addr(ip_addr); // 宛先のアドレスの指定 IPアドレスの文字列を変換
// サーバに接続する
//---------------------------------------------------
if ((connect(g_socket, (struct sockaddr *)&sin, sizeof(sin))) < 0) {
perror("client: connect");
exit(1);
}
}
//-----------------------------------------------------------------------------------
// socket_close
//-----------------------------------------------------------------------------------
void
socket_close()
{
close( g_socket );
}
//-----------------------------------------------------------------------------------
// socket_read
//-----------------------------------------------------------------------------------
int
socket_read( int socket, void* buf, int buf_size )
{
int size;
int left_size = buf_size;
char* p = (char*) buf;
// 指定されたサイズのバッファを受信完了するまで、
// ループで読み込み続ける
//---------------------------------------------------
while( left_size > 0 ) {
size = read(socket, p, left_size);
p += size;
left_size -= size;
if( size <= 0 ) break;
}
return buf_size - left_size;
}
//-----------------------------------------------------------------------------------
// socket_write
//-----------------------------------------------------------------------------------
int
socket_write( int socket, void* buf, int buf_size )
{
int size;
int left_size = buf_size;
char* p = (char*) buf;
// 指定されたサイズのバッファを送信完了するまで、
// ループで書き込み続ける
//---------------------------------------------------
while( left_size > 0 ) {
size = write(socket, p, left_size);
p += size;
left_size -= size;
if( size <= 0 ) break;
}
return buf_size - left_size;
}
//-----------------------------------------------------------------------------------
// send_sock
//-----------------------------------------------------------------------------------
void*
thread_send(void *arg)
{
char buf[1024];
printf("chat start\n");
while(1){
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0'; //改行文字消す
int size = strlen(buf)+1;
if ( strcmp(buf,"_logout") == 0 ) g_switch= 0;//for logout
if (socket_write( g_socket, &size, sizeof(int) ) < 0){
return NULL;
}
if (socket_write( g_socket, buf, size ) < 0){
return NULL;
}
}
return NULL;
}
//-----------------------------------------------------------------------------------
// recv_sock
//-----------------------------------------------------------------------------------
void*
thread_recv(void *arg)
{
char buf[1024];
int size;
socket_read(g_socket, &g_width, sizeof(int) );
socket_read(g_socket, &g_height, sizeof(int) );
socket_read(g_socket, &g_depth, sizeof(int) );
socket_read(g_socket, &g_nChannel, sizeof(int) );
while(1){
if(g_switch){
socket_read(g_socket, &size, sizeof(int));
// ダミーではないとき
if( size > 0 ) {
socket_read(g_socket, buf, size );
printf("%s\n", buf);
} else {
socket_read(g_socket, g_image_buf, g_width*g_height*(g_depth/8)*g_nChannel);
//image_buf, width*nChannel
}
} else{ //after input logout
socket_read(g_socket, &size, sizeof(int));
if (size == 0){
socket_read(g_socket, g_image_buf, size);
break;
}
else socket_read(g_socket, buf, size );
}
}
socket_close();
return NULL;
}
//-----------------------------------------------------------------------------------
// init
//-----------------------------------------------------------------------------------
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
glGenTextures( 1, &g_texture );
glBindTexture( GL_TEXTURE_2D, g_texture );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
//-----------------------------------------------------------------------------------
// display
//-----------------------------------------------------------------------------------
void display(void)
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10, 10, -10, 10, -10, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3ub(255,255,255);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_texture);
glScalef(20,20,20);
glTranslatef(-0.5f, -0.5f, 0.0f);
glBegin(GL_QUADS);
glTexCoord2f(0,1); glVertex2f(0,0);
glTexCoord2f(1,1); glVertex2f(1,0);
glTexCoord2f(1,0); glVertex2f(1,1);
glTexCoord2f(0,0); glVertex2f(0,1);
glEnd();
glColor3f(1.0,1.0,0.0);
glFlush();
glFinish();
glutSwapBuffers();
}
//-----------------------------------------------------------------------------------
// idle
//-----------------------------------------------------------------------------------
void idle()
{
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, g_width, g_height, 0,
GL_BGR, GL_UNSIGNED_BYTE, g_image_buf );
glutPostRedisplay();
}
//-----------------------------------------------------------------------------------
// reshape
//-----------------------------------------------------------------------------------
void reshape (int w, int h)
{
glViewport(0, 0, w, h);
}
//-----------------------------------------------------------------------------------
// keyboard
//-----------------------------------------------------------------------------------
/* ARGSUSED1 */
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
socket_close();
exit(0);
break;
}
}
//-----------------------------------------------------------------------------------
// main
//-----------------------------------------------------------------------------------
int
main(int argc, char *argv[])
{
if(argc == 1){
printf("usage: client <server IP address>\n");
exit(1);
}
// GLUTの初期化
//---------------------------------------------------
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (680, 480);
glutInitWindowPosition (100, 100);
// サーバーへ接続
//---------------------------------------------------
socket_open( argv[1] );
pthread_mutex_init( &g_mutex, NULL );
pthread_create( &g_tr, NULL, thread_recv, NULL );
pthread_create( &g_ts, NULL, thread_send, NULL );
// 新しくウィンドウを作る
//---------------------------------------------------
glutCreateWindow (argv[0]);
init();
// GLUTのメインループ
//---------------------------------------------------
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}