Beispiel #1
0
 public void Send(byte[] information)
 {
     CheckPacketNumber();
     pocketNumber++;
     maintainPacket = new MCPPacket(pocketNumber, myIpAddress, myPort, MCPState.Information, information);
     connection.Send(maintainPacket.GetPacketBytes());
 }
Beispiel #2
0
        public MCPConnector(IPAddress multicastAddress, int port, int ttl, IPAddress myIpAddress, int myPort, int maintainDelay)
        {
            //create connection for listener
            IPEndPoint whom      = new IPEndPoint(multicastAddress, port);//default, becase listener will't send
            IPEndPoint wherefrom = whom;
            IPEndPoint bindEP    = new IPEndPoint(IPAddress.Any, port);

            connection = new MulticastUdpConnection(whom, wherefrom, bindEP, ttl);

            //create listener
            listener = new Listener();
            listener.Init(connection);
            listener.MessageAvailableEvent += NewMessageTask;

            this.myIpAddress   = myIpAddress;
            this.myPort        = myPort;
            this.maintainDelay = maintainDelay;

            pocketNumber   = 0;
            maintainPacket = new MCPPacket(pocketNumber, myIpAddress, myPort, MCPState.Reset);
            Send(maintainPacket.GetPacketBytes());
            pocketNumber  += 1;
            maintainPacket = new MCPPacket(pocketNumber, myIpAddress, myPort, MCPState.MaintainConnection);
            maintainThread = new Thread(MaintainConnection);
            maintainThread.Start();
        }
Beispiel #3
0
 public void Close()
 {
     CheckPacketNumber();
     pocketNumber++;
     maintainPacket = new MCPPacket(pocketNumber, myIpAddress, myPort, MCPState.Close);
     connection.Send(maintainPacket.GetPacketBytes());
     IsWork = false;
     maintainThread.Abort();
     Stop();
     //close and connection too
     listener.Close();
 }
Beispiel #4
0
 private void MaintainConnection()
 {
     while (true)
     {
         while (IsWork)
         {
             connection.Send(maintainPacket.GetPacketBytes());
             Thread.Sleep(maintainDelay);
         }
         waitHandle.WaitOne();
     }
 }
Beispiel #5
0
        private void CheckPacketNumber()
        {
            Random random = new Random();

            if (pocketNumber == UInt32.MaxValue)
            {
                maintainPacket = new MCPPacket(0, myIpAddress, myPort, MCPState.Reset);
                connection.Send(maintainPacket.GetPacketBytes());
                pocketNumber = 1;
                int sleeptime = random.Next() % (2 * maintainDelay) + maintainDelay;
                Thread.Sleep(sleeptime);
            }
        }