Advertisement
Jinfaa

severConsole.cs

Feb 7th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.48 KB | None | 0 0
  1. using System;
  2. using System.Runtime.Remoting;
  3. using System.Runtime.Remoting.Channels;
  4. using System.Runtime.Remoting.Channels.Tcp;
  5. using OperClassSpace;
  6. using ServerAppSpace;
  7. using ServerClassSpace;
  8. using LinkedList;
  9.  
  10. namespace Server
  11. {
  12.     class ProgramServer : IServerApp
  13.     {
  14.         private LinkedList.LinkedList userList = new LinkedList.LinkedList();
  15.         private static int _globalPortNo = 10000;
  16.  
  17.         [STAThread]
  18.         public static void Main(string[] args)
  19.         {
  20.             var channel = new TcpChannel(9001);
  21.             ChannelServices.RegisterChannel(channel);
  22.  
  23.             var remService = new ServerClass();
  24.             var obj = RemotingServices.Marshal(remService, "TcpService");
  25.  
  26.             var frmMain = new ProgramServer();
  27.  
  28.             // marshaling
  29.             remService.TheMainServer = frmMain;
  30.  
  31.             Console.WriteLine("Server start!");
  32.             Console.WriteLine("please press enter...");
  33.             Console.ReadLine();
  34.  
  35.             // Сервер закрывается
  36.  
  37.             frmMain.DropClients();
  38.  
  39.             while (frmMain.userList.IsListEmpty() == false) ;//well...this part may arise question marks...when we called DropClients() method,
  40.             //the clients will logoff the server but if the server closes before the clients loggedoff completely, clients will create exception...
  41.             //so we will wait until all clients logs off...
  42.             RemotingServices.Unmarshal(obj);
  43.             RemotingServices.Disconnect(remService);
  44.         }
  45.  
  46.         private void InvokeOper(string msg, int MsgOrUser, string receiveruser)     //This module calls the related client or clients...it will send them msg info, userlist info or both...
  47.         {
  48.             if (receiveruser == null)       //all users will be notified
  49.             {
  50.                 switch (MsgOrUser)
  51.                 {
  52.                     case 1:         //only message will be send...
  53.                         {
  54.                             for (userList.Init(); userList.More(); userList.Next())
  55.                             {
  56.                                 ListItem item = userList.GetCurrent();
  57.                                 string receivermach = userList.ReadItemValue(item);
  58.                                 string receiverport = userList.ReadItemValue(userList.GotoSubitem(item, 1));
  59.  
  60.                                 var clientobj =
  61.                                     (OperClass)Activator.GetObject
  62.                                     (
  63.                                     typeof(OperClass),
  64.                                     "tcp://" + receivermach + ":" + receiverport + "/TcpClient"     //====>>>> Portno here
  65.                                     );
  66.  
  67.                                 clientobj.UpdateOper(msg, null, 1);
  68.                             }
  69.  
  70.                             break;
  71.                         }
  72.                     case 2:         //only userlist will be sent....because the userlist is a shared data among all clients, it will always be sent to all of the clients.
  73.                         {
  74.                             string usernamestring = "";
  75.                             for (userList.Init(); userList.More(); userList.Next())
  76.                             {
  77.                                 usernamestring += (userList.ReadItemValue(userList.GotoSubitem(userList.GetCurrent(), 2))) + ":";
  78.                             }
  79.  
  80.                             for (userList.Init(); userList.More(); userList.Next())
  81.                             {
  82.                                 ListItem item = userList.GetCurrent();
  83.                                 string receivermach = userList.ReadItemValue(item);
  84.                                 string receiverport = userList.ReadItemValue(userList.GotoSubitem(item, 1));
  85.  
  86.                                 var clientobj =
  87.                                     (OperClass)Activator.GetObject
  88.                                     (
  89.                                     typeof(OperClass),
  90.                                     "tcp://" + receivermach + ":" + receiverport + "/TcpClient"     //====>>>> Portno here
  91.                                     );
  92.  
  93.                                 clientobj.UpdateOper(null, usernamestring, 2);
  94.                             }
  95.                             break;
  96.                         }
  97.                     case 3:         //both message and userlist info will be send....
  98.                         {
  99.                             string usernamestring = "";
  100.                             for (userList.Init(); userList.More(); userList.Next())         //usernameString is the collection of all usernames separated with ":"
  101.                             {
  102.                                 usernamestring += (userList.ReadItemValue(userList.GotoSubitem(userList.GetCurrent(), 2))) + ":";   //usernames of all online clients are collected here to pass to the client.
  103.                             }
  104.  
  105.                             for (userList.Init(); userList.More(); userList.Next())     //iterates through the userList
  106.                             {
  107.                                 ListItem item = userList.GetCurrent();
  108.                                 string receiverMach = userList.ReadItemValue(item);
  109.                                 string receiverPort = userList.ReadItemValue(userList.GotoSubitem(item, 1));
  110.                                 //                          string dareceiverUser = userList.ReadItemValue(userList.GotoSubitem(item, 2));
  111.  
  112.  
  113.                                 OperClass clientobj =
  114.                                     (OperClass)Activator.GetObject
  115.                                     (
  116.                                     typeof(OperClass),
  117.                                     "tcp://" + receiverMach + ":" + receiverPort + "/TcpClient"     //====>>>> Portno here
  118.                                     );
  119.                                
  120.                                 clientobj.UpdateOper(msg, usernamestring, 3);       //msg is taken as a parameter.
  121.                                 clientobj.UpdateOper(msg, usernamestring, 3);       //msg is taken as a parameter.
  122.                             }
  123.                             break;
  124.                         }
  125.                 }
  126.  
  127.             }
  128.             else                            //message will be sent only to the receiveruser....
  129.             {
  130.                 ListItem ptr = userList.SearchSubitem(2, receiveruser); //gets the list adress of the receiver.
  131.  
  132.                 string receivermach = userList.ReadItemValue(ptr);
  133.                 string receiverport = userList.ReadItemValue(userList.GotoSubitem(ptr, 1));
  134.  
  135.                 var clientobj =
  136.                     (OperClass)Activator.GetObject
  137.                     (
  138.                     typeof(OperClass),
  139.                     "tcp://" + receivermach + ":" + receiverport + "/TcpClient"     //====>>>> Portno here
  140.                     );
  141.  
  142.                 clientobj.UpdateOper(msg, null, 1);
  143.             }
  144.         }
  145.  
  146.         private void DropClients()
  147.         {
  148.             throw new NotImplementedException();
  149.         }
  150.  
  151.         public void RegisterUser(string machinename, string portno, string username)
  152.         {
  153.             ListItem item = userList.AddItem(null, 0, machinename);                     //adds user to the linked list that i provided...
  154.             userList.AddItem(item, 3, portno); //
  155.             userList.AddItem(item, 3, username); //
  156.  
  157.  
  158.             Console.WriteLine(username + " has logged in");
  159.  
  160.             string tempmsg = username + " has logged in";
  161.  
  162.             InvokeOper(tempmsg, 3, null);
  163.         }
  164.  
  165.         public void UnRegisterUser(string machinename, string username)
  166.         {
  167.             int count = 0;
  168.  
  169.             ListItem ptr = null;
  170.             ptr = userList.SearchSubitem(2, username); //gets the list adress of the receiver.
  171.  
  172.             if (ptr != null)
  173.                 count = userList.DeleteItem(machinename, ptr);
  174.  
  175.             if (count > 0)
  176.                 userList.RenumberList(count);
  177.  
  178.             string tempmsg = username + " вышел";
  179.             Console.WriteLine(tempmsg);
  180.  
  181.             InvokeOper(tempmsg, 3, null);
  182.         }
  183.  
  184.         public void SendMsg(string sendermachine, string senderusername, string receiverusername, bool isGlobal, string msgString)      //sends message
  185.         {
  186.             if (isGlobal == false)      //to the receiver user only
  187.             {
  188.                 msgString = DateTime.Now.ToString() + "\n" + senderusername + ": " + msgString;
  189.  
  190.                 ListItem ptr = userList.SearchSubitem(2, receiverusername); //gets the list adress of the receiver.
  191.                 string receiverMach = userList.ReadItemValue(ptr);
  192.  
  193.                 InvokeOper(msgString, 1, receiverusername);
  194.             }
  195.             else                        //global message...all users will receive the message
  196.             {
  197.                 msgString = DateTime.Now.ToString() + "\n" + "(GLOBAL) " + senderusername + ": " + msgString;
  198.                 InvokeOper(msgString, 1, null);
  199.             }
  200.         }
  201.  
  202.         public string GetMsgs(string machinename)
  203.         {
  204.             throw new NotImplementedException();
  205.         }
  206.  
  207.         public int ServerGivePortNo()                       //assigns a port no. for each new client...
  208.         {
  209.             return _globalPortNo++;
  210.         }
  211.  
  212.         public bool UserNameExists(string username)     //checks if the username exists or not
  213.         {
  214.             ListItem ptr = null;
  215.             ptr = userList.SearchSubitem(2, username);      //gets the list adress of the receiver.
  216.  
  217.             if (ptr != null)
  218.                 return true;
  219.  
  220.             return false;
  221.         }
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement