예제 #1
0
 private async Task Server_OnSessionClosed(AsyncTcpServerSession session)
 {
     if (clientList.Contains(session))
     {
         clientList.Add(session);
     }
     Console.WriteLine($"{session.LocalEndPoint}已断开!");
 }
예제 #2
0
 private async Task Server_OnSessionStarted(AsyncTcpServerSession session)
 {
     if (clientList.Contains(session))
     {
         clientList.Remove(session);
     }
     Console.WriteLine($"{session.LocalEndPoint}已连接!");
 }
예제 #3
0
 public async Task ServerSendData(AsyncTcpServerSession session, byte[] code, byte[] body)
 {
     try
     {
         byte[] data = new byte[body.Length + 4];
         Array.Copy(code, 0, data, 0, 4);
         Array.Copy(body, 0, data, 4, body.Length);
         await session.SendAsync(data);
     }
     catch (Exception ex)
     {
         LogHelper.WriteError(ex);
     }
 }
예제 #4
0
        private async Task Server_OnSessionDataReceived(AsyncTcpServerSession session, byte[] data, int offset, int count)
        {
            try
            {
                byte[] code = data.Take(4).ToArray();    //功能码
                byte[] body = new byte[data.Length - 4]; //正文
                Array.Copy(data, 4, body, 0, body.Length);
                receiveHelper.DealReceivedgData(code, body);

                //DataReceived?.Invoke(session, code, body);
            }
            catch (Exception ex)
            {
                LogHelper.WriteError(ex);
            }
        }
예제 #5
0
        public async Task OnSessionDataReceived(AsyncTcpServerSession session, byte[] data, int offset, int count)
        {
            var text = Encoding.UTF8.GetString(data, offset, count);

            Console.Write($"Client : {session.RemoteEndPoint} --> ");
            if (count < 1024 * 1024 * 1)
            {
                Console.WriteLine(text);
            }
            else
            {
                Console.WriteLine("{0} Bytes", count);
            }

            await session.SendAsync(Encoding.UTF8.GetBytes(text));
        }
예제 #6
0
 public async Task OnSessionClosed(AsyncTcpServerSession session)
 {
     Console.WriteLine($"TCP session {session} has disconnected.");
     await Task.CompletedTask;
 }
예제 #7
0
 public async Task OnSessionStarted(AsyncTcpServerSession session)
 {
     Console.WriteLine($"TCP session {session.RemoteEndPoint} has connected {session}.");
     await Task.CompletedTask;
 }
예제 #8
0
 public async Task OnSessionStarted(AsyncTcpServerSession session)
 {
     Console.WriteLine($"{session.LocalEndPoint}已连接!");
 }
예제 #9
0
        public async Task OnSessionDataReceived(AsyncTcpServerSession session, byte[] data, int offset, int count)
        {
            if (data == null || data.Length <= 0)
            {
                return;
            }
            byte code = data[0];                      //功能码

            byte[] bytes = new byte[data.Length - 1]; //正文数据
            Array.Copy(data, 1, bytes, 0, bytes.Length);
            string unBase64txt = Encoding.UTF8.GetString(bytes);

            byte[] unBase64Bytes = Convert.FromBase64String(unBase64txt);   //bas64解码
            if (!code.Equals(CODE_HEARTBEAT))
            {
                Console.WriteLine($"功能码:{code.ToString()},数据{Encoding.UTF8.GetString(unBase64Bytes)}");
            }
            try
            {
                switch (code)
                {
                case CODE_FACEUPLOAD:       //名单上传
                {
                    string     upLoadFaceStr = Encoding.UTF8.GetString(unBase64Bytes);
                    UpLoadFace upLoadFace    = SerializeHelper.SerializeJsonToObject <UpLoadFace>(upLoadFaceStr);
                    byte[]     imgBytes      = Convert.FromBase64String(upLoadFace.imagebytes);
                    FileReadWriteHelper.WriteBytesToFile($@"D:\Test\{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.jpg", imgBytes);          //保存图片

                    ServerControl.GetInstance().faceList.Add(new FaceInfo()
                        {
                            filename     = upLoadFace.filename,
                            idnumber     = upLoadFace.idnumber,
                            imagebytes   = upLoadFace.imagebytes,
                            name         = upLoadFace.name,
                            sex          = upLoadFace.sex,
                            serialnumber = upLoadFace.serialnumber,
                            type         = upLoadFace.type
                        });
                    ServerControl.GetInstance().ServerSendMsg(CODE_FACEUPLOAD, new byte[] { SUCCESS });
                    Console.WriteLine($"名单上传成功,列表还有白名单{ServerControl.GetInstance().faceList.Where(p => p.type.Equals("white")).Count()}个,和名单{ServerControl.GetInstance().faceList.Where(p => p.type.Equals("black")).Count()}个!");
                    break;
                }

                case CODE_FACESELECT:       //名单查询
                {
                    SelectFace selectFace = new SelectFace();
                    switch (unBase64Bytes[0])
                    {
                    case WHITEFACE:
                        selectFace.faceList = ServerControl.GetInstance().faceList.Where(p => p.type.Equals("white")).ToList();
                        break;

                    case BLACKFACE:
                        selectFace.faceList = ServerControl.GetInstance().faceList.Where(p => p.type.Equals("black")).ToList();
                        break;

                    case ALLFACE:
                        selectFace.faceList = ServerControl.GetInstance().faceList;
                        break;
                    }
                    string jsonStr = SerializeHelper.SerializeObjectToJson(selectFace);
                    byte[] tBytes  = Encoding.UTF8.GetBytes(jsonStr);
                    ServerControl.GetInstance().ServerSendMsg(CODE_FACESELECT, tBytes);
                    Console.WriteLine($"名单查询成功,一共查到{selectFace.faceList.Count}个名单!");
                    break;
                }

                case CODE_FACEDELETE:       //名单删除
                {
                    string     deleteFaceStr = Encoding.UTF8.GetString(unBase64Bytes);
                    DeleteFace deleteFace    = SerializeHelper.SerializeJsonToObject <DeleteFace>(deleteFaceStr);

                    var dFace = ServerControl.GetInstance().faceList.Where(p => p.type.Equals(deleteFace.type) && p.serialnumber.Equals(deleteFace.serialnumber)).First();
                    ServerControl.GetInstance().faceList.Remove(dFace);

                    ServerControl.GetInstance().ServerSendMsg(CODE_FACEDELETE, new byte[] { SUCCESS });
                    Console.WriteLine($"名单删除成功,列表还有白名单{ServerControl.GetInstance().faceList.Where(p => p.type.Equals("white")).Count()}个,和名单{ServerControl.GetInstance().faceList.Where(p => p.type.Equals("black")).Count()}个!");
                    break;
                }

                case CODE_FACECLEAR:        //名单清除
                {
                    List <FaceInfo> clList = new List <FaceInfo>();
                    switch (unBase64Bytes[0])
                    {
                    case WHITEFACE:
                        clList = ServerControl.GetInstance().faceList.Where(p => p.type.Equals("black")).ToList();
                        break;

                    case BLACKFACE:
                        clList = ServerControl.GetInstance().faceList.Where(p => p.type.Equals("black")).ToList();
                        break;

                    case ALLFACE:
                        clList = ServerControl.GetInstance().faceList;
                        break;
                    }
                    foreach (var item in clList)
                    {
                        ServerControl.GetInstance().faceList.Remove(item);
                    }

                    ServerControl.GetInstance().ServerSendMsg(CODE_FACECLEAR, new byte[] { SUCCESS });
                    Console.WriteLine($"名单清除成功,列表还有白名单{ServerControl.GetInstance().faceList.Where(p => p.type.Equals("white")).Count()}个,和名单{ServerControl.GetInstance().faceList.Where(p => p.type.Equals("black")).Count()}个!");
                    break;
                }

                case CODE_FACCONTRAST:        //人脸对比
                {
                    switch (unBase64Bytes[0])
                    {
                    case START:
                        ServerControl.GetInstance().ServerSendMsg(CODE_FACCONTRAST, new byte[] { SUCCESS });
                        IsSend = false;
                        Console.WriteLine("启动人脸对比成功,抓图中。。。");
                        SendImg();
                        break;

                    case STOP:
                        ServerControl.GetInstance().ServerSendMsg(CODE_FACCONTRAST, new byte[] { STOPSUCCESS });
                        IsSend = false;
                        Console.WriteLine("关闭人脸对比成功!");
                        break;
                    }
                    break;
                }

                case CODE_FOLLOW:       //跟随
                {
                    switch (unBase64Bytes[0])
                    {
                    case START:
                        ServerControl.GetInstance().ServerSendMsg(CODE_FOLLOW, new byte[] { SUCCESS });
                        Console.WriteLine("启动跟随成功!");
                        break;

                    case STOP:
                        ServerControl.GetInstance().ServerSendMsg(CODE_FOLLOW, new byte[] { STOPSUCCESS });
                        Console.WriteLine("关闭跟随成功!");
                        break;

                    case RESTART:
                        ServerControl.GetInstance().ServerSendMsg(CODE_FOLLOW, new byte[] { RESTARTSUCCESS });
                        Console.WriteLine("重新跟随成功!");
                        break;
                    }
                    break;
                }

                case CODE_GETPOINT:     //获取黑体坐标
                {
                    BlackBodyPoint blackBodyPoint = new BlackBodyPoint()
                    {
                        xpoint = 55,
                        ypoint = 105
                    };
                    string jsonStr = SerializeHelper.SerializeObjectToJson(blackBodyPoint);
                    byte[] body    = Encoding.UTF8.GetBytes(jsonStr);
                    ServerControl.GetInstance().ServerSendMsg(CODE_SETPOINT, body);
                    Console.WriteLine("校准黑体坐标成功!");
                    break;
                }

                case CODE_FACEIDENTIFY:     //人脸识别
                    break;

                case CODE_FACEALARM:        //人脸报警
                    break;

                case CODE_HEARTBEAT:        //心跳
                    break;
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteError(ex);
                Console.WriteLine($"解析接收数据异常:{ex.ToString()}");
            }
        }