[ create a new paste ] login | about

Link: http://codepad.org/UVc4kFnu    [ raw code | fork ]

C++, pasted on May 22:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/***************************************************************
 * Name:      Crypted_Ip2Ip_chatMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    Rada Florin Daniel (florin.rada87@yahoo.com)
 * Created:   2012-04-08
 * Copyright: Rada Florin Daniel ()
 * License:
 **************************************************************/

#include "Crypted_Ip2Ip_chatMain.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(Crypted_Ip2Ip_chatFrame)
#include <wx/intl.h>
#include <wx/string.h>
//*)
#include <wx/textdlg.h>
#include <wx/tokenzr.h>

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

const long Crypted_Ip2Ip_chatFrame::ID_TP_CLASA_A = 4001;
const long Crypted_Ip2Ip_chatFrame::ID_TP_CLASA_B = 4002;
const long Crypted_Ip2Ip_chatFrame::ID_TP_CLASA_C = 4003;
const long Crypted_Ip2Ip_chatFrame::ID_TP_CLASA_D = 4004;

//(*IdInit(Crypted_Ip2Ip_chatFrame
const long Crypted_Ip2Ip_chatFrame::ID_STATICTEXT1 = wxNewId();
const long Crypted_Ip2Ip_chatFrame::ID_STATICTEXT2 = wxNewId();
const long Crypted_Ip2Ip_chatFrame::ID_TP_PORT = wxNewId();
const long Crypted_Ip2Ip_chatFrame::ID_BP_CONNECT = wxNewId();
const long Crypted_Ip2Ip_chatFrame::ID_BP_DISCONNECT = wxNewId();
const long Crypted_Ip2Ip_chatFrame::ID_TP_OUT = wxNewId();
const long Crypted_Ip2Ip_chatFrame::ID_TP_INPUT = wxNewId();
const long Crypted_Ip2Ip_chatFrame::ID_BP_SEND = wxNewId();
const long Crypted_Ip2Ip_chatFrame::idMenuQuit = wxNewId();
const long Crypted_Ip2Ip_chatFrame::idMenuAbout = wxNewId();
const long Crypted_Ip2Ip_chatFrame::ID_STATUSBAR1 = wxNewId();
//*
const int Crypted_Ip2Ip_chatFrame::SERVER_ID = wxNewId();
const int Crypted_Ip2Ip_chatFrame::CONN_ID = wxNewId();
const long Crypted_Ip2Ip_chatFrame::ID_SETARI_PORT = wxNewId();
const int Crypted_Ip2Ip_chatFrame::MAX_POINTS = 3;


BEGIN_EVENT_TABLE(Crypted_Ip2Ip_chatFrame,wxFrame)
    //(*EventTable(Crypted_Ip2Ip_chatFrame)
    //*)
    EVT_TEXT(ID_TP_PORT, Crypted_Ip2Ip_chatFrame::onPortUpdate)
    EVT_SOCKET(SERVER_ID, Crypted_Ip2Ip_chatFrame::onServerEvent)
    EVT_SOCKET(CONN_ID, Crypted_Ip2Ip_chatFrame::onSocketInput)\
    EVT_TEXT_ENTER(ID_TP_INPUT, Crypted_Ip2Ip_chatFrame::Onbp_sendClick)

END_EVENT_TABLE()

Crypted_Ip2Ip_chatFrame::Crypted_Ip2Ip_chatFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(Crypted_Ip2Ip_chatFrame)
    wxMenuItem* MenuItem2;
    wxMenuItem* MenuItem1;
    wxBoxSizer* BoxSizer2;
    wxMenu* Menu1;
    wxBoxSizer* BoxSizer1;
    wxMenuBar* MenuBar1;
    wxFlexGridSizer* FlexGridSizer1;
    wxBoxSizer* BoxSizer3;
    wxMenu* Menu2;

    Create(parent, id, _("Crypted Ip2Ip chat"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER|wxFULL_REPAINT_ON_RESIZE, _T("id"));
    FlexGridSizer1 = new wxFlexGridSizer(4, 1, 0, 0);
    FlexGridSizer1->AddGrowableCol(0);
    FlexGridSizer1->AddGrowableRow(1);
    BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
    StaticText1 = new wxStaticText(this, ID_STATICTEXT1, _("Adresa"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
    BoxSizer1->Add(StaticText1, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    tp_clasaA = new wxTextCtrl(this, ID_TP_CLASA_A, wxEmptyString, wxDefaultPosition, wxSize(35,21), 0, wxDefaultValidator, _T("ID_TP_CLASA_A"));
    tp_clasaA->SetMaxLength(3);
    BoxSizer1->Add(tp_clasaA, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    tp_clasaB = new wxTextCtrl(this, ID_TP_CLASA_B, wxEmptyString, wxDefaultPosition, wxSize(35,21), 0, wxDefaultValidator, _T("ID_TP_CLASA_B"));
    tp_clasaB->SetMaxLength(3);
    BoxSizer1->Add(tp_clasaB, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    tp_clasaC = new wxTextCtrl(this, ID_TP_CLASA_C, wxEmptyString, wxDefaultPosition, wxSize(35,21), 0, wxDefaultValidator, _T("ID_TP_CLASA_C"));
    tp_clasaC->SetMaxLength(3);
    BoxSizer1->Add(tp_clasaC, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    tp_clasaD = new wxTextCtrl(this, ID_TP_CLASA_D, wxEmptyString, wxDefaultPosition, wxSize(35,21), 0, wxDefaultValidator, _T("ID_TP_CLASA_D"));
    tp_clasaD->SetMaxLength(3);
    BoxSizer1->Add(tp_clasaD, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    StaticText2 = new wxStaticText(this, ID_STATICTEXT2, _("Port:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT2"));
    BoxSizer1->Add(StaticText2, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    tp_port = new wxTextCtrl(this, ID_TP_PORT, wxEmptyString, wxDefaultPosition, wxSize(47,23), 0, wxDefaultValidator, _T("ID_TP_PORT"));
    tp_port->SetMaxLength(5);
    BoxSizer1->Add(tp_port, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    bp_connect = new wxButton(this, ID_BP_CONNECT, _("Connect"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BP_CONNECT"));
    BoxSizer1->Add(bp_connect, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    bp_disconnect = new wxButton(this, ID_BP_DISCONNECT, _("Disconnect"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BP_DISCONNECT"));
    BoxSizer1->Add(bp_disconnect, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer1->Add(BoxSizer1, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer2 = new wxBoxSizer(wxVERTICAL);
    tp_out = new wxTextCtrl(this, ID_TP_OUT, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxTE_READONLY|wxTE_WORDWRAP, wxDefaultValidator, _T("ID_TP_OUT"));
    tp_out->SetMinSize(wxSize(400,200));
    BoxSizer2->Add(tp_out, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer1->Add(BoxSizer2, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_TOP, 5);
    BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
    tp_input = new wxTextCtrl(this, ID_TP_INPUT, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER|wxHSCROLL, wxDefaultValidator, _T("ID_TP_INPUT"));
    tp_input->SetMaxLength(512);
    tp_input->SetMinSize(wxSize(400,50));
    BoxSizer3->Add(tp_input, 1, wxALL|wxEXPAND|wxALIGN_LEFT|wxALIGN_TOP, 5);
    bp_send = new wxButton(this, ID_BP_SEND, _("Send"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BP_SEND"));
    BoxSizer3->Add(bp_send, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer1->Add(BoxSizer3, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    SetSizer(FlexGridSizer1);
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);
    FlexGridSizer1->Fit(this);
    FlexGridSizer1->SetSizeHints(this);

    Connect(ID_BP_CONNECT,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Crypted_Ip2Ip_chatFrame::Onbp_connectClick);
    Connect(ID_BP_DISCONNECT,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Crypted_Ip2Ip_chatFrame::Onbp_disconnectClick);
    Connect(ID_BP_SEND,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Crypted_Ip2Ip_chatFrame::Onbp_sendClick);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&Crypted_Ip2Ip_chatFrame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&Crypted_Ip2Ip_chatFrame::OnAbout);
    //*)
    Connect(4001, 4004, wxEVT_COMMAND_TEXT_UPDATED, (wxObjectEventFunction)&Crypted_Ip2Ip_chatFrame::onClasaUpdate);
    wxIPV4address addr;
    addr.AnyAddress();
    port_local = wxGetTextFromUser(_("Selecteaza portul pe care sa asculte aplicatia."), _("Alege port"), _("3000"));
    if(port_local.empty())
    {
        wxString ports = wxString::Format(_("3000"));
        addr.Service(ports);
    }
    else
    {
        addr.Service(port_local);
    }
    mp_sockServ = new wxSocketServer(addr);
    while(!mp_sockServ->IsOk())
    {
        port_local = wxGetTextFromUser(_("Portul selectat este deja in uz sau eroare la initializare socket.\nIntrodu un alt port."), _("Alege port."));
        mp_sockServ->Destroy();
        if(port_local.empty())
        {
            port_local = _("3000");
        }
        addr.Service(port_local);
        mp_sockServ =  new wxSocketServer(addr);
    }
    mp_sockServ->SetEventHandler(*this, SERVER_ID);
    mp_sockServ->SetNotify(wxSOCKET_CONNECTION_FLAG);
    mp_sockServ->Notify(true);
    tp_input->Enable(false);
    bp_send->Enable(false);
    bp_disconnect->Enable(false);
    StatusBar1->SetStatusText(_("Ascultam pe portul: ") + port_local);
    connectat = 0;
}

Crypted_Ip2Ip_chatFrame::~Crypted_Ip2Ip_chatFrame()
{
    //(*Destroy(Crypted_Ip2Ip_chatFrame)
    //*)
}

void Crypted_Ip2Ip_chatFrame::OnQuit(wxCommandEvent& event)
{
    mp_sockClient->Destroy();
    mp_sockServ->Destroy();
    Close();
}

void Crypted_Ip2Ip_chatFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void Crypted_Ip2Ip_chatFrame::onSeteazaPort(wxCommandEvent& event)
{
    wxMessageBox(_("onSeteazaPORT"));
    //port = wxGetTextFromUser();
}



void Crypted_Ip2Ip_chatFrame::onPortUpdate(wxCommandEvent& event)
{
    size_t pos = tp_port->GetInsertionPoint();
    wxString temp = tp_port->GetLineText(0);
    if(port_extern.size() > temp.size() || tp_port->IsEmpty())
    {
        port_extern = temp;
        return;
    }
    if(temp[(pos - 1)] <= '9' && temp[(pos - 1)] >= '0')
    {
            //points_num++;
            port_extern = temp;
            return;
    }
    else
    {
        port_extern = temp.substr(0, pos - 1);
        tp_port->ChangeValue(port_extern);
        tp_port->SetInsertionPoint(pos -1);
    }
}
// cand apasam pe sent sau apasam enter aceasta metoda este invocata
// va prelua tot ce a fost introdus si il va trimite prin mp_sockClient
// Eroarea este ca nu
void Crypted_Ip2Ip_chatFrame::Onbp_sendClick(wxCommandEvent& event)
{
    wxString s;

    tp_input->SetInsertionPoint(0);
    s.clear();
    for(int i = 0; i < tp_input->GetNumberOfLines(); i++)
    {
        wxString temp = tp_input->GetLineText(i);
        if(temp.size() <= 0)
        {
            continue;
        }
        s.append(temp);
    }/**
    unsigned char len = (unsigned char)s.size();
    mp_sockClient->Write(&len, 1);
    #if wxUSE_UNICODE
        mp_sockClient->Write(s.wx_str(), len);
    #else
        mp_sockClient->Write(s.mb_str(), len);
    #endif // wxUSE_UNICODE

    tp_out->AppendText(_("Eu: ") + s);
    tp_out->AppendText(_("\n"));
    tp_input->Clear();*/
    //mp_sockClient->Write(s.wx_str(), (s.size() * sizeof(wchar_t)));
    const wxChar* buf;
    s.append(_("\0"));
    buf = s.c_str();
    size_t len;
    len = (wxStrlen(buf) + 1) * sizeof(wxChar);
    //mp_sockClient->Write(&len, 1);
    //wxMessageBox(wxString::Format(_("%i"), len));
    mp_sockClient->WriteMsg(buf, len);
    size_t write_len = mp_sockClient->LastCount();
    //tp_out->AppendText(wxString::Format(_("%i"), write_len));
    tp_out->AppendText(_("Eu: ") + s);
    tp_out->AppendText(_("\n"));
    tp_input->Clear();

}
// executa conexiunea catre un alt utilizator
void Crypted_Ip2Ip_chatFrame::Onbp_connectClick(wxCommandEvent& event)
{
    tp_out->Clear();
    //wxString adresa = clasaA + "." + clasaB + "." + clasaC + "." + clasaD; // concactam cele 4 clase intr-un ip
    if(tp_clasaA->IsEmpty() || tp_clasaB->IsEmpty() || tp_clasaC->IsEmpty() || tp_clasaD->IsEmpty())
    {
        wxMessageBox(_("Nu ati introdus ip-ul complet."), _("Eroare."), wxICON_ERROR);
        return;
    }
    if(tp_port->IsEmpty())
    {
        wxMessageBox(_("Nu ati intrudus portul la care trebuie sa va conectati."), _("Eroare."), wxICON_ERROR);
        return;
    }
    port_extern = tp_port->GetLineText(0);
    wxString adresa = tp_clasaA->GetLineText(0) + _(".") + tp_clasaB->GetLineText(0) + _(".") + tp_clasaC->GetLineText(0) + _(".") + tp_clasaD->GetLineText(0);
    //wxMessageBox((adresa + port_extern));
    wxIPV4address addr;
    addr.Hostname(adresa);
    addr.Service(port_extern);
    // creem noul socket prin care se va face comunicarea cu serverul
    mp_sockClient = new wxSocketClient();
    mp_sockClient->SetEventHandler(*this, CONN_ID);
    mp_sockClient->SetNotify(wxSOCKET_CONNECTION_FLAG | wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
    mp_sockClient->Notify(true);
    mp_sockClient->Connect(addr, false);
    mp_sockClient->WaitOnConnect(10);
    //mp_sockServ->Destroy(); // oprim sockServ , nu avem nevoie sa mai sculte atat timp cat suntem conectati la alt ip
    bp_connect->Enable(false); // orpim butonul de connect pana primim un raspuns in legatura cu connexiunea.
    if(mp_sockClient->IsConnected())
    {
        // actualizam status bar
        StatusBar1->SetStatusText((_("Ne-am connectat cu succes la : ") + adresa));
        // oprim input-ul pentr ip, port si butonul de connect.
        tp_clasaA->Enable(false);
        tp_clasaB->Enable(false);
        tp_clasaC->Enable(false);
        tp_clasaD->Enable(false);
        tp_port->Enable(false);
        bp_connect->Enable(false);
        // pornit inputul pentru chat si butonul de send
        tp_input->Enable(true);
        bp_send->Enable(true);
        //pornim butonul de disconnect
        bp_disconnect->Enable(true);
        mp_sockServ->Destroy(); // distrugem sockServer doar daca am reusit sa initiem o noua conexiune.

    }
    else
    {
        StatusBar1->SetStatusText((_("Nu ne-am putut conecta la : ") + adresa));
        //addr.Hostname("localhost");
        //addr.Service(port);
        //mp_sockServ = new wxSocketServer(addr);
        //mp_sockServ->SetEventHandler(*this, SERVER_ID);
        //mp_sockServ->SetNotify(wxSOCKET_CONNECTION_FLAG);
        //mp_sockServ->Notify(true);
        bp_connect->Enable(true); // repornim butonul de connect
        mp_sockClient->Close();
    }
}

void Crypted_Ip2Ip_chatFrame::onClasaUpdate(wxCommandEvent& event)
{
    wxTextCtrl* clasa = (wxTextCtrl*)event.GetEventObject();
    size_t pos = clasa->GetInsertionPoint();
    wxString temp = clasa->GetLineText(0);
    wxString* p_clasa;
    char min = '0';
    char max = '9';
    if(ID_TP_CLASA_A == event.GetId())
    {
        p_clasa = &clasaA;
    }
    else if(ID_TP_CLASA_B == event.GetId())
    {
        p_clasa = &clasaB;
    }
    else if(ID_TP_CLASA_C == event.GetId())
    {
        p_clasa =&clasaC;
    }
    else if(ID_TP_CLASA_D == event.GetId())
    {
        p_clasa = &clasaD;
    }
    else
    {
        p_clasa = NULL;
        return;
    }
    if(p_clasa->size() > temp.size() || clasa->IsEmpty())
    {
        *p_clasa = temp;
        return;
    }
    if(temp[(pos - 1)] <= max && temp[(pos - 1)] >= min)
    {
        if(temp[0] >= '3')
        {
            clasa->SetMaxLength(2);
        }
        else
        {
            clasa->SetMaxLength(3);
            if(3 == temp.size())
            {
                int temp_num = wxAtoi(temp);
                if(255 < temp_num)
                {

                    *p_clasa = temp.SubString(0, 1);
                    clasa->ChangeValue(*p_clasa);
                    clasa->SetInsertionPoint(pos - 1);
                }
            }
        }
    }
    else
    {
        *p_clasa = temp.substr(0, pos - 1);
        clasa->ChangeValue(*p_clasa);
        clasa->SetInsertionPoint(pos -1);
    }

}

// Deconecteaza conexiunea
void Crypted_Ip2Ip_chatFrame::Onbp_disconnectClick(wxCommandEvent& event)
{
    // -> repornim mp_sockserver pentru ascultare - e distrus atunci cand noi initiem conectarea la alt ip.
    wxIPV4address addr;
    addr.AnyAddress();
    addr.Service(port_local);
    mp_sockClient->Destroy();
    mp_sockServ = new wxSocketServer(addr);
    mp_sockServ->SetEventHandler(*this, SERVER_ID);
    mp_sockServ->SetNotify(wxSOCKET_CONNECTION_FLAG);
    mp_sockServ->Notify(true);
    // <- incheiat repornire mp_sockserver
    // -> repornim text boxurile pentru introducere ip si port + butonul de reconectare
    tp_clasaA->Enable(true);
    tp_clasaB->Enable(true);
    tp_clasaC->Enable(true);
    tp_clasaD->Enable(true);
    tp_port->Enable(true);
    bp_connect->Enable(true);
    // <- incheiat repornire text boxuri si butoane aferente
    // ->oprim text box-ul pentru input si butonul de send.
    tp_input->Enable(false);
    bp_send->Enable(false);
    // <- incheia oprire input si send
    // -> oprim butonul de disconect
    bp_disconnect->Enable(false);
    // <- incheiat oprire buton disconnect.
    // -> actualizam status bar.
    StatusBar1->SetStatusText(_("Ne-am deconectat."));
    // <- incheiat actualizare status bar
}


void Crypted_Ip2Ip_chatFrame::onServerEvent(wxSocketEvent& event)
{
    switch(event.GetSocketEvent())
    {
        case wxSOCKET_CONNECTION: // caz initializare conexiune noua din exterior
        {
            tp_out->Clear();
            // atribuim noua conexiune lui mp_sockClient pentru a nu creea un nou socket.
            mp_sockClient = (wxSocketClient*)mp_sockServ->Accept(false);
            mp_sockClient->SetEventHandler(*this, CONN_ID);
            mp_sockClient->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
            mp_sockClient->Notify(true);
            // luam adresa prin getPeer si o salvam in temp pentru a putea actualiza status bar-ul.
            wxString temp;
            wxIPV4address addr_temp;
            mp_sockClient->GetPeer(addr_temp);
            temp = addr_temp.IPAddress();
            // actualizam status bar-ul
            StatusBar1->SetStatusText(_("Am acceptat conexiunea pentru: ") + temp);
            // oprim inputul pentru ip si port + butonul de conect.
            tp_clasaA->Enable(false);
            tp_clasaB->Enable(false);
            tp_clasaC->Enable(false);
            tp_clasaD->Enable(false);
            tp_port->Enable(false);
            bp_connect->Enable(false);
            // pornim inputul pentru chat (tp_input) si butonul de send.
            tp_input->Enable(true);
            bp_send->Enable(true);
            // pornim butonul de disconnect.
            bp_disconnect->Enable(true);
            break;
        }
        case wxSOCKET_LOST:
        {
            wxMessageBox(_("Conexiunea a fost pierduta sau inchisa de catre celalat utilizator."));
        }
        default:
        {
            wxMessageBox(_("Eveniment server necunoscut."), _("Eroare."), wxICON_ERROR);
        }
    }
}
// functie chemata cand primim in eveniment al mp_sockClient
void Crypted_Ip2Ip_chatFrame::onSocketInput(wxSocketEvent& event)
{
    switch(event.GetSocketEvent())
    {
        case wxSOCKET_INPUT: // cand primim date
        {
            /*
            wxString s;
            wxIPV4address addr;
            mp_sockClient->GetPeer(addr);
            s = addr.IPAddress();
            int* len;
            mp_sockClient->Read(*len, sizeof(wxChar));
            //tp_out->AppendText("Numar caractere primite: " + wxString::Format(wxT("%i"), len) + "\n");
            wxChar* buf = new wxChar[*len + sizeof(wxChar)];
            mp_sockClient->Read(buf, (*len * sizeof(wxChar)));
            buf[*len] = '\0';
            //wxMessageBox(wxString::Format(wxT("%i"), len));
            //wxMessageBox(s);
            s.append(_(": "));
            s.append(buf);
            tp_out->AppendText(s);
            tp_out->AppendText(_("\n"));
            if(buf[len - 1] != '\n')
            {
                tp_out->AppendText("\n");
            }
            //wxMessageBox(s);
            wxMessageBox(wxString::Format(_("%d, %d"), len, sizeof(wxChar)));
            delete[] buf;
            break;*/
            wxString s;
            wxIPV4address addr;
            mp_sockClient->GetPeer(addr);
            s = addr.IPAddress();
            wxUint32 len = 256;
            //mp_sockClient->Read(&len, 1);
            wxChar* buf = new wxChar[len];
            mp_sockClient->ReadMsg(buf, len);
            tp_out->AppendText(s);
            tp_out->AppendText(_(": "));
            tp_out->AppendText(buf);
            tp_out->AppendText(_("\n"));
            delete[] buf;
            break;


        }
        case wxSOCKET_LOST: // conexiune pierduta
        {
            wxMessageBox(_("Conexiunea a fost pierduta, sau inchisa de utilizator."));
            tp_clasaA->Enable(true);
            tp_clasaB->Enable(true);
            tp_clasaC->Enable(true);
            tp_clasaD->Enable(true);
            tp_port->Enable(true);
            bp_connect->Enable(true);
            // <- incheiat repornire text boxuri si butoane aferente
            // ->oprim text box-ul pentru input si butonul de send.
            tp_input->Enable(false);
            bp_send->Enable(false);
            // <- incheia oprire input si send
            // -> oprim butonul de disconect
            bp_disconnect->Enable(false);
            wxIPV4address addr;
            addr.AnyAddress();
            addr.Service(port_local);
            mp_sockClient->Destroy();
            mp_sockServ = new wxSocketServer(addr);
            mp_sockServ->SetEventHandler(*this, SERVER_ID);
            mp_sockServ->SetNotify(wxSOCKET_CONNECTION_FLAG);
            mp_sockServ->Notify(true);
            break;

        }
        default:
        {

        }
    }
}


Create a new paste based on this one


Comments: