Exemple #1
0
        private void Send <TRequest, TResponse>(Howell5198Session session, FixedHeaderPackageInfo <ProtocolHeader> requestInfo, Func <Howell5198Session, TRequest, TResponse> instanceFunction)
            where TRequest : class, IBytesSerialize, new()
            where TResponse : class, IBytesSerialize, new()
        {
            TRequest request = new TRequest();

            request.FromBytes(requestInfo.Payload, 0, requestInfo.Payload.Length);
            TResponse response = null;

            session.ErrorNo = 0;
            try
            {
                response = instanceFunction(session, request);
            }
            catch (Exception ex)
            {
                session.ErrorNo = ErrorNo.HW_NET_NETWORK_RECV_ERROR;
                Console.WriteLine("Protocol Process Error,{0}.", ex.Message);
            }
            if (response == null)
            {
                session.Send(session.ErrorNo, requestInfo.Header.proType, null);
            }
            else
            {
                session.Send(session.ErrorNo, requestInfo.Header.proType, response.GetBytes());
            }
        }
Exemple #2
0
        //数据接收
        void m_Server_NewRequestReceived(FixedHeaderProtocolSession <ProtocolHeader> session, FixedHeaderPackageInfo <ProtocolHeader> requestInfo)
        {
            try
            {
                Howell5198Session vcSession = null;
                lock (m_Sessions)
                {
                    if (m_Sessions.SyncTryGet(session.SessionID, out vcSession) == false)
                    {
                        vcSession = new Howell5198Session(session, AliveInterval, session.RemoteEndPoint, new Howell5198SessionContext("", "", session.SessionID));
                        m_Sessions.Add(session.SessionID, vcSession);
                    }
                }
                if (requestInfo.Header.proType == ProtocolType.Login)
                {
                    Send <LoginRequest, LoginResponse>(vcSession, requestInfo, m_AppInstance.Login);
                    if (vcSession.ErrorNo == ErrorNo.HW_NET_NOERROR)
                    {
                        LoginRequest request = new LoginRequest();
                        request.FromBytes(requestInfo.Payload, 0, requestInfo.Payload.Length);
                        vcSession.Context.SetLoggedIn(request.UserName, request.Password, true);
                        //登陆成功后跟着发送ServerInfo,并没有额外的request
                        ServerInfo serverinfo = m_AppInstance.GetServerInfo(vcSession);
                        session.Send(new FixedHeaderPackageInfo <ProtocolHeader>(Guid.NewGuid().ToString("N"),
                                                                                 new ProtocolHeader()
                        {
                            proType = ProtocolType.Serverinfo, errornum = (uint)vcSession.ErrorNo, dataLen = (uint)serverinfo.GetLength()
                        },
                                                                                 serverinfo.GetBytes()));

                        //提交设备登录成功事件
                        RaiseSessionRegistered(vcSession);
                    }
                }
                else if (requestInfo.Header.proType == ProtocolType.Main_stream || requestInfo.Header.proType == ProtocolType.Sub_stream || requestInfo.Header.proType == ProtocolType.Unknow)
                {
                    if (m_StreamSessions.SyncContainsKey(session.SessionID) == true)
                    {
                        return;
                    }
                    StreamRequest request = new StreamRequest();
                    request.FromBytes(requestInfo.Payload, 0, requestInfo.Payload.Length);

                    int type = requestInfo.Header.proType == ProtocolType.Sub_stream ? 1 : 0;
                    MediaStreamSession newStreamSession = new MediaStreamSession(session, AliveInterval, session.RemoteEndPoint,
                                                                                 new MediaStreamSessionContext(session.SessionID, new MediaStreamIdentifier(request.ChannelNo, type)));
                    StreamResponse response = null;
                    try
                    {
                        response = m_AppInstance.GetStream(newStreamSession);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("GetStream Error,{0}.", ex.Message);
                    }

                    if (response.Success == -1)
                    {
                        vcSession.Send(0, requestInfo.Header.proType, response.GetBytes());
                        newStreamSession.Dispose();
                    }
                    else
                    {
                        lock (m_StreamSessions)
                        {
                            m_StreamSessions.Add(session.SessionID, newStreamSession);
                        }
                        vcSession.Send(0, requestInfo.Header.proType, response.GetBytes());
                        RaiseStreamSessionRegistered(newStreamSession);
                    }
                }
                else if (requestInfo.Header.proType == ProtocolType.GetFile)
                {
                    if (m_StreamSessions.SyncContainsKey(session.SessionID) == true)
                    {
                        return;
                    }
                    GetFileRequest request = new GetFileRequest();
                    request.FromBytes(requestInfo.Payload, 0, requestInfo.Payload.Length);
                    MediaStreamSession newSession = new MediaStreamSession(session, AliveInterval, session.RemoteEndPoint,
                                                                           new MediaStreamSessionContext(session.SessionID, new MediaStreamIdentifier(request.ChannelNo, 0), true,
                                                                                                         new DateTime(request.Beg.WYear, request.Beg.WMonth, request.Beg.WDay, request.Beg.WHour, request.Beg.WMinute, request.Beg.WSecond, DateTimeKind.Local),
                                                                                                         new DateTime(request.End.WYear, request.End.WMonth, request.End.WDay, request.End.WHour, request.End.WMinute, request.End.WSecond, DateTimeKind.Local)));
                    if (session.Connected)
                    {
                        m_StreamSessions.SyncAdd(session.SessionID, newSession);
                    }
                    else
                    {
                        newSession.Dispose();
                        return;
                    }
                    try
                    {
                        m_AppInstance.GetFile(newSession, request);//获得GetFileResponse.Type=0的包
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("GetFile Error,{0}.", ex.Message);
                    }
                }
                else if (requestInfo.Header.proType == ProtocolType.GetNetHead)
                {
                    Send <GetNetHeadRequest, GetNetHeadResponse>(vcSession, requestInfo, m_AppInstance.GetNetHead);
                }
                else if (requestInfo.Header.proType == ProtocolType.ForceIFrame)
                {
                    Send <ForceIFrameRequest, ForceIFrameResponse>(vcSession, requestInfo, m_AppInstance.ForceIFrame);
                }
                else if (requestInfo.Header.proType == ProtocolType.Get_color)
                {
                    Send <GetColorRequest, ColorInfo>(vcSession, requestInfo, m_AppInstance.GetColor);
                }
                else if (requestInfo.Header.proType == ProtocolType.Set_color)
                {
                    Send <ColorInfo, SetColorResponse>(vcSession, requestInfo, m_AppInstance.SetColor);
                }
                else if (requestInfo.Header.proType == ProtocolType.Get_osdchannel)
                {
                    Send <GetOsdChannelRequest, OsdChannelInfo>(vcSession, requestInfo, m_AppInstance.GetOsdChannel);
                }
                else if (requestInfo.Header.proType == ProtocolType.Set_osdchannel)
                {
                    Send <OsdChannelInfo, SetOsdChannelResponse>(vcSession, requestInfo, m_AppInstance.SetOsdChannel);
                }
                else if (requestInfo.Header.proType == ProtocolType.Get_osddate)
                {
                    Send <GetOsdDateRequest, OsdDateInfo>(vcSession, requestInfo, m_AppInstance.GetOsdDate);
                }
                else if (requestInfo.Header.proType == ProtocolType.Set_osddate)
                {
                    Send <OsdDateInfo, SetOsdDateResponse>(vcSession, requestInfo, m_AppInstance.SetOsdDate);
                }
                else if (requestInfo.Header.proType == ProtocolType.Get_videoquality)
                {
                    Send <GetVideoQualityRequest, VideoQualityInfo>(vcSession, requestInfo, m_AppInstance.GetVideoQuality);
                }
                else if (requestInfo.Header.proType == ProtocolType.Set_videoquality)
                {
                    Send <VideoQualityInfo, SetVideoQualityResponse>(vcSession, requestInfo, m_AppInstance.SetVideoQuality);
                }
                else if (requestInfo.Header.proType == ProtocolType.Get_streamtype)
                {
                    Send <GetStreamTypeRequest, StreamTypeInfo>(vcSession, requestInfo, m_AppInstance.GetStreamType);
                }
                else if (requestInfo.Header.proType == ProtocolType.Set_streamtype)
                {
                    Send <StreamTypeInfo, SetStreamTypeResponse>(vcSession, requestInfo, m_AppInstance.SetStreamType);
                }
                else if (requestInfo.Header.proType == ProtocolType.Get_netinfo)
                {
                    Send <NetInfo>(vcSession, requestInfo, m_AppInstance.GetNetInfo);
                }
                else if (requestInfo.Header.proType == ProtocolType.Set_netinfo)
                {
                    Send <NetInfo, SetNetInfoResponse>(vcSession, requestInfo, m_AppInstance.SetNetInfo);
                }
                else if (requestInfo.Header.proType == ProtocolType.Get_systemtime)
                {
                    Send <SystemTimeInfo>(vcSession, requestInfo, m_AppInstance.GetSystemTime);
                }
                else if (requestInfo.Header.proType == ProtocolType.Set_systemtime)
                {
                    Send <SystemTimeInfo, SetSystemTimeResponse>(vcSession, requestInfo, m_AppInstance.SetSystemTime);
                }
                else if (requestInfo.Header.proType == ProtocolType.Restart_device)
                {
                    Send <RestartDeviceResponse>(vcSession, requestInfo, m_AppInstance.RestartDevice);
                }

                else if (requestInfo.Header.proType == ProtocolType.Close_device)
                {
                    Send <CloseDeviceResponse>(vcSession, requestInfo, m_AppInstance.CloseDevice);
                }
                else if (requestInfo.Header.proType == ProtocolType.Reset)
                {
                    Send <ResetDeviceResponse>(vcSession, requestInfo, m_AppInstance.ResetDevice);
                }
                else if (requestInfo.Header.proType == ProtocolType.Get_rs232cfg)
                {
                    Send <GetRs232CfgRequest, Rs232CfgInfo>(vcSession, requestInfo, m_AppInstance.GetRs232Cfg);
                }
                else if (requestInfo.Header.proType == ProtocolType.Set_rs232cfg)
                {
                    Send <Rs232CfgInfo, SetRs232CfgResponse>(vcSession, requestInfo, m_AppInstance.SetRs232Cfg);
                }
                else if (requestInfo.Header.proType == ProtocolType.Get_ptzrs232cfg)
                {
                    Send <GetPtzRs232CfgRequest, PtzRs232CfgInfo>(vcSession, requestInfo, m_AppInstance.GetPtzRs232Cfg);
                }
                else if (requestInfo.Header.proType == ProtocolType.Set_ptzrs232cfg)
                {
                    Send <PtzRs232CfgInfo, SetPtzRs232CfgResponse>(vcSession, requestInfo, m_AppInstance.SetPtzRs232Cfg);
                }
                else if (requestInfo.Header.proType == ProtocolType.Ptzcontrol)
                {
                    Send <PtzControlRequest, PtzControlResponse>(vcSession, requestInfo, m_AppInstance.PtzControl);
                }
                else if (requestInfo.Header.proType == ProtocolType.SearchFile)
                {
                    Send <SearchFileRequest, SearchFileResponse>(vcSession, requestInfo, m_AppInstance.SearchFile);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetFileInfo)
                {
                    Send <GetFileInfoRequest, GetFileInfoResponse>(vcSession, requestInfo, m_AppInstance.GetFileInfo);
                }
                else if (requestInfo.Header.proType == ProtocolType.RegisterAlarm)
                {
                    Send <RegisterAlarmRequest, RegisterAlarmResponse>(vcSession, requestInfo, m_AppInstance.SetRegisterAlarm);
                }
                else if (requestInfo.Header.proType == ProtocolType.Get_MotionExSet)
                {
                    Send <GetMotionExRequest, GetMotionExResponse>(vcSession, requestInfo, m_AppInstance.GetMotionExSet);
                }
                else if (requestInfo.Header.proType == ProtocolType.Set_MotionExSet)
                {
                    Send <SetMotionExRequest, SetMotionExResponse>(vcSession, requestInfo, m_AppInstance.SetMotionExSet);
                }
                else if (requestInfo.Header.proType == ProtocolType.Get_SubChannelSet)
                {
                    Send <GetSubChannelSetRequest, GetSubChannelSetResponse>(vcSession, requestInfo, m_AppInstance.GetSubChannelSet);
                }
                else if (requestInfo.Header.proType == ProtocolType.Set_SubChannelSet)
                {
                    Send <SetSubChannelSetRequest, SetSubChannelSetResponse>(vcSession, requestInfo, m_AppInstance.SetSubChannelSet);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetTimeEx)
                {
                    Send <GetNetSyncTimeResponse>(vcSession, requestInfo, m_AppInstance.GetNetSyncTime);
                }
                else if (requestInfo.Header.proType == ProtocolType.SetTimeEx)
                {
                    Send <NetSyncTime, SetNetSyncTimeResponse>(vcSession, requestInfo, m_AppInstance.SetNetSyncTime);
                }
                else if (requestInfo.Header.proType == ProtocolType.ForceIFrame)
                {
                    Send <ForceIFrameRequest, ForceIFrameResponse>(vcSession, requestInfo, m_AppInstance.ForceIFrame);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetDeviceCfg)
                {
                    Send <DeviceConfig>(vcSession, requestInfo, m_AppInstance.GetDeviceConfig);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetMotionSet)
                {
                    Send <GetMotionRequest, GetMotionResponse>(vcSession, requestInfo, m_AppInstance.GetMotionSet);
                }
                else if (requestInfo.Header.proType == ProtocolType.SetMotionSet)
                {
                    Send <SetMotionRequest, SetMotionResponse>(vcSession, requestInfo, m_AppInstance.SetMotionSet);
                }
                else if (requestInfo.Header.proType == ProtocolType.SyncTime)
                {
                    Send <SyncTimeRequest, SyncTimeResponse>(vcSession, requestInfo, m_AppInstance.SyncTime);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetUser)
                {
                    Send <DavinciUsers>(vcSession, requestInfo, m_AppInstance.GetUsers);
                }
                else if (requestInfo.Header.proType == ProtocolType.UpdateUser)
                {
                    Send <UpdateUserRequest, UpdateUserResponse>(vcSession, requestInfo, m_AppInstance.UpdateUser);
                }
                else if (requestInfo.Header.proType == ProtocolType.CaptureJpeg)
                {
                    Send <CaptureRequest, CapturenResponse>(vcSession, requestInfo, m_AppInstance.CaptureJpeg);
                }
                else if (requestInfo.Header.proType == ProtocolType.Get_ntpinfo)
                {
                    Send <NtpInfo>(vcSession, requestInfo, m_AppInstance.GetNtpInfo);
                }
                else if (requestInfo.Header.proType == ProtocolType.Set_ntpinfo)
                {
                    Send <NtpInfo, SetNtpInfoResponse>(vcSession, requestInfo, m_AppInstance.SetNtpInfo);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetPanoCameraList)
                {
                    Send <tQueryString, tPanoCameraList>(vcSession, requestInfo, m_AppInstance.GetPanoCameraList);
                }
                else if (requestInfo.Header.proType == ProtocolType.AddPanoCamera)
                {
                    Send <tPanoCamera, tFault>(vcSession, requestInfo, m_AppInstance.AddPanoCamera);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetPanoCamera)
                {
                    Send <tPanoCameraId, tPanoCamera>(vcSession, requestInfo, m_AppInstance.GetPanoCamera);
                }
                else if (requestInfo.Header.proType == ProtocolType.SetPanoCamera)
                {
                    Send <tPanoCamera, tFault>(vcSession, requestInfo, m_AppInstance.SetPanoCamera);
                }
                else if (requestInfo.Header.proType == ProtocolType.DeletePanoCamera)
                {
                    Send <tPanoCameraId, tFault>(vcSession, requestInfo, m_AppInstance.DeletePanoCamera);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetServiceVersion)
                {
                    Send <tServiceVersion>(vcSession, requestInfo, m_AppInstance.GetServiceVersion);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetDeviceInfo)
                {
                    Send <tDeviceInfo>(vcSession, requestInfo, m_AppInstance.GetDeviceInfo);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetDeviceStatus)
                {
                    Send <tDeviceStatus>(vcSession, requestInfo, m_AppInstance.GetDeviceStatus);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetNetworkInterface)
                {
                    Send <tNetworkInterface>(vcSession, requestInfo, m_AppInstance.GetNetworkInterface);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetDecodingUnitList)
                {
                    Send <tDecodingUnitList>(vcSession, requestInfo, m_AppInstance.GetDecodingUnitList);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetDecodingUnit)
                {
                    Send <tDecodingUnitId, tDecodingUnit>(vcSession, requestInfo, m_AppInstance.GetDecodingUnit);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetRotatingSpeed)
                {
                    Send <tDecodingUnitId, tRotatingSpeed>(vcSession, requestInfo, m_AppInstance.GetRotatingSpeed);
                }
                else if (requestInfo.Header.proType == ProtocolType.SetRotatingSpeed)
                {
                    Send <tRotatingSpeed, tFault>(vcSession, requestInfo, m_AppInstance.SetRotatingSpeed);
                }
                else if (requestInfo.Header.proType == ProtocolType.SwitchPanoCamera)
                {
                    Send <SwitchPanoCameraRequest, tFault>(vcSession, requestInfo, m_AppInstance.SwitchPanoCamera);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetViewPoint)
                {
                    Send <tDecodingUnitId, tViewPoint>(vcSession, requestInfo, m_AppInstance.GetViewPoint);
                }
                else if (requestInfo.Header.proType == ProtocolType.SetViewPoint)
                {
                    Send <SetViewPointRequest, tFault>(vcSession, requestInfo, m_AppInstance.SetViewPoint);
                }
                else if (requestInfo.Header.proType == ProtocolType.SetViewPointFixed)
                {
                    Send <SetViewPointFixedRequest, tFault>(vcSession, requestInfo, m_AppInstance.SetViewPointFixed);
                }
                else if (requestInfo.Header.proType == ProtocolType.SetViewPointRows)
                {
                    Send <SetViewPointRowsRequest, tFault>(vcSession, requestInfo, m_AppInstance.SetViewPointRows);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetPlayerStatus)
                {
                    Send <tDecodingUnitId, tPlayerStatus>(vcSession, requestInfo, m_AppInstance.GetPlayerStatus);
                }
                else if (requestInfo.Header.proType == ProtocolType.OneByOne)
                {
                    Send <OneByOneRequest, tFault>(vcSession, requestInfo, m_AppInstance.OneByOne);
                }
                else if (requestInfo.Header.proType == ProtocolType.Pause)
                {
                    Send <PauseRequest, tFault>(vcSession, requestInfo, m_AppInstance.Pause);
                }
                else if (requestInfo.Header.proType == ProtocolType.Resume)
                {
                    Send <ResumeRequest, tFault>(vcSession, requestInfo, m_AppInstance.Resume);
                }
                else if (requestInfo.Header.proType == ProtocolType.Seek)
                {
                    Send <SeekRequest, tFault>(vcSession, requestInfo, m_AppInstance.Seek);
                }
                else if (requestInfo.Header.proType == ProtocolType.GetCapability)
                {
                    session.Send(new FixedHeaderPackageInfo <ProtocolHeader>(Guid.NewGuid().ToString("N"),
                                                                             new ProtocolHeader()
                    {
                        proType = requestInfo.Header.proType, errornum = ErrorNo.HW_NET_NOSUPPORT
                    },
                                                                             null));
                }
                else
                {
                    //未注册异常应答
                    session.Send(new FixedHeaderPackageInfo <ProtocolHeader>(Guid.NewGuid().ToString("N"),
                                                                             new ProtocolHeader()
                    {
                        proType = requestInfo.Header.proType, errornum = ErrorNo.HW_NET_NOSUPPORT
                    },
                                                                             null));
                    RaisingError(new Exception(String.Format("不支持的协议类型:{0:X00000000}", requestInfo.Header.proType)));
                }
            }
            catch (Exception ex)
            {
                RaisingError(ex);
            }
        }