Ejemplo n.º 1
0
        private HandleResult Server_OnReceive(IntPtr connId, int length)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            #region 收数据
            // 需要长度
            int required = pkgInfo.Length;

            // 剩余大小
            int remain = length;

            while (remain >= required)
            {
                IntPtr bufferPtr = IntPtr.Zero;
                try
                {
                    remain   -= required;
                    bufferPtr = Marshal.AllocHGlobal(required);
                    if (server.Fetch(connId, bufferPtr, required) == FetchResult.Ok)
                    {
                        if (pkgInfo.IsHeader == true)
                        {
                            PkgHeader header = (PkgHeader)Marshal.PtrToStructure(bufferPtr, typeof(PkgHeader));

                            required = header.BodySize;
                        }
                        else
                        {
                            //intptr转byte[]
                            byte[] bytes = new byte[required];
                            Marshal.Copy(bufferPtr, bytes, 0, required);
                            using (var fs = new FileStream(rvtName, FileMode.Create))
                            {
                                fs.Write(bytes, 0, bytes.Length);
                                fs.Close();
                            }

                            required = pkgHeaderSize;
                        }

                        // 在后面赋值,因为前面需要用到pkgInfo.Length
                        pkgInfo.IsHeader = !pkgInfo.IsHeader;
                        pkgInfo.Length   = required;
                        if (server.SetExtra(connId, pkgInfo) == false)
                        {
                            return(HandleResult.Error);
                        }
                    }
                }
                catch
                {
                    return(HandleResult.Error);
                }
                finally
                {
                    if (bufferPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(bufferPtr);
                        bufferPtr = IntPtr.Zero;
                    }
                }
            }
            #endregion

            return(HandleResult.Ok);
        }
Ejemplo n.º 2
0
        private HandleResult Server_OnReceive(IntPtr connId, int length)
        {
            if (count == 0)
            {
                now = DateTime.Now.ToString("yyyyMMddhhmmss");
            }
            if (!Directory.Exists(path + now))
            {
                Directory.CreateDirectory(path + now);
                dwfFile = path + now + "\\" + now + ".dwf";
                jpgFile = dwfFile.Replace(".dwf", ".jpg");
                dwgFile = dwfFile.Replace(".dwf", ".dwg");
            }

            #region 收数据
            // 需要长度
            int required = pkgInfo.Length;

            // 剩余大小
            int remain = length;

            while (remain >= required)
            {
                IntPtr bufferPtr = IntPtr.Zero;
                try
                {
                    remain   -= required;
                    bufferPtr = Marshal.AllocHGlobal(required);
                    if (server.Fetch(ID, bufferPtr, required) == FetchResult.Ok)
                    {
                        if (pkgInfo.IsHeader == true)
                        {
                            PkgHeader header = (PkgHeader)Marshal.PtrToStructure(bufferPtr, typeof(PkgHeader));

                            required = header.BodySize;
                        }
                        else
                        {
                            //intptr转byte[]
                            byte[] bytes = new byte[required];
                            Marshal.Copy(bufferPtr, bytes, 0, required);
                            using (var fs = new FileStream(dwfFile, FileMode.Create))
                            {
                                fs.Write(bytes, 0, bytes.Length);
                                fs.Close();
                            }

                            required = pkgHeaderSize;
                        }

                        // 在后面赋值,因为前面需要用到pkgInfo.Length
                        pkgInfo.IsHeader = !pkgInfo.IsHeader;
                        pkgInfo.Length   = required;
                        if (server.SetExtra(ID, pkgInfo) == false)
                        {
                            return(HandleResult.Error);
                        }
                    }
                }
                catch
                {
                    return(HandleResult.Error);
                }
                finally
                {
                    if (bufferPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(bufferPtr);
                        bufferPtr = IntPtr.Zero;
                    }
                }
            }
            #endregion
            if (count == 0)
            {
                Application.Idle += Application_Idle;
                count             = 1;
            }

            return(HandleResult.Ok);
        }
Ejemplo n.º 3
0
        private HandleResult AcceptPool(IntPtr connId)
        {
            try
            {
                string strAdvance   = string.Empty;
                byte[] strCmd       = new byte[0];
                bool   IsDisconnect = false; //是否强制断开客户端
                string ip           = string.Empty;
                ushort sport        = 0;

                if (_server.GetRemoteAddress(connId, ref ip, ref sport))
                {
                    strAdvance = string.Format(" > [{0},OnAccept] -> PASS({1}:{2})", connId, ip.ToString(), sport);
                    WriteDeviceLog.WriteLog("Log\\" + _name + "\\Accept客户端", strAdvance, Guid.NewGuid().ToString());
                }
                else
                {
                    strAdvance = string.Format(" > [{0},OnAccept] -> Server_GetClientAddress() Error", connId);
                    WriteDeviceLog.WriteLog("Log\\" + _name + "\\Accept客户端", strAdvance, Guid.NewGuid().ToString());
                }
                ClientInfo clientInfo = new ClientInfo();
                clientInfo.ConnId    = connId;
                clientInfo.IpAddress = ip;
                clientInfo.Port      = sport;
                if (_server.SetExtra(connId, clientInfo) == false)
                {
                    //给客户端连接加载连接参数失败,处理出错。
                }
                //连接断开时要干掉客户端对象。不然会增长过大。
                if (DicConnId.ContainsKey(connId.ToString()))
                {
                    DicConnId[connId.ToString()] = StrDeviceId;
                }
                else
                {
                    DicConnId.TryAdd(connId.ToString(), StrDeviceId); //客户端连接池
                }

                //if (SpreadObject.ContainsKey(connId))
                //{
                //    SpreadObject[connId] = new SpreadModel();
                //}
                //else
                //{
                //    SpreadObject.TryAdd(connId, new SpreadModel());//客户端扩展对象池
                //}
                Thread.Sleep(1500);
                if (Dic.ContainsKey(StrDeviceId))
                {
                    IResolve = Dic[StrDeviceId];
                    IResolve.OnLinkStart(connId.ToString(), ref SpreadObject, strAdvance, ref strCmd, ref IsDisconnect);
                    //是否强制断开连接True:强制断开客户端 False 不执行断开操作
                    if (IsDisconnect)
                    {
                        return(HandleResult.Error);
                    }
                    if (strCmd.Length > 0)
                    {
                        if (_server.Send(connId, strCmd, strCmd.Length) == false)
                        {
                            WriteDeviceLog.WriteLog("Log\\" + _name + "\\OnConnect",
                                                    "   应答没有发出去。strCmd:" + Encoding.Default.GetString(strCmd), Guid.NewGuid().ToString());
                        }
                    }
                }

                InitServiceInfo(true);
                return(HandleResult.Ok);
            }
            catch (Exception ex)
            {
                WriteDeviceLog.WriteLog("Log\\" + _name + "\\Accept客户端", ex.ToString(), Guid.NewGuid().ToString());
                return(HandleResult.Error);
            }
            finally
            {
                //InitMonitorService.AutoResetEvent.Set();
            }
        }