Exemplo n.º 1
0
        private static void Downline(onezl.iocp.AsyncSocketUserToken SocketArg)
        {
            var ipstr = SocketArg.IpportStr;
            var aaaa  = new byte[0];

            websocketdatecahe.TryRemove(SocketArg.IpportStr, out aaaa);
            Console.WriteLine("掉线客户端:" + ipstr);

            if (userip.ContainsKey(ipstr))
            {
                foreach (var item in userlist)
                {
                    if (item.Key != userip[ipstr])
                    {
                        DataFrame dfff = new DataFrame("用户:" + userip[ipstr] + "已经掉线");
                        socketserver.PushSendQue(item.Value, dfff.GetBytes());
                        DataFrame dfff1 = new DataFrame("userlist," + string.Join(",", userlist.Select(a => a.Key).Where(b => b != userip[ipstr])));
                        socketserver.PushSendQue(item.Value, dfff1.GetBytes());
                    }
                }
                var o = new SocketAsyncEventArgs();
                userlist.TryRemove(userip[ipstr], out o);
                var stemp = "";
                userip.TryRemove(ipstr, out stemp);
            }
        }
Exemplo n.º 2
0
        private static void myfuncion(onezl.iocp.AsyncSocketUserToken SocketArg, byte[] byteArr)
        {
            try
            {
                //连接

                //socketserver.PushSendQue(SocketArg.ReceiveEventArgs, Encoding.UTF8.GetBytes("HTTP/1.1 200 OK\r\nContent-Type:text/html;charset=utf-8\r\nContent-Length:18\r\n\r\nWelcome to tinyweb"));
                //   return;

                Console.WriteLine("queueid:" + SocketArg.QueueId);
                string strAll = Encoding.UTF8.GetString(byteArr);

                Console.WriteLine(strAll);
                string   appNum     = strAll.Substring(0, 2);
                string   commandStr = strAll.Substring(0, 6);
                string   str        = strAll.Substring(6);
                string[] strs       = str.Split(new string[] { "[f.ff]" }, StringSplitOptions.RemoveEmptyEntries);


                switch (commandStr)
                {
                case "000505":
                    var ipstr = SocketArg.IpportStr;
                    socketserver.CleandicBuffer(SocketArg.QueueId, SocketArg.IpportStr); //清空粘包缓存区
                    socketserver.CloseClientSocketTopool((SocketArg.ReceiveEventArgs.UserToken as Socket),
                                                         SocketArg.ReceiveEventArgs);    //把异步对象清空,投入异步对象池中重新利用

                    var aaaa = new byte[0];
                    websocketdatecahe.TryRemove(SocketArg.IpportStr, out aaaa);


                    if (userip.ContainsKey(ipstr))
                    {
                        foreach (var item in userlist)
                        {
                            if (item.Key != userip[ipstr])
                            {
                                DataFrame dfff = new DataFrame("用户:" + userip[ipstr] + "已经掉线");
                                socketserver.PushSendQue(item.Value, dfff.GetBytes());
                                DataFrame dfff1 = new DataFrame("userlist," + string.Join(",", userlist.Select(a => a.Key).Where(b => b != userip[ipstr])));
                                socketserver.PushSendQue(item.Value, dfff1.GetBytes());
                            }
                        }
                        var o = new SocketAsyncEventArgs();
                        userlist.TryRemove(userip[ipstr], out o);
                        var stemp = "";
                        userip.TryRemove(ipstr, out stemp);
                    }



                    break;

                case "000506":


                    socketserver.CleandicBufferforConnect(SocketArg.QueueId, SocketArg.IpportStr, SocketArg.ReceiveEventArgs); //清空粘包缓存区,并且返回池子
                    break;

                case "000001"://注册身份命令 001[f.f]黎明

                    socketserver.RemoveZombieSocketAsyncEventArgs(SocketArg.ReceiveEventArgs);
                    break;



                case "001"://注册身份命令 001[f.f]黎明

                    socketserver.PushSendQue(SocketArg.ReceiveEventArgs, Encoding.UTF8.GetBytes("你好"));
                    //不用占包,toserver内部处理

                    break;

                default:


                    #region Sec-WebSocket-Key WebSocketConnect


                    if (strAll.Contains("Sec-WebSocket-Key"))
                    {
                        //socketserver.PushSendQue(SocketArg.ReceiveEventArgs, Encoding.UTF8.GetBytes("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept:xsOSgr30aKL2GNZKNHKmeT1qYjA = "));

                        string header = strAll;

                        try
                        {
                            Regex webSocketKeyRegex     = new Regex("Sec-WebSocket-Key: (.*)");
                            Regex webSocketVersionRegex = new Regex("Sec-WebSocket-Version: (.*)");

                            // check the version. Support version 13 and above
                            const int WebSocketVersion    = 13;
                            int       secWebSocketVersion = Convert.ToInt32(webSocketVersionRegex.Match(header).Groups[1].Value.Trim());
                            if (secWebSocketVersion < WebSocketVersion)
                            {
                                //throw new WebSocketVersionNotSupportedException(string.Format("WebSocket Version {0} not suported. Must be {1} or above", secWebSocketVersion, WebSocketVersion));
                            }

                            string secWebSocketKey = webSocketKeyRegex.Match(header).Groups[1].Value.Trim();



                            SHA1   sha1               = new SHA1CryptoServiceProvider();
                            byte[] bytes_sha1_in      = Encoding.UTF8.GetBytes(secWebSocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
                            byte[] bytes_sha1_out     = sha1.ComputeHash(bytes_sha1_in);
                            string str_sha1_out       = Convert.ToBase64String(bytes_sha1_out);
                            string setWebSocketAccept = str_sha1_out;


                            string response = ("HTTP/1.1 101 Switching Protocols" + Environment.NewLine
                                               + "Connection: Upgrade" + Environment.NewLine
                                               + "Upgrade: websocket" + Environment.NewLine
                                               + "Sec-WebSocket-Accept: " + setWebSocketAccept + Environment.NewLine + Environment.NewLine);
                            Console.WriteLine(response);
                            socketserver.PushSendQue(SocketArg.ReceiveEventArgs, Encoding.UTF8.GetBytes(response));
                            return;
                        }


                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }

                        return;
                    }
                    #endregion



                    if (!websocketdatecahe.ContainsKey(SocketArg.IpportStr))
                    {
                        websocketdatecahe.TryAdd(SocketArg.IpportStr, byteArr);
                    }
                    else
                    {
                        websocketdatecahe[SocketArg.IpportStr] = CombomBinaryArray(websocketdatecahe[SocketArg.IpportStr], byteArr);
                    }



                    List <string> slist = AnalyzeClientData(SocketArg.IpportStr);
                    foreach (var s in slist)
                    {
                        string messageReceived = string.Empty;

                        if (s.Trim().StartsWith("name:") && s.Trim().Length > 5)
                        {
                            var name = s.Trim().Substring(5);
                            if (userlist.ContainsKey(name))
                            {
                                DataFrame dfff = new DataFrame("你注册的用户名已存在,请换个名字试试!");

                                socketserver.PushSendQue(SocketArg.ReceiveEventArgs, dfff.GetBytes());
                                continue;
                            }
                            else
                            {
                                if (userip.ContainsKey(SocketArg.IpportStr))//已经注册过
                                {
                                    DataFrame dfff = new DataFrame("你已经注册过用户名,你的用户名为:" + userip[SocketArg.IpportStr]);

                                    socketserver.PushSendQue(SocketArg.ReceiveEventArgs, dfff.GetBytes());
                                    continue;
                                }
                                socketserver.RemoveZombieSocketAsyncEventArgs(SocketArg.ReceiveEventArgs);
                                userlist.TryAdd(name, SocketArg.ReceiveEventArgs);
                                userip.TryAdd(SocketArg.IpportStr, name);

                                foreach (var item in userlist.Values)
                                {
                                    DataFrame dfff = new DataFrame("用户:" + name + "已经登录");
                                    socketserver.PushSendQue(item, dfff.GetBytes());

                                    DataFrame dfff1 = new DataFrame("userlist," + string.Join(",", userlist.Select(a => a.Key)));
                                    socketserver.PushSendQue(item, dfff1.GetBytes());
                                }
                            }
                            continue;
                        }

                        string fromname = "未知用户";
                        if (userip.ContainsKey(SocketArg.IpportStr))
                        {
                            fromname = userip[SocketArg.IpportStr];
                        }

                        if (s.Trim().StartsWith("@") && s.Trim().Length > 1)
                        {
                            var name    = s.Trim();
                            var toname  = "";
                            var content = "";
                            if (name.IndexOf(" ") > -1)
                            {
                                toname  = name.Substring(1, name.IndexOf(" ") - 1);
                                content = name.Replace("@" + toname + " ", "");
                            }
                            else
                            {
                                toname = name.Substring(1);
                            }
                            if (userlist.ContainsKey(toname))
                            {
                                DataFrame dfff = new DataFrame("消息来自:" + fromname + ",内容:" + content);

                                socketserver.PushSendQue(userlist[toname], dfff.GetBytes());

                                DataFrame dfff2 = new DataFrame("消息\"" + content + "\"已发送给" + toname + "!");

                                socketserver.PushSendQue(SocketArg.ReceiveEventArgs, dfff2.GetBytes());
                                continue;
                            }
                            else
                            {
                                DataFrame dfff = new DataFrame("你发送的对象不在线!");

                                socketserver.PushSendQue(SocketArg.ReceiveEventArgs, dfff.GetBytes());
                            }


                            continue;
                        }

                        if (s.Trim() != "")
                        {
                            foreach (var item in userlist.Values)
                            {
                                DataFrame dfff = new DataFrame("消息来自:" + fromname + ",内容:" + s);
                                socketserver.PushSendQue(item, dfff.GetBytes());
                            }
                        }
                    }



                    //---------------------------

                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }