Beispiel #1
0
        private void SendReceiveConfirmationMessage(int refNo, IPAddress remoteIP)
        {
            if (remoteIP == null || remoteIP == IPAddress.None)
            {
                return;
            }
            IPEndPoint receiverEndPoint = new IPEndPoint(remoteIP, MulticastGroupIpEndPoint.Port);

            Message message = new ReceiveConfirmationMessage(refNo);

            Socket udpSocket = SocketHelper.GetUdpSocketForSender();

            SendMessage(udpSocket, receiverEndPoint, message);
        }
Beispiel #2
0
        internal void SendSignOnReply(IPAddress remoteIP)
        {
            if (remoteIP == null || remoteIP == IPAddress.None)
            {
                return;
            }
            IPEndPoint receiverEndPoint = new IPEndPoint(remoteIP, MulticastGroupIpEndPoint.Port);

            Message message = new SignonMessage();

            message.HeaderData.IsReply = true;

            Socket udpSocket = SocketHelper.GetUdpSocketForSender();

            SendMessage(udpSocket, receiverEndPoint, message);
        }
Beispiel #3
0
        internal void SendCurrentUserInfoMessage(IPAddress remoteIP, bool IsReply)
        {
            if (remoteIP == null || remoteIP == IPAddress.None)
            {
                return;
            }
            IPEndPoint receiverEndPoint = new IPEndPoint(remoteIP, MulticastGroupIpEndPoint.Port);

            UserInfo currentUserInfo = UserInfo.GetCurrentUserInfo();

            Message message = new UserInfoMessage(currentUserInfo);

            message.HeaderData.IsReply = IsReply;

            Socket udpSocket = SocketHelper.GetUdpSocketForSender();

            SendMessage(udpSocket, receiverEndPoint, message);
        }
Beispiel #4
0
        internal void SendSignOn()
        {
            List <Socket> mcastSenderSockets = SocketHelper.GetUdpMulticastSocketForSender(MulticastGroupIpEndPoint);

            if (mcastSenderSockets == null || mcastSenderSockets.Count == 0)
            {
                return;
            }

            Message message = new SignonMessage();

            foreach (Socket mcastSenderSocket in mcastSenderSockets)
            {
                if (mcastSenderSocket == null)
                {
                    continue;
                }
                SendMessage(mcastSenderSocket, MulticastGroupIpEndPoint, message);
            }
        }
Beispiel #5
0
        public void SendTextMessage(string remoteIPString, string text)
        {
            if (remoteIPString == null || remoteIPString.Trim() == string.Empty)
            {
                return;
            }
            IPAddress remoteIP;

            if (IPAddress.TryParse(remoteIPString, out remoteIP) == false)
            {
                return;
            }
            if (remoteIP == null || remoteIP == IPAddress.None)
            {
                return;
            }
            IPEndPoint receiverEndPoint = new IPEndPoint(remoteIP, MulticastGroupIpEndPoint.Port);

            Message message = new TextMessage(text);

            Socket udpSocket = SocketHelper.GetUdpSocketForSender();

            SendMessage(udpSocket, receiverEndPoint, message);
        }
Beispiel #6
0
        private void UdpListenerWorkMethod()
        {
            List <Socket> udpListenerSockets = SocketHelper.GetUdpMulticastSocketForReceiver(MulticastGroupIpEndPoint);

            try
            {
                byte[]   dataBuffer = null;
                EndPoint remoteEP   = null;

                while (true)
                {
                    Socket udpListenerSocket = null;
                    while (true)
                    {
                        bool dataAvailable = false;
                        for (int i = 0; i < udpListenerSockets.Count; i++)
                        {
                            udpListenerSocket = udpListenerSockets[i];

                            if (udpListenerSocket != null && udpListenerSocket.Available > 0)
                            {
                                dataAvailable = true;
                                break;
                            }
                        }
                        if (dataAvailable == true)
                        {
                            break;
                        }
                        //Wait
                        Thread.Sleep(100);
                    }

                    remoteEP   = new IPEndPoint(IPAddress.Any, 0);
                    dataBuffer = new byte[udpListenerSocket.Available];
                    int dataLength = udpListenerSocket.ReceiveFrom(dataBuffer, ref remoteEP);

                    dataBuffer = SevenZipHelper.Decompress(dataBuffer);

                    int     msgHeaderDataLength;
                    Message message = GetMessageFromBytes(dataBuffer, out msgHeaderDataLength);

                    if (message == null || message.HeaderData == null || msgHeaderDataLength < 0)
                    {
                        continue;
                    }

                    IPAddress ipAddress = IPAddress.None;

                    if (remoteEP is IPEndPoint)
                    {
                        ipAddress = (remoteEP as IPEndPoint).Address;
                    }

                    if (message.HeaderData.MsgDataType != EMsgDataType.ReceiveConfirmation)
                    {
                        SendReceiveConfirmationMessage(message.HeaderData.RefNo, ipAddress);
                    }

                    ProcessMessage(message, ipAddress);

                    dataBuffer = null;
                    remoteEP   = null;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            for (int i = 0; i < udpListenerSockets.Count; i++)
            {
                if (udpListenerSockets[i] != null)
                {
                    udpListenerSockets[i].Close();
                }
            }
        }