public void GotControl(CdpTransmitter transmitter, out int sendBufferOffsetLimit, out bool requestImmediateAck) { CtpRequest request; if (!clients.TryGetValue(transmitter, out request)) { throw new InvalidOperationException(String.Format("Got control from client '{0}' without an entry in the dictionary", transmitter.RemoteEndPoint)); } CtpResponse response = new CtpResponse(); response.flags = 0; response.encoding = CtpEncoding.None; response.setCookies = null; if (request.resources != null) { response.resources = new Resource[request.resources.Length]; } //transmitter.RequestSendBuffer( sendBufferOffsetLimit = 0; requestImmediateAck = false; }
public void TestResendsAfterLosingAcks() { Cdp.TryStaticInit(Cdp.MaxPayloadWithIDOverUdp); IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Loopback, testUdpPort); FixedRetryTimeout timeout = new FixedRetryTimeout(100, 6); using (TestServerWrapper server = new TestServerWrapper(serverEndPoint, timeout)) { Cdp.TryStaticInit(Cdp.MaxPayloadWithIDOverUdp); // // // UdpConnectedClientTransmitter udpTransmitter = new UdpConnectedClientTransmitter(serverEndPoint); ClumsyTransmitter clumsyTransmitter = new ClumsyTransmitter(udpTransmitter, Console.Out); CdpTransmitter transmitter = new CdpTransmitter(clumsyTransmitter); Int64 startTicks = Stopwatch.GetTimestamp(); Console.WriteLine("[Sender {0} millis] Sending...", (Stopwatch.GetTimestamp() - startTicks).StopwatchTicksAsInt64Milliseconds()); UInt32 offset; Byte[] datagram = transmitter.RequestSendBuffer(10, out offset); for (Byte i = 0; i < 10; i++) { datagram[offset++] = (Byte)('A' + i); } clumsyTransmitter.DropAllReceivedDatagramsForTheNext(400); transmitter.ControllerSendPayloadWithAck(offset, timeout); server.TestSucceeded(); } }
//Int32 lastPayloadIDReceived; public CdpServerDatagramHandler(CdpTransmitter transmitter, ICdpServerHandler serverHandler, ICdpTimeout timeout) { this.transmitter = transmitter; this.serverHandler = serverHandler; this.timeout = timeout; //this.lastPayloadIDReceived = Cdp.MaxPayloadID; }
public Boolean NewConnection(CdpTransmitter transmitter, out ICdpServerHandler handler, out int maxSendBeforeAck) { connectionCount++; Console.WriteLine("[Server] New Connection from '{0}' ({1} connections)", transmitter.RemoteEndPoint, connectionCount); handler = new CdpServerHandlerForTests(transmitter); maxSendBeforeAck = 0xFF; return(false); }
public void GotControl(CdpTransmitter transmitter, out int sendBufferOffsetLimit, out bool requestImmediateAck) { throw new NotImplementedException(); }
public CdpServerHandlerForTests(CdpTransmitter transmitter) { this.transmitter = transmitter; }
public void TestMethod1() { Cdp.TryStaticInit(Cdp.MaxPayloadWithIDOverUdp); IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Loopback, testUdpPort); FixedRetryTimeout timeout = new FixedRetryTimeout(500, 6); using (TestServerWrapper server = new TestServerWrapper(serverEndPoint, timeout)) { UdpConnectedClientTransmitter udpTransmitter = new UdpConnectedClientTransmitter(serverEndPoint); CdpTransmitter transmitter = new CdpTransmitter(udpTransmitter); UInt32 offset; Byte[] myMessage; Byte[] payloadBuffer; // // Send and wait for ack // myMessage = Encoding.UTF8.GetBytes("This should be a normal payload with an immediate ack"); payloadBuffer = transmitter.RequestSendBuffer((UInt32)myMessage.Length, out offset); Array.Copy(myMessage, 0, payloadBuffer, offset, myMessage.Length); offset += (UInt32)myMessage.Length; Console.WriteLine("[Client] Sending Payload With Ack..."); transmitter.ControllerSendPayloadWithAck(offset, timeout); // // Send heartbeat // Console.WriteLine("[Client] Sending Heartbeat..."); transmitter.SendHearbeat(); // // Send Random Payload // myMessage = Encoding.UTF8.GetBytes("This should be a random payload"); payloadBuffer = transmitter.RequestSendBuffer((UInt32)myMessage.Length, out offset); Array.Copy(myMessage, 0, payloadBuffer, offset, myMessage.Length); offset += (UInt32)myMessage.Length; Console.WriteLine("[Client] Sending Random Payload..."); transmitter.ControllerSendRandomPayload(offset); // // Send Payload no ack // myMessage = Encoding.UTF8.GetBytes("This should be the first payload with no immediate ack"); payloadBuffer = transmitter.RequestSendBuffer((UInt32)myMessage.Length, out offset); Array.Copy(myMessage, 0, payloadBuffer, offset, myMessage.Length); offset += (UInt32)myMessage.Length; Console.WriteLine("[Client] Sending Payload No Ack..."); transmitter.ControllerSendPayloadNoAck(offset); // // Send Payload no ack // myMessage = Encoding.UTF8.GetBytes("This should be the second payload with no immediate ack"); payloadBuffer = transmitter.RequestSendBuffer((UInt32)myMessage.Length, out offset); Array.Copy(myMessage, 0, payloadBuffer, offset, myMessage.Length); offset += (UInt32)myMessage.Length; Console.WriteLine("[Client] Sending Payload No Ack..."); transmitter.ControllerSendPayloadNoAck(offset); // // Send and wait for ack // myMessage = Encoding.UTF8.GetBytes("This should be a normal payload with an immediate ack"); payloadBuffer = transmitter.RequestSendBuffer((UInt32)myMessage.Length, out offset); Array.Copy(myMessage, 0, payloadBuffer, offset, myMessage.Length); offset += (UInt32)myMessage.Length; Console.WriteLine("[Client] Sending Payload With Ack..."); transmitter.ControllerSendPayloadWithAck(offset, timeout); // // Send Halt // Console.WriteLine("[Client] Sending Halt..."); transmitter.SendHaltNoPayload(); server.TestSucceeded(); Console.WriteLine("[Client] Done (Success)"); } }
public static void UdpServerLoop(ICdpServer server, Socket udpSocket, EndPoint listenEndPoint, Byte[] maxDatagramBuffer, ICdpTimeout timeout) { Dictionary <EndPoint, CdpServerDatagramHandler> endPointToHandler = new Dictionary <EndPoint, CdpServerDatagramHandler>(); //Socket udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); udpSocket.Bind(listenEndPoint); try { while (true) { EndPoint from = listenEndPoint; int bytesRead; try { bytesRead = udpSocket.ReceiveFrom(maxDatagramBuffer, ref from); } catch (SocketException e) { Boolean stopServerGracefully = server.SocketException(e); if (stopServerGracefully) { throw new NotImplementedException("Stop server gracefully not implemented"); } continue; } if (bytesRead <= 0) { // it's just a heartbeat Console.WriteLine("[CdpDebug] Got a heartbeat from '{0}'", from); continue; } CdpServerDatagramHandler handler; // // Handle new connection // if (!endPointToHandler.TryGetValue(from, out handler)) { CdpTransmitter transmitter = new CdpTransmitter(new UdpConnectedServerTransmitter(udpSocket, from)); ICdpServerHandler serverHandler; Int32 maxSendBeforeAck; Boolean refuseConnection = server.NewConnection(transmitter, out serverHandler, out maxSendBeforeAck); if (refuseConnection) { handler.Closed(); server.ConnectionClosed(from); throw new NotImplementedException("Refusing connection is not yet implemented"); } if (serverHandler == null) { handler.Closed(); server.ConnectionClosed(from); throw new InvalidOperationException("You provided a null payload handler"); } handler = new CdpServerDatagramHandler(transmitter, serverHandler, timeout); endPointToHandler.Add(from, handler); } Boolean closeClient = handler.Datagram(maxDatagramBuffer, 0, bytesRead); if (closeClient) { handler.Closed(); server.ConnectionClosed(from); endPointToHandler.Remove(from); } } } finally { if (udpSocket != null) { udpSocket.Close(); } } }
public MyCdpPayloadHandler(CdpTransmitter transmitter) { this.transmitter = transmitter; }
static Int32 Main(string[] args) { CdpCatOptions optionsParser = new CdpCatOptions(); if (args.Length <= 0) { optionsParser.PrintUsage(); return(-1); } List <String> nonOptionArgs = optionsParser.Parse(args); Cdp.StaticInit(optionsParser.maxPayload.ArgValue); ICdpTimeout timeout = new MyCdpTimeout(); if (optionsParser.listenPort.set) { IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, optionsParser.listenPort.ArgValue); MyCdpServer cdpServer = new MyCdpServer(); Socket udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); Cdp.UdpServerLoop(cdpServer, udpSocket, localEndPoint, new Byte[optionsParser.maxPayload.ArgValue], timeout); return(-1); } else { if (nonOptionArgs.Count < 2) { Console.WriteLine("Missing command line arguments"); optionsParser.PrintUsage(); return(-1); } UInt16 port = UInt16.Parse(nonOptionArgs[1]); EndPoint serverEndPoint = new IPEndPoint(EndPoints.ParseIPOrResolveHost(nonOptionArgs[0], DnsPriority.IPv4ThenIPv6), port); UdpConnectedClientTransmitter udpTransmitter = new UdpConnectedClientTransmitter(serverEndPoint); CdpTransmitter transmitter = new CdpTransmitter(udpTransmitter); UInt32 offset; Byte[] myMessage; Byte[] payloadBuffer; transmitter.SendHearbeat(); myMessage = Encoding.UTF8.GetBytes("This should be a random payload"); payloadBuffer = transmitter.RequestSendBuffer((UInt32)myMessage.Length, out offset); Array.Copy(myMessage, 0, payloadBuffer, offset, myMessage.Length); transmitter.ControllerSendRandomPayload(offset + (UInt32)myMessage.Length); myMessage = Encoding.UTF8.GetBytes("This should be the first payload with no immediate ack"); payloadBuffer = transmitter.RequestSendBuffer((UInt32)myMessage.Length, out offset); Array.Copy(myMessage, 0, payloadBuffer, offset, (UInt32)myMessage.Length); transmitter.ControllerSendPayloadNoAck(offset + (UInt32)myMessage.Length); myMessage = Encoding.UTF8.GetBytes("This should be the second payload with no immediate ack"); payloadBuffer = transmitter.RequestSendBuffer((UInt32)myMessage.Length, out offset); Array.Copy(myMessage, 0, payloadBuffer, offset, (UInt32)myMessage.Length); transmitter.ControllerSendPayloadNoAck(offset + (UInt32)myMessage.Length); // // Send and wait for ack // myMessage = Encoding.UTF8.GetBytes("This should be a normal payload with an immediate ack"); payloadBuffer = transmitter.RequestSendBuffer((UInt32)myMessage.Length, out offset); Array.Copy(myMessage, 0, payloadBuffer, offset, myMessage.Length); offset += (UInt32)myMessage.Length; transmitter.ControllerSendPayloadWithAck(offset, timeout); transmitter.SendHaltNoPayload(); return(0); } }
public bool NewConnection(CdpTransmitter transmitter, out ICdpServerHandler handler, out int maxSendBeforeAck) { handler = this; maxSendBeforeAck = 32; return(false); }