public override void Received(IUDTPacket packet, Destination peer) { lastPacket = packet; if (packet is ConnectionHandshake) { ConnectionHandshake hs = (ConnectionHandshake)packet; FlashLogger.Info("Received connection handshake from " + peer + "\n" + hs); if (State != ready) { if (hs.ConnectionType == 1) { try { //TODO validate parameters sent by peer // long peerSocketID = hs.SocketID; // DestinationID = peerSocketID; // destination(peerSocketID); destination.SocketID = hs.SocketID; SendConfirmation(hs); } catch (Exception ex) { FlashLogger.Warn("Error creating socket", ex); State = invalid; } return; } else { try { //TODO validate parameters sent by peer //理论上这里是getConnectionType==-1 // long peerSocketID = hs.getSocketID(); // destination.SetSocketID(peerSocketID); // setState(ready); destination.SocketID = hs.SocketID; State = ready; Thread.Sleep(50); //多个握手序列,使用接收的第一个 this.initialSequenceNumber = hs.InitialSeqNo;//cd 必须重置 socket = new UDTSocket(endPoint, this); } catch (Exception ex) { FlashLogger.Warn("Error creating socket", ex); State = invalid; } return; } } } if (State == ready) { if (packet is Shutdown) { State = shutdown; active = false; FlashLogger.Info("Connection shutdown initiated by the other side."); return; } active = true; try { if (packet.ForSender()) { socket.GetSender().Receive(lastPacket); } else { socket.GetReceiver().Receive(lastPacket); } } catch (Exception ex) { //session is invalid FlashLogger.Error("Error in " + toString(), ex); //setState(invalid); State = invalid; } return; } }
public override void Received(IUDTPacket packet, Destination peer) { lastPacket = packet; if (packet is ConnectionHandshake) { ConnectionHandshake connectionHandshake = (ConnectionHandshake)packet; FlashLogger.Info("Received " + connectionHandshake); if (State <= ready) { destination.SocketID = connectionHandshake.SocketID; if (State <= handshaking) { State = handshaking; } try { HandleHandShake(connectionHandshake); n_handshake++; try { //理论上应该先检验cookie State = ready; socket = new UDTSocket(endPoint, this); cc.Init(); } catch (Exception uhe) { //session is invalid FlashLogger.Error("", uhe); State = invalid; } } catch (IOException ex) { //session invalid FlashLogger.Warn("Error processing ConnectionHandshake", ex); State = invalid; } return; } else { //cd 回复 try { HandleHandShake(connectionHandshake); } catch (IOException e) { } } } else if (packet is KeepAlive) { socket.GetReceiver().ResetEXPTimer(); active = true; return; } if (State == ready) { active = true; if (packet is KeepAlive) { //nothing to do here return; } else if (packet is Shutdown) { try { socket.GetReceiver().Stop(); } catch (IOException ex) { FlashLogger.Warn("", ex); } State = shutdown; Console.WriteLine("SHUTDOWN ***"); active = false; FlashLogger.Info("Connection shutdown initiated by the other side."); return; } else { try { Console.WriteLine("收到数据包"); if (packet.ForSender()) { socket.GetSender().Receive(packet); } else { socket.GetReceiver().Receive(packet); } } catch (Exception ex) { //session invalid FlashLogger.Error("", ex); State = invalid; // setState(invalid); } } return; } }