/
i_chat.c
executable file
·213 lines (197 loc) · 5.57 KB
/
i_chat.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
/********************************************/
/*名称:i_chat.c
/*描述:此文件定义了文件传输相关的函数
/*作者:
/*日期:
/********************************************/
#ifndef __I_CHAT_
#define __I_CHAT_
#include "i_chat.h"
#define NAME_LEN 256
#define MSG_BUF 8096
static DB *chatList;
typedef struct foreachParm_T
{
chatFunc func;
void *parm;
}foreachParm;
static int doForeach(DB_DAT *chatList,void *parm);
static int doSearch(LinpopChat *chat,void *addr);
static int searchDB_DAT(DB_DAT *chatData,void *parm);
/*static int printUser(DB_DAT *chatData,void *para);*/
/**************************************************/
/*函数名: initChatList
/*功能描述:初始化窗口列表
/*时间:
/*返回值:void
/*作者:
/***************************************************/
void initChatList(void)
{
chatList = db_new(NULL);
}
/**************************************************/
/*函数名: getChatNumber
/*功能描述:获取窗口号码
/*时间:
/*返回值:void
/*作者:
/***************************************************/
unsigned int getChatNumber()
{
return chatList->num;
}
/**************************************************/
/*函数名: deleteChatByIP
/*功能描述:删除窗口
/*时间:
/*参数:
struct sockaddr_in addr:地址
/*返回值:void
/*作者:
/***************************************************/
gboolean deleteChatByIP(struct sockaddr_in addr)
{
DB_DAT *chatData = NULL;
if((chatData= (DB_DAT*)db_foreach(chatList,searchDB_DAT,&addr)) != 0)
{
LinpopChat *chat = (LinpopChat *)chatData->data;
g_free(chat->msg);
g_free(chat);
dbd_free(chatList,chatData);
return TRUE;
}
return FALSE;
}
/**************************************************/
/*函数名:addChat
/*功能描述:添加窗口
/*时间:
/*参数:
LinpopChat *win:聊天窗口
/*返回值:void
/*作者:
/***************************************************/
void addChat(LinpopChat *win)
{
LinpopChat *chat;
foreachParm parm = {(chatFunc)doSearch,(void *)&win->from};
//DB_DAT *chatData;
if((chat = db_foreach(chatList,doForeach,&parm)) != 0)
return ;
chat = g_malloc(sizeof(*chat));
chat->mainWindow = win->mainWindow;
chat->showTextView = win->showTextView;
chat->inputTextView = win->inputTextView;
chat->showWindow = win->showWindow;
chat->inputWindow = win->inputWindow;
chat->selfImage = win->selfImage;
chat->otherImage = win->otherImage;
chat->selfLabel = win->selfLabel;
chat->otherLabel = win->otherLabel;
chat->from = win->from;
chat->msg = g_malloc(MSG_BUF);
chat->msg[0] = '\0';
dbd_append(chatList, chat);
return ;
}
/**************************************************/
/*函数名:getChatByIP
/*功能描述:通过ip获取聊天窗口
/*时间:
/*参数:
struct sockaddr_in addr:
LinpopChat *chat:
/*返回值:gboolean
/*作者:
/***************************************************/
gboolean getChatByIP(struct sockaddr_in addr,LinpopChat *chat)
{
LinpopChat *temp = NULL;
foreachParm parm = {(chatFunc)doSearch,&addr};
if((temp = db_foreach(chatList,doForeach,&parm)) != 0){
chat->mainWindow = temp->mainWindow;
chat->showTextView = temp->showTextView;
chat->inputTextView = temp->inputTextView;
chat->showWindow = temp->showWindow;
chat->inputWindow = temp->inputWindow;
chat->selfImage = temp->selfImage;
chat->otherImage = temp->otherImage;
chat->selfLabel = temp->selfLabel;
chat->otherLabel = temp->otherLabel;
chat->from = temp->from;
chat->msg = temp->msg;
return TRUE;
}
else
return FALSE;
}
/**************************************************/
/*函数名:chatListForeach
/*功能描述:
/*时间:
/*参数:
chatFunc func:函数
void *arg:
/*返回值:int
/*作者:
/***************************************************/
int chatListForeach(chatFunc func,void *arg)
{
foreachParm parm = {func,arg};
return db_foreach(chatList,doForeach,&parm);
}
/**************************************************/
/*函数名:doSearch
/*功能描述:
/*时间:
/*参数:
LinpopChat *chat
void *addr
/*返回值:int
/*作者:
/***************************************************/
static int doSearch(LinpopChat *chat,void *addr)
{
struct sockaddr_in searchAddr = *(struct sockaddr_in *)addr;
if( memcmp(&chat->from.sin_addr,&searchAddr.sin_addr,4) != 0)
return 0;
return (int)chat;
}
/**************************************************/
/*函数名:doForeach
/*功能描述:
/*时间:
/*参数:
DB_DAT *chatList:指针窗口列表
void *parm
/*返回值:int
/*作者:
/***************************************************/
static int doForeach(DB_DAT *chatList,void *parm)
{
LinpopChat *chat = (LinpopChat *)chatList->data;
chatFunc func = ((foreachParm *)parm)->func;
void *arg = ((foreachParm *)parm)->parm;
return func(chat,arg);
}
/**************************************************/
/*函数名:searchDB_DAT
/*功能描述:
/*时间:
/*参数:
DB_DAT *chatData
void *parm
/*返回值:int
/*作者:
/***************************************************/
static int searchDB_DAT(DB_DAT *chatData,void *parm)
{
struct sockaddr_in addr = *(struct sockaddr_in *)parm;
LinpopChat *chat = (LinpopChat *)chatData->data;
if(memcmp(&chat->from.sin_addr,&addr.sin_addr,4) != 0)
return 0;
else
return (int)chatData;
}
#endif