Esempio n. 1
0
 //Add new user to client socket list
 private void newUserConnected(ClientSocket tmpClientSocket)
 {
     this._clientSockets.Add(tmpClientSocket.GetID(),tmpClientSocket);
     this._availableIDs.Remove(tmpClientSocket.GetID());
 }
Esempio n. 2
0
        //#############Accept and receive messages
        private void AcceptCallBack(IAsyncResult ar)
        {
            allDone.Set();
            try
            {
                Socket tmpSocket = _serverSocket.EndAccept(ar);
                ClientSocket tmpClientSocket = null;
                bool created = false;

                lock (_availableIDsBaton)
                {
                    if (this._availableIDs.Count > 0)
                    {
                        tmpClientSocket = new ClientSocket(NextIDAvailable(), tmpSocket);
                        tmpClientSocket._localBuffer = new Byte[tmpSocket.ReceiveBufferSize];
                        this.newUserConnected(tmpClientSocket);
                        created = true;
                    }

                }

                if (created)
                {
                    tmpClientSocket._clientSocket.BeginReceive(tmpClientSocket._localBuffer, 0, tmpClientSocket._localBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), tmpClientSocket);
                }
                else
                {
                    disconnectSocket(tmpSocket);
                }

            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }