public void Send(byte[] information) { CheckPacketNumber(); pocketNumber++; maintainPacket = new MCPPacket(pocketNumber, myIpAddress, myPort, MCPState.Information, information); connection.Send(maintainPacket.GetPacketBytes()); }
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(); }
private void NewMessageTask(object obj, MessageAvailableEventArgs args) { MCPPacket packet = MCPPacket.Parse(args.Message); if (packet.IpAddress.ToString() + ":" + packet.Port.ToString() != myIpAddress.ToString() + ":" + myPort.ToString()) { AnalysePacket(packet); } }
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(); }
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); } }
private void AnalysePacket(MCPPacket packet) { lock (ipCounterTable) { IPAddress ipKey = packet.IpAddress; bool isNew = false; if (ipCounterTable.ContainsKey(ipKey)) { if (ipCounterTable[ipKey] < packet.Number || packet.State == MCPState.Reset) { ipCounterTable[ipKey] = packet.Number; isNew = true; } } else { ipCounterTable.Add(ipKey, packet.Number); isNew = true; } if (isNew) { switch (packet.State) { case MCPState.Information: InformationEvent?.Invoke(this, new InformationEventArgs() { Address = packet.IpAddress, Port = packet.Port, Information = packet.Information }); break; case MCPState.MaintainConnection: MaintainEvent?.Invoke(this, new MaintainEventArgs() { Address = packet.IpAddress, Port = packet.Port }); break; case MCPState.Close: CloseEvent?.Invoke(this, new CloseEventArgs() { Address = packet.IpAddress, Port = packet.Port }); ipCounterTable.Remove(ipKey); break; default: break; } } } }