示例#1
0
        public SimConn(SimSocket socket, SimEndpoint remoteAddress, SimProc proc, uint seq, uint ackNumber)
        {
            _socket       = socket;
            RemoteAddress = remoteAddress;
            _proc         = proc;

            _sequenceNumber = seq;
            _ackNumber      = ackNumber;
        }
示例#2
0
        public async Task <IConn> Connect(SimProc process, SimEndpoint destination)
        {
            SimRoute route;

            if (!Cluster.TryGetRoute(Name, destination.Machine, out route))
            {
                throw new IOException($"Route not found");
            }


            // todo: allocate port
            // todo: allow Azure SNAT delay scenario
            var socketId     = NextSocketID();
            var source       = new SimEndpoint(Name, socketId);
            var clientSocket = new SimSocket(process, source, Cluster);

            try {
                _sockets.Add(socketId, clientSocket);


                var conn = new SimConn(clientSocket, destination, process, 0, 0);

                clientSocket._connections.Add(destination, conn);


                // handshake
                await conn.Write(null, SimFlag.Syn);

                var response = await conn.Read(5.Sec());

                if (response.Flag != (SimFlag.Ack | SimFlag.Syn))
                {
                    Debug(LogType.Warning, $"Bad handshake: {response.BodyString()}");
                    await conn.Write(null, SimFlag.Reset);

                    clientSocket._connections.Remove(destination);
                    throw new IOException($"Failed to connect (got {response.BodyString()})");
                }

                await conn.Write(null, SimFlag.Ack);

                return(new ClientConn(conn));
            } catch (IOException) {
                throw;
            }
            catch (Exception ex) {
                _sockets.Remove(socketId);
                clientSocket.Dispose();
                throw new IOException($"Failed to connect: {ex.Message}", ex);
            }
        }
示例#3
0
        public async Task <ISocket> Bind(SimProc proc, ushort port, TimeSpan timeout)
        {
            // socket is bound to the owner
            var endpoint = new SimEndpoint(proc.Id.Machine, port);

            if (_sockets.ContainsKey(port))
            {
                throw new IOException($"Address {endpoint} in use");
            }

            var socket = new SimSocket(proc, endpoint, Cluster);

            _sockets.Add(port, socket);

            proc.RegisterSocket(socket);

            return(socket);
        }
示例#4
0
 public void RegisterSocket(SimSocket proc)
 {
     _sockets.Add(proc.Endpoint.Port, proc);
 }