Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Runtime.Remoting;
- using System.Runtime.Remoting.Channels;
- using System.Runtime.Remoting.Channels.Tcp;
- using OperClassSpace;
- using ServerAppSpace;
- using ServerClassSpace;
- using LinkedList;
- namespace Server
- {
- class ProgramServer : IServerApp
- {
- private LinkedList.LinkedList userList = new LinkedList.LinkedList();
- private static int _globalPortNo = 10000;
- [STAThread]
- public static void Main(string[] args)
- {
- var channel = new TcpChannel(9001);
- ChannelServices.RegisterChannel(channel);
- var remService = new ServerClass();
- var obj = RemotingServices.Marshal(remService, "TcpService");
- var frmMain = new ProgramServer();
- // marshaling
- remService.TheMainServer = frmMain;
- Console.WriteLine("Server start!");
- Console.WriteLine("please press enter...");
- Console.ReadLine();
- // Сервер закрывается
- frmMain.DropClients();
- while (frmMain.userList.IsListEmpty() == false) ;//well...this part may arise question marks...when we called DropClients() method,
- //the clients will logoff the server but if the server closes before the clients loggedoff completely, clients will create exception...
- //so we will wait until all clients logs off...
- RemotingServices.Unmarshal(obj);
- RemotingServices.Disconnect(remService);
- }
- 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...
- {
- if (receiveruser == null) //all users will be notified
- {
- switch (MsgOrUser)
- {
- case 1: //only message will be send...
- {
- for (userList.Init(); userList.More(); userList.Next())
- {
- ListItem item = userList.GetCurrent();
- string receivermach = userList.ReadItemValue(item);
- string receiverport = userList.ReadItemValue(userList.GotoSubitem(item, 1));
- var clientobj =
- (OperClass)Activator.GetObject
- (
- typeof(OperClass),
- "tcp://" + receivermach + ":" + receiverport + "/TcpClient" //====>>>> Portno here
- );
- clientobj.UpdateOper(msg, null, 1);
- }
- break;
- }
- 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.
- {
- string usernamestring = "";
- for (userList.Init(); userList.More(); userList.Next())
- {
- usernamestring += (userList.ReadItemValue(userList.GotoSubitem(userList.GetCurrent(), 2))) + ":";
- }
- for (userList.Init(); userList.More(); userList.Next())
- {
- ListItem item = userList.GetCurrent();
- string receivermach = userList.ReadItemValue(item);
- string receiverport = userList.ReadItemValue(userList.GotoSubitem(item, 1));
- var clientobj =
- (OperClass)Activator.GetObject
- (
- typeof(OperClass),
- "tcp://" + receivermach + ":" + receiverport + "/TcpClient" //====>>>> Portno here
- );
- clientobj.UpdateOper(null, usernamestring, 2);
- }
- break;
- }
- case 3: //both message and userlist info will be send....
- {
- string usernamestring = "";
- for (userList.Init(); userList.More(); userList.Next()) //usernameString is the collection of all usernames separated with ":"
- {
- usernamestring += (userList.ReadItemValue(userList.GotoSubitem(userList.GetCurrent(), 2))) + ":"; //usernames of all online clients are collected here to pass to the client.
- }
- for (userList.Init(); userList.More(); userList.Next()) //iterates through the userList
- {
- ListItem item = userList.GetCurrent();
- string receiverMach = userList.ReadItemValue(item);
- string receiverPort = userList.ReadItemValue(userList.GotoSubitem(item, 1));
- // string dareceiverUser = userList.ReadItemValue(userList.GotoSubitem(item, 2));
- OperClass clientobj =
- (OperClass)Activator.GetObject
- (
- typeof(OperClass),
- "tcp://" + receiverMach + ":" + receiverPort + "/TcpClient" //====>>>> Portno here
- );
- clientobj.UpdateOper(msg, usernamestring, 3); //msg is taken as a parameter.
- clientobj.UpdateOper(msg, usernamestring, 3); //msg is taken as a parameter.
- }
- break;
- }
- }
- }
- else //message will be sent only to the receiveruser....
- {
- ListItem ptr = userList.SearchSubitem(2, receiveruser); //gets the list adress of the receiver.
- string receivermach = userList.ReadItemValue(ptr);
- string receiverport = userList.ReadItemValue(userList.GotoSubitem(ptr, 1));
- var clientobj =
- (OperClass)Activator.GetObject
- (
- typeof(OperClass),
- "tcp://" + receivermach + ":" + receiverport + "/TcpClient" //====>>>> Portno here
- );
- clientobj.UpdateOper(msg, null, 1);
- }
- }
- private void DropClients()
- {
- throw new NotImplementedException();
- }
- public void RegisterUser(string machinename, string portno, string username)
- {
- ListItem item = userList.AddItem(null, 0, machinename); //adds user to the linked list that i provided...
- userList.AddItem(item, 3, portno); //
- userList.AddItem(item, 3, username); //
- Console.WriteLine(username + " has logged in");
- string tempmsg = username + " has logged in";
- InvokeOper(tempmsg, 3, null);
- }
- public void UnRegisterUser(string machinename, string username)
- {
- int count = 0;
- ListItem ptr = null;
- ptr = userList.SearchSubitem(2, username); //gets the list adress of the receiver.
- if (ptr != null)
- count = userList.DeleteItem(machinename, ptr);
- if (count > 0)
- userList.RenumberList(count);
- string tempmsg = username + " вышел";
- Console.WriteLine(tempmsg);
- InvokeOper(tempmsg, 3, null);
- }
- public void SendMsg(string sendermachine, string senderusername, string receiverusername, bool isGlobal, string msgString) //sends message
- {
- if (isGlobal == false) //to the receiver user only
- {
- msgString = DateTime.Now.ToString() + "\n" + senderusername + ": " + msgString;
- ListItem ptr = userList.SearchSubitem(2, receiverusername); //gets the list adress of the receiver.
- string receiverMach = userList.ReadItemValue(ptr);
- InvokeOper(msgString, 1, receiverusername);
- }
- else //global message...all users will receive the message
- {
- msgString = DateTime.Now.ToString() + "\n" + "(GLOBAL) " + senderusername + ": " + msgString;
- InvokeOper(msgString, 1, null);
- }
- }
- public string GetMsgs(string machinename)
- {
- throw new NotImplementedException();
- }
- public int ServerGivePortNo() //assigns a port no. for each new client...
- {
- return _globalPortNo++;
- }
- public bool UserNameExists(string username) //checks if the username exists or not
- {
- ListItem ptr = null;
- ptr = userList.SearchSubitem(2, username); //gets the list adress of the receiver.
- if (ptr != null)
- return true;
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement