Пример #1
0
 static void OnNetworkStateChange(NetworkUserState oldState, NetworkUserState newState)
 {
     Action<bool, bool> d = SetVisibleAndActivate;
     if (oldState != newState)
     {
         if (newState == NetworkUserState.authorized)
         {
             loginForm.Invoke(d, true, false);
             //NetworkHandler.RequestNotes();
         }
         else if (mainForm.Visible)
         {
             mainForm.Invoke(d, false, true);
         }
     }
 }
Пример #2
0
        private static void HandleNetwork()
        {
            try
            {
                connection.Connect(serverAddr, port);
                State = NetworkUserState.connected;
                stream = connection.GetStream();
                bf = new BinaryFormatter();
                bool stopHandle = false;
                while (!stopHandle && connection.Connected)
                {
                    var message = (SToCMessage)bf.Deserialize(stream);
                    if (!ValidateState(message))
                        continue;
                    switch (message.type)
                    {
                        case SToCMessagesType.LoginAccepted:
                            State = NetworkUserState.authorized;
                            break;
                        case SToCMessagesType.NotesSync:
                            if (NotesCallback == null)
                                break;
                            var notesResponse = message as SToCNotesContainer;

                            switch (notesResponse.syncType)
                            {
                                case NotesSyncType.GetNotes:
                                    NotesCallback.OnGetNotes();
                                    break;
                                case NotesSyncType.AddNotes:
                                    NotesCallback.OnAddNotes(notesResponse.notes);
                                    break;
                                case NotesSyncType.RemoveNotes:
                                    NotesCallback.OnRemoveNotes(notesResponse.notes);
                                    break;
                                case NotesSyncType.ModifyNotes:
                                    NotesCallback.OnModifyNotes(notesResponse.notes);
                                    break;
                            }
                            break;
                        case SToCMessagesType.Disconnect:
                            stopHandle = true;
                            break;
                    }
                    if (stopHandle)
                        break;
                }
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.ToString());
            }
            finally
            {
                stream.Close();
                connection.Close();
                state = NetworkUserState.disconnected;
            }
        }
Пример #3
0
 public static void Logout()
 {
     SendMessage(new CToSMessage(CToSMessagesType.Logout));
     state = NetworkUserState.connected;
 }