public virtual Connection GetConnection(EndPoint endPoint) { String lHost = endPoint.ToString(); lock (fSyncRoot) { ConnectionQueue lQueue = this.fCache.ContainsKey(lHost) ? (ConnectionQueue)this.fCache[lHost] : null; if (lQueue != null && lQueue.Count > 0) { return(lQueue.Dequeue()); } } return(this.GetNewConnection(endPoint)); }
// Accepts incoming connections if we are able private void AcceptPeerConnection() { // Create a wait handle array so we can cancel this thread if need be WaitHandle[] Wait = new[] { ReadyEvent, StopEvent }; while (0 == WaitHandle.WaitAny(Wait)) { // Check if stopped if (StopEvent.WaitOne(0)) { break; } // Lock our connection queue to prevent any race conditions lock (ConnectionQueue) { // Connection queue has entries, accept one if (ConnectionQueue.Count > 0) { // Dequeue the new peer in line var Connection = ConnectionQueue.Dequeue(); // Create a peer instance var Peer = new P2pPeer(this, Connection, PeerDirection.IN); // Handle this connection AddPeer(Peer); } // There are no entries in the connection queue else { // No peers in line, reset ready event ReadyEvent.Reset(); continue; } } } }
/// <summary> /// Creates and returns a private chatroom. /// </summary> public Chatroom CreatePrivateRoom(int joincount) { if (ConnectionQueue.Count < joincount) { return(null); } Chatroom privateRoom = new Chatroom(ServerManager.ChatroomManager.LastRoomId++); PrivateChatrooms.Add(privateRoom.ID, privateRoom); ServerManager.ChatroomManager.AddChatroom(privateRoom); for (int i = 0; i < joincount; i++) { ConnectionCore core = ConnectionQueue.Dequeue(); privateRoom.Join(core); core.SendMessage(new CreatePrivateRoomComposer(privateRoom)); core.SendMessage(new JoinChatroomComposer(privateRoom.ID, JoinState.JoinChatRoomOk)); } return(privateRoom); }