コード例 #1
0
ファイル: Sock.cs プロジェクト: temper8/chatter
        static public void StartClientSide(Socket sender, string buddyIp)
        {
            beginThread();

            debug("Client thread launched");
            int errors = 0;

            try
            {
#if !DEBUG_LOOPBACK
                if (!checkForRunningThread(buddyIp))
                {
                    endThread();
                    debug("Client ignored this ip: " + buddyIp);
                    return;
                }
#endif


                if (!ipBuddyMessages.ContainsKey(buddyIp))
                {
                    ipBuddyMessages.Add(buddyIp, new Queue <string>());
                }

                if (!ipBuddyIsTyping.ContainsKey(buddyIp))
                {
                    ipBuddyIsTyping.Add(buddyIp, 0);
                }

                ipBuddyMessages[buddyIp].Enqueue(CMessageHandler.GenerateMessage(myPCName, myIP, "Connected!"));

                CMessageHandler m = new CMessageHandler("ClientMH", buddyIp, myIP);

                // Enter the working loop
                while (stayAlive)
                {
                    if (ipBuddyMessages[buddyIp].Count > 0)
                    {
                        lock (ipBuddyMessages[buddyIp])
                        {
                            try
                            {
                                m.AddMessageToSend(ipBuddyMessages[buddyIp].Dequeue());
                            }
                            catch { }
                        }
                    }

                    MessageEventArgs         mea;
                    CMessageHandler.MsgState state = m.ProcessStates(out mea);
                    messageHandling(m, mea);

                    // Write to Server as needed
                    if (state == CMessageHandler.MsgState.ReadyForRemote || state == CMessageHandler.MsgState.SendAck)
                    {
                        debug("Client sent out msg to " + buddyIp);
                        send(sender, m.MessageReady, m);
                    }
                    else
                    {
                        if (myTypingStatus > 0)
                        {
                            send(sender, "\t", m);
                        }
                        else
                        {
                            send(sender, " ", m);
                        }
                        Thread.Sleep(150); // slow down pinging
                    }

                    if (!receive(sender, m))
                    {
                        if (errors++ >= 3)
                        {
                            debug("Client error " + buddyIp);
                            break;
                        }
                    }
                    else
                    {
                        errors = 0;
                    }

                    CSavedIPs.AppendIP(buddyIp);
                }

                sender.Shutdown(SocketShutdown.Both);
                sender.Close();
            }
            catch (Exception e)
            {
                debug("Client exception for " + buddyIp + e.ToString());
                if (_debug != null && ipBuddyFullNameLookup.ContainsKey(buddyIp))
                {
                    _debug.InjectTestMessage("User aborted: " + ipBuddyFullNameLookup[buddyIp]);
                }
            }

            lock (ipsInUse)
            {
                if (ipsInUse.Contains(buddyIp))
                {
                    ipsInUse.Remove(buddyIp);
                }
            }
            //lock (ipBuddyMessages)
            //{
            //    if (ipBuddyMessages.ContainsKey(buddyIp))
            //        ipBuddyMessages.Remove(buddyIp);
            //}

            endThread();
        }
コード例 #2
0
ファイル: Sock.cs プロジェクト: temper8/chatter
        static private void StartServerSide(Socket handler, string buddyIp)
        {
            beginThread();

            Thread.Sleep(50);
            debug("Server thread launched");
            int errors = 0;

            try
            {
                CMessageHandler m = new CMessageHandler("ServerMH", buddyIp, myIP);

                if (!ipBuddyMessages.ContainsKey(buddyIp))
                {
                    ipBuddyMessages.Add(buddyIp, new Queue <string>());
                }

                if (!ipBuddyIsTyping.ContainsKey(buddyIp))
                {
                    ipBuddyIsTyping.Add(buddyIp, 0);
                }

                ipBuddyMessages[buddyIp].Enqueue(CMessageHandler.GenerateMessage(myPCName, myIP, "Connected!"));

                handler.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, false);

                // An incoming connection needs to be processed.
                while (stayAlive)
                {
                    if (ipBuddyMessages[buddyIp].Count > 0)
                    {
                        lock (ipBuddyMessages[buddyIp])
                        {
                            try
                            {
                                m.AddMessageToSend(ipBuddyMessages[buddyIp].Dequeue());
                            }
                            catch { }
                        }
                    }

                    MessageEventArgs         mea;
                    CMessageHandler.MsgState state = m.ProcessStates(out mea);
                    messageHandling(m, mea);

                    // Write to Client as needed
                    if (state == CMessageHandler.MsgState.ReadyForRemote || state == CMessageHandler.MsgState.SendAck)
                    {
                        debug("Server sent out msg to " + buddyIp);

                        send(handler, m.MessageReady, m);
                    }
                    else
                    {
                        if (myTypingStatus > 0)
                        {
                            send(handler, "\t", m);
                        }
                        else
                        {
                            send(handler, " ", m);
                        }
                        Thread.Sleep(150); // slow down pinging
                    }

                    if (!receive(handler, m))
                    {
                        if (errors++ >= 3)
                        {
                            debug("Server error " + buddyIp);
                            break;
                        }
                    }
                    else
                    {
                        errors = 0;
                    }

                    CSavedIPs.AppendIP(buddyIp);
                }
            }
            catch (Exception e)
            {
                debug("Server exception for " + buddyIp + e.ToString());
                if (_debug != null && ipBuddyFullNameLookup.ContainsKey(buddyIp))
                {
                    _debug.InjectTestMessage("User aborted: " + ipBuddyFullNameLookup[buddyIp]);
                }
            }

            if (handler != null)
            {
                handler.Shutdown(SocketShutdown.Both);
                handler.Disconnect(false);
                handler.Close();
                //handler.Dispose();
            }


            lock (ipsInUse)
            {
                if (ipsInUse.Contains(buddyIp))
                {
                    ipsInUse.Remove(buddyIp);
                }
            }
            //lock (ipBuddyMessages)
            //{
            //    if (ipBuddyMessages.ContainsKey(buddyIp))
            //        ipBuddyMessages.Remove(buddyIp);
            //}

            endThread();
        }