예제 #1
0
        public TcpConnection(TcpServer parent, Socket socket, int bufferSize, Type cipherType)
        {
            if (cipherType != null)
            {
                cipher = Activator.CreateInstance(cipherType) as ICipher;
            }
            this.socket         = socket;
            this.recv_buffer    = new byte[bufferSize];
            this.storage_buffer = new byte[0];
            this.parent         = parent;
            this.ConnectionId   = StrongRandom.NextInt(1, int.MaxValue);

            socket.BeginReceive(recv_buffer, 0,
                                recv_buffer.Length, SocketFlags.None,
                                new AsyncCallback(ReceiveCallback), null);
        }
예제 #2
0
        public int RegisterSession(string remoteIp, IConnectedObjectUdp obj)
        {
            UdpSession session = new UdpSession();

            session.address = IPAddress.Parse(remoteIp);
            session.obj     = obj;
            obj.UdpSession  = session;

            lock (sessions)
            {
                while (true)
                {
                    session.sessionId = StrongRandom.NextInt(int.MinValue, int.MaxValue);
                    if (!sessions.ContainsKey(session.sessionId))
                    {
                        sessions.Add(session.sessionId, session);
                        break;
                    }
                }
            }

            return(session.sessionId);
        }